In [1]:
import sys
sys.path.append('../src')
import Pyfuzzy as Fuzz
import numpy as np
import pylab as plt
import pandas as pd
%matplotlib inline
In [2]:
f1=Fuzz.read_model('zr_simple.fis') # 27 rules, 3*3*3 membership functions (soil, T, P))
# f1=Fuzz.read_model('zr_simple334.fis') # 36 rules, 3*4*3 membership functions
# f1=Fuzz.read_model('zr_simple344.fis') # 48 rules, 3*4*4 membership functions
In [3]:
f1.show_model(tag1=0,default1=60)
('a0:', 50.0, 'b0:', 68.0)
('a1:', 50.0, 'b1:', 380.0)
('a:', 50.0, 'b:', 380.0, 'c:', 60.0)
Out[3]:
True
In [4]:
f1.show_model(tag1=1,default1=55)
('a0:', 20.0, 'b0:', 60.0)
('a1:', 50.0, 'b1:', 380.0)
('a:', 50.0, 'b:', 380.0, 'c:', 55.0)
Out[4]:
True
In [5]:
f1.show_model(tag1=2,default1=150)
('a0:', 20.0, 'b0:', 60.0)
('a1:', 50.0, 'b1:', 68.0)
('a:', 50.0, 'b:', 68.0, 'c:', 150.0)
Out[5]:
True
In [6]:
ZR=pd.ExcelFile('ZR_Daten_DDR_1976_1990.xlsx')
df=ZR.parse("Tabelle1")
df[(df['ERTRAG (dt/ha)']<600) & (df['ERTRAG (dt/ha)']>100)] # exclude outliers
Out[6]:
FRUCHTART
ERNTEJAHR
NATUERLICHE_STANDORTEINHEIT
Mittlere Ackerzahl
TAG_AUSSAAT
TAG_AUFGANG
Tage Aussaat bis Aufgang
N_DUENGUNG
TAG_ERNTEABSCHLUSS
ERTRAG (dt/ha)
LT_1
LT_2
LT_3
LT_4
LT_5
LT_6
LT_7
LT_8
LT_9
LT_10
0
Zuckerrüben zur Zuckergewinnun
1976
D5/N
48
116
134
18
200
311
275.0
0.8
-0.2
0.9
5.6
11.5
15.7
18.3
16.8
13.5
9.2
...
1
Zuckerrüben zur Zuckergewinnun
1976
D5/N
48
112
132
20
250
308
342.0
0.8
-0.2
0.9
5.6
11.5
15.7
18.3
16.8
13.5
9.2
...
2
Zuckerrüben zur Zuckergewinnun
1976
D4/N
36
118
137
19
185
306
279.0
0.8
-0.2
0.9
5.6
11.5
15.7
18.3
16.8
13.5
9.2
...
3
Zuckerrüben zur Zuckergewinnun
1977
D5/N
48
119
134
15
233
308
320.0
0.0
1.6
4.9
5.6
11.0
15.0
16.1
16.0
12.7
10.6
...
4
Zuckerrüben zur Zuckergewinnun
1977
D4/N
36
128
142
14
185
301
285.0
0.0
1.6
4.9
5.6
11.0
15.0
16.1
16.0
12.7
10.6
...
5
Zuckerrüben zur Zuckergewinnun
1977
D4/N
36
134
144
10
262
305
325.0
0.0
1.6
4.9
5.6
11.0
15.0
16.1
16.0
12.7
10.6
...
6
Zuckerrüben zur Zuckergewinnun
1978
D5/N
48
110
136
26
240
307
264.0
1.5
-0.7
3.8
4.5
10.8
15.2
15.3
16.3
12.3
10.5
...
7
Zuckerrüben zur Zuckergewinnun
1978
D5/N
48
105
135
30
240
293
320.0
1.5
-0.7
3.8
4.5
10.8
15.2
15.3
16.3
12.3
10.5
...
8
Zuckerrüben zur Zuckergewinnun
1978
D5/N
48
118
137
19
220
306
259.0
1.5
-0.7
3.8
4.5
10.8
15.2
15.3
16.3
12.3
10.5
...
9
Zuckerrüben zur Zuckergewinnun
1979
D5/N
48
134
140
6
257
283
247.0
-3.4
-2.9
2.3
5.7
12.2
15.4
14.5
15.6
13.6
8.2
...
10
Zuckerrüben zur Zuckergewinnun
1979
D4/N
36
110
128
18
226
319
286.0
-3.4
-2.9
2.3
5.7
12.2
15.4
14.5
15.6
13.6
8.2
...
11
Zuckerrüben zur Zuckergewinnun
1979
D4/N
36
131
139
8
322
298
302.0
-3.4
-2.9
2.3
5.7
12.2
15.4
14.5
15.6
13.6
8.2
...
12
Zuckerrüben zur Zuckergewinnun
1980
D5/N
48
125
139
14
150
287
280.0
-3.0
0.1
1.5
5.6
9.1
14.5
15.8
16.0
14.2
9.1
...
13
Zuckerrüben zur Zuckergewinnun
1980
D4/N
36
113
135
22
159
314
240.0
-3.0
0.1
1.5
5.6
9.1
14.5
15.8
16.0
14.2
9.1
...
14
Zuckerrüben zur Zuckergewinnun
1980
D5/N
48
126
138
12
235
340
180.0
-3.0
0.1
1.5
5.6
9.1
14.5
15.8
16.0
14.2
9.1
...
15
Zuckerrüben zur Zuckergewinnun
1981
D5/N
48
105
129
24
223
300
335.0
0.2
1.2
5.0
6.2
13.5
14.7
16.5
16.3
14.1
8.4
...
16
Zuckerrüben zur Zuckergewinnun
1981
D4/N
36
114
132
18
232
318
303.7
0.2
1.2
5.0
6.2
13.5
14.7
16.5
16.3
14.1
8.4
...
17
Zuckerrüben zur Zuckergewinnun
1981
D5/N
48
114
131
17
218
289
335.0
0.2
1.2
5.0
6.2
13.5
14.7
16.5
16.3
14.1
8.4
...
18
Zuckerrüben zur Zuckergewinnun
1982
D5/N
48
109
129
20
180
274
225.0
-2.5
0.0
4.0
6.7
11.3
15.2
18.0
17.7
15.3
10.4
...
19
Zuckerrüben zur Zuckergewinnun
1982
D4/N
36
105
129
24
277
301
350.0
-2.5
0.0
4.0
6.7
11.3
15.2
18.0
17.7
15.3
10.4
...
20
Zuckerrüben zur Zuckergewinnun
1982
D5/N
48
106
129
23
226
293
230.0
-2.5
0.0
4.0
6.7
11.3
15.2
18.0
17.7
15.3
10.4
...
21
Zuckerrüben zur Zuckergewinnun
1983
D4/N
36
143
154
11
121
293
228.0
5.0
0.3
4.2
7.2
11.5
15.4
18.7
18.0
14.4
10.2
...
22
Zuckerrüben zur Zuckergewinnun
1983
D5/N
48
127
138
11
177
302
194.1
5.0
0.3
4.2
7.2
11.5
15.4
18.7
18.0
14.4
10.2
...
23
Zuckerrüben zur Zuckergewinnun
1983
D5/N
48
159
171
12
100
297
108.4
5.0
0.3
4.2
7.2
11.5
15.4
18.7
18.0
14.4
10.2
...
24
Zuckerrüben zur Zuckergewinnun
1984
D4/N
36
103
123
20
150
282
350.0
1.9
0.5
1.9
6.6
10.7
13.7
15.8
17.3
12.9
11.2
...
25
Zuckerrüben zur Zuckergewinnun
1984
D5/N
48
105
125
20
182
298
265.9
1.9
0.5
1.9
6.6
10.7
13.7
15.8
17.3
12.9
11.2
...
26
Zuckerrüben zur Zuckergewinnun
1984
D5/N
48
105
125
20
194
326
280.0
1.9
0.5
1.9
6.6
10.7
13.7
15.8
17.3
12.9
11.2
...
27
Zuckerrüben zur Zuckergewinnun
1985
D5/N
48
128
140
12
97
298
380.0
-4.2
-3.3
2.2
6.4
10.6
13.8
17.3
16.5
13.2
10.5
...
28
Zuckerrüben zur Zuckergewinnun
1985
D5/N
48
129
141
12
97
298
325.0
-4.2
-3.3
2.2
6.4
10.6
13.8
17.3
16.5
13.2
10.5
...
29
Zuckerrüben zur Zuckergewinnun
1985
D5/N
48
127
140
13
174
304
330.0
-4.2
-3.3
2.2
6.4
10.6
13.8
17.3
16.5
13.2
10.5
...
30
Zuckerrüben zur Zuckergewinnun
1986
D5/N
48
120
134
14
266
317
417.0
0.2
-4.2
2.7
4.6
13.1
15.0
17.3
15.9
11.7
10.1
...
31
Zuckerrüben zur Zuckergewinnun
1986
D5/N
48
119
134
15
187
302
381.0
0.2
-4.2
2.7
4.6
13.1
15.0
17.3
15.9
11.7
10.1
...
32
Zuckerrüben zur Zuckergewinnun
1986
D5/N
48
122
136
14
213
300
393.0
0.2
-4.2
2.7
4.6
13.1
15.0
17.3
15.9
11.7
10.1
...
33
Zuckerrüben zur Zuckergewinnun
1987
D5/N
48
117
138
21
183
309
334.4
-4.9
-0.2
-0.4
7.3
9.5
13.2
16.0
15.5
13.6
9.7
...
34
Zuckerrüben zur Zuckergewinnun
1987
D5/N
48
118
136
18
151
296
448.6
-4.9
-0.2
-0.4
7.3
9.5
13.2
16.0
15.5
13.6
9.7
...
35
Zuckerrüben zur Zuckergewinnun
1987
D5/N
48
118
136
18
80
309
268.0
-4.9
-0.2
-0.4
7.3
9.5
13.2
16.0
15.5
13.6
9.7
...
36
Zuckerrüben zur Zuckergewinnun
1988
D4/N
36
110
129
19
198
298
282.8
4.0
3.0
2.9
7.0
13.2
15.2
17.5
16.6
14.2
9.6
...
37
Zuckerrüben zur Zuckergewinnun
1988
D5/N
48
110
129
19
198
308
330.0
4.0
3.0
2.9
7.0
13.2
15.2
17.5
16.6
14.2
9.6
...
38
Zuckerrüben zur Zuckergewinnun
1988
D4/N
36
109
127
18
163
283
406.9
4.0
3.0
2.9
7.0
13.2
15.2
17.5
16.6
14.2
9.6
...
39
Zuckerrüben zur Zuckergewinnun
1989
D5/N
48
101
115
14
129
310
383.5
4.3
4.8
6.6
7.5
12.6
15.5
17.8
16.9
15.5
11.4
...
40
Zuckerrüben zur Zuckergewinnun
1989
D4/N
36
102
117
15
197
289
348.0
4.3
4.8
6.6
7.5
12.6
15.5
17.8
16.9
15.5
11.4
...
41
Zuckerrüben zur Zuckergewinnun
1989
D5/N
48
89
102
13
206
282
425.4
4.3
4.8
6.6
7.5
12.6
15.5
17.8
16.9
15.5
11.4
...
42
Zuckerrüben zur Zuckergewinnun
1976
D5/N
48
114
134
20
220
306
313.1
0.8
-0.2
0.9
5.6
11.5
15.7
18.3
16.8
13.5
9.2
...
43
Zuckerrüben zur Zuckergewinnun
1976
D5/N
48
110
130
20
182
288
302.0
0.8
-0.2
0.9
5.6
11.5
15.7
18.3
16.8
13.5
9.2
...
44
Zuckerrüben zur Zuckergewinnun
1977
D4/N
36
125
140
15
200
342
254.0
0.0
1.6
4.9
5.6
11.0
15.0
16.1
16.0
12.7
10.6
...
45
Zuckerrüben zur Zuckergewinnun
1977
D5/N
48
125
140
15
185
339
320.0
0.0
1.6
4.9
5.6
11.0
15.0
16.1
16.0
12.7
10.6
...
46
Zuckerrüben zur Zuckergewinnun
1977
D5/N
48
128
142
14
200
291
320.0
0.0
1.6
4.9
5.6
11.0
15.0
16.1
16.0
12.7
10.6
...
47
Zuckerrüben zur Zuckergewinnun
1978
D4/N
36
125
135
10
155
338
280.0
1.5
-0.7
3.8
4.5
10.8
15.2
15.3
16.3
12.3
10.5
...
48
Zuckerrüben zur Zuckergewinnun
1978
D4/N
36
119
137
18
210
311
340.0
1.5
-0.7
3.8
4.5
10.8
15.2
15.3
16.3
12.3
10.5
...
49
Zuckerrüben zur Zuckergewinnun
1978
D4/N
36
115
135
20
210
301
310.0
1.5
-0.7
3.8
4.5
10.8
15.2
15.3
16.3
12.3
10.5
...
50
Zuckerrüben zur Zuckergewinnun
1979
D6/N
52
137
144
7
208
319
256.0
-3.4
-2.9
2.3
5.7
12.2
15.4
14.5
15.6
13.6
8.2
...
51
Zuckerrüben zur Zuckergewinnun
1979
D5/N
48
130
140
10
246
292
307.5
-3.4
-2.9
2.3
5.7
12.2
15.4
14.5
15.6
13.6
8.2
...
52
Zuckerrüben zur Zuckergewinnun
1979
D5/N
48
130
139
9
200
273
250.0
-3.4
-2.9
2.3
5.7
12.2
15.4
14.5
15.6
13.6
8.2
...
53
Zuckerrüben zur Zuckergewinnun
1980
D6/N
52
118
136
18
193
338
260.0
-3.0
0.1
1.5
5.6
9.1
14.5
15.8
16.0
14.2
9.1
...
54
Zuckerrüben zur Zuckergewinnun
1980
D5/N
48
133
143
10
180
344
210.0
-3.0
0.1
1.5
5.6
9.1
14.5
15.8
16.0
14.2
9.1
...
55
Zuckerrüben zur Zuckergewinnun
1980
D5/N
48
110
128
18
198
293
210.0
-3.0
0.1
1.5
5.6
9.1
14.5
15.8
16.0
14.2
9.1
...
56
Zuckerrüben zur Zuckergewinnun
1981
D5/N
48
112
125
13
211
300
320.0
0.2
1.2
5.0
6.2
13.5
14.7
16.5
16.3
14.1
8.4
...
57
Zuckerrüben zur Zuckergewinnun
1982
D4/N
36
117
135
18
219
275
240.0
-2.5
0.0
4.0
6.7
11.3
15.2
18.0
17.7
15.3
10.4
...
58
Zuckerrüben zur Zuckergewinnun
1982
D5/N
48
105
124
19
180
278
322.7
-2.5
0.0
4.0
6.7
11.3
15.2
18.0
17.7
15.3
10.4
...
59
Zuckerrüben zur Zuckergewinnun
1982
D5/N
48
112
130
18
205
305
320.0
-2.5
0.0
4.0
6.7
11.3
15.2
18.0
17.7
15.3
10.4
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
4474 rows × 36 columns
In [7]:
df['ERTRAG (dt/ha)']-=(2.867*(df['ERNTEJAHR']-1976)) # 2.867 with range correction
#df['ERTRAG (dt/ha)']-=(1.96*(df['ERNTEJAHR']-1976)) # 1.96 without range correction
In [8]:
# df['ERTRAG (dt/ha)']-=(0.752*(df['TAG_AUSSAAT']-75)) # remove the sowing date
In [9]:
Y=np.array(df['ERTRAG (dt/ha)'])
print np.mean(Y), np.std(Y)
286.104324105 92.0581543132
In [10]:
X=np.array([df['Mittlere Ackerzahl'],df['LT_6']+df['LT_7']+df['LT_8']+df['LT_9'],df['NI_6']+df['NI_7']+df['NI_8']+df['NI_9']])
In [11]:
X=X.T
print X.shape
(4551, 3)
In [12]:
index=np.arange(X.shape[0])
s=np.random.permutation(index)
sample=1000
TX=X[s[0:sample],:]
TY=Y[s[0:sample]]
print TX[0:10,:]
print TY[0:10]
print TX.shape
[[ 59. 63.5 198.1]
[ 75. 64.8 316.5]
[ 66. 72.6 97.1]
[ 59. 62. 258.4]
[ 36. 64. 121.6]
[ 56. 64.9 109.9]
[ 59. 65. 292. ]
[ 56. 69.6 134.1]
[ 48. 61.1 456.1]
[ 36. 57.3 325.2]]
[ 341.33 245.665 402.798 378.532 300. 250. 426.665 272.729
228.532 246.266]
(1000, 3)
In [13]:
f1.set_trainX(TX)
f1.set_trainY(TY)
In [14]:
print f1.get_rmse(), f1.get_mae()
103.95224762 81.0153884888
In [15]:
for i in range(10):
print TX[i,:], TY[i],f1.calc3(TX[i,0],TX[i,1],TX[i,2])
[ 59. 63.5 198.1] 341.33 352.639312744
[ 75. 64.8 316.5] 245.665 405.498046875
[ 66. 72.6 97.1] 402.798 231.399993896
[ 59. 62. 258.4] 378.532 406.107757568
[ 36. 64. 121.6] 300.0 180.92868042
[ 56. 64.9 109.9] 250.0 253.781158447
[ 59. 65. 292.] 426.665 382.461364746
[ 56. 69.6 134.1] 272.729 240.621734619
[ 48. 61.1 456.1] 228.532 427.777801514
[ 36. 57.3 325.2] 246.266 404.338928223
In [16]:
Fuzz.start_training(f1)
('minout:', [-100.0, 0.0, 100.0, 200.0, 300.0, 400.0, 500.0])
('maxout:', [100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0])
('start:', [0.0, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0])
*************Result of Optimization*****************
('max:', 0.8666601181030273)
('parameter:', array([ 66.66666667, 166.66666667, 266.66666667, 300. ,
333.33333333, 433.33333333, 533.33333333]))
In [17]:
for i in range(10):
print TX[i,:], TY[i],f1.calc3(TX[i,0],TX[i,1],TX[i,2])
[ 59. 63.5 198.1] 341.33 317.52166748
[ 75. 64.8 316.5] 245.665 355.260894775
[ 66. 72.6 97.1] 402.798 277.133300781
[ 59. 62. 258.4] 378.532 352.854309082
[ 36. 64. 121.6] 300.0 227.805404663
[ 56. 64.9 109.9] 250.0 277.457244873
[ 59. 65. 292.] 426.665 342.831237793
[ 56. 69.6 134.1] 272.729 273.609649658
[ 48. 61.1 456.1] 228.532 366.931243896
[ 36. 57.3 325.2] 246.266 360.504974365
In [18]:
print f1.get_rmse(), f1.get_mae()
86.8241958618 64.9915924072
In [25]:
f1.store_model('zr_simple_t.fis')
Out[25]:
True
In [19]:
Content source: Ralf3/samt2
Similar notebooks: