-
Notifications
You must be signed in to change notification settings - Fork 0
/
runClassifierPercentilesInSeason.py
281 lines (223 loc) · 11.5 KB
/
runClassifierPercentilesInSeason.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
2020-10-02
RUN:
runClassifierPercentilesInSeason.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/train1400 \
-t /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1400 -l 30
# Use -n to save predictions:
runClassifierPercentilesInSeason.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/train1400 \
-t /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1400 -l 30 -n
# Use -m to use also meteorological features:
runClassifierPercentilesInSeason.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/train1400 \
-t /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1400 -l 30 -n -m
"""
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
from math import sqrt
import pandas as pd
import numpy as np
import pickle
import os.path
from pathlib import Path
import argparse
import textwrap
# FUNCTIONS:
def load_intensities(filename):
with open(filename, "rb") as f:
data = pickle.load(f)
return data
def classify(trainfile: str, testfile: str, predfile: str, learner: str, important: int, savePred=False):
basename = predfile.split('/')[-1][:-4]
train = pd.read_pickle(trainfile)
test = pd.read_pickle(testfile)
testJune = test.filter(regex='04|05|y|farmID')
testJuly = test.filter(regex='04|05|06|y|farmID')
testAugust = test.filter(regex='04|05|06|07|y|farmID')
trainJune = train.filter(regex='04|05|y|farmID')
trainJuly = train.filter(regex='04|05|06|y|farmID')
trainAugust = train.filter(regex='04|05|06|07|y|farmID')
# June (up to 15.6.)
print('The prediction in June, up until 15.6.')
X_train = trainJune.drop(['y', 'farmID'], axis = 1)
y_train = trainJune['y']
X_test = testJune.drop(['y', 'farmID'], axis = 1)
y_test = testJune['y']
predfileJune = os.path.join(os.path.dirname(predfile), basename + 'June' + '.pkl')
print(f"Shape of training set: {X_train.shape}")
if learner == 'rf':
rf = RandomForestRegressor(n_estimators=500, random_state=42, max_features = 'sqrt')
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
if savePred:
# merge with KUNTA or ELY data:
# check the path to dictionary for regional codes:
dictionary = load_intensities('/Users/myliheik/Documents/myCROPYIELD/data/farmID-elyt-Dict.pkl')
uusi = pd.DataFrame(y_pred)
uusi['farmID'] = test['farmID']
uusi['ELY'] = uusi['farmID'].copy()
for key in dictionary.keys():
uusi['ELY'] = uusi['ELY'].replace(key, dictionary[key])
print(f'Saving predictions on test set into {predfileJune}...\n')
with open(predfileJune, 'wb+') as outputfile:
pickle.dump(uusi, outputfile)
mse = mean_squared_error(y_test, y_pred)
print(f"RMSE: {sqrt(mse)}")
print(f"MSE: {mse}")
print(f"R2: {r2_score(y_test, y_pred)}")
print("Calculating feature importances ... \n")
feature_importances = pd.DataFrame(rf.feature_importances_,
index = X_train.columns,
columns=['importance']).sort_values('importance',
ascending=False)
print(feature_importances[1:important])
# July (up to 15.7.)
print('The prediction in July, up until 15.7.')
X_train = trainJuly.drop(['y', 'farmID'], axis = 1)
y_train = trainJuly['y']
X_test = testJuly.drop(['y', 'farmID'], axis = 1)
y_test = testJuly['y']
predfileJuly = os.path.join(os.path.dirname(predfile), basename + 'July' + '.pkl')
print(f"Shape of training set: {X_train.shape}")
if learner == 'rf':
rf = RandomForestRegressor(n_estimators=500, random_state=42, max_features = 'sqrt')
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
if savePred:
# merge with KUNTA or ELY data:
# check the path to dictionary for regional codes:
dictionary = load_intensities('/Users/myliheik/Documents/myCROPYIELD/data/farmID-elyt-Dict.pkl')
uusi = pd.DataFrame(y_pred)
uusi['farmID'] = test['farmID']
uusi['ELY'] = uusi['farmID'].copy()
for key in dictionary.keys():
uusi['ELY'] = uusi['ELY'].replace(key, dictionary[key])
print(f'Saving predictions on test set into {predfileJuly}...\n')
with open(predfileJuly, 'wb+') as outputfile:
pickle.dump(uusi, outputfile)
mse = mean_squared_error(y_test, y_pred)
print(f"RMSE: {sqrt(mse)}")
print(f"MSE: {mse}")
print(f"R2: {r2_score(y_test, y_pred)}")
print("Calculating feature importances ... \n")
feature_importances = pd.DataFrame(rf.feature_importances_,
index = X_train.columns,
columns=['importance']).sort_values('importance',
ascending=False)
print(feature_importances[1:important])
# August (up to 15.8.)
print('The prediction in August, up until 15.8.')
X_train = trainAugust.drop(['y', 'farmID'], axis = 1)
y_train = trainAugust['y']
X_test = testAugust.drop(['y', 'farmID'], axis = 1)
y_test = testAugust['y']
predfileAugust = os.path.join(os.path.dirname(predfile), basename + 'August' + '.pkl')
print(f"Shape of training set: {X_train.shape}")
if learner == 'rf':
rf = RandomForestRegressor(n_estimators=500, random_state=42, max_features = 'sqrt')
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
if savePred:
# merge with KUNTA or ELY data:
# check the path to dictionary for regional codes:
dictionary = load_intensities('/Users/myliheik/Documents/myCROPYIELD/data/farmID-elyt-Dict.pkl')
uusi = pd.DataFrame(y_pred)
uusi['farmID'] = test['farmID']
uusi['ELY'] = uusi['farmID'].copy()
for key in dictionary.keys():
uusi['ELY'] = uusi['ELY'].replace(key, dictionary[key])
print(f'Saving predictions on test set into {predfileAugust}...\n')
with open(predfileAugust, 'wb+') as outputfile:
pickle.dump(uusi, outputfile)
mse = mean_squared_error(y_test, y_pred)
print(f"RMSE: {sqrt(mse)}")
print(f"MSE: {mse}")
print(f"R2: {r2_score(y_test, y_pred)}")
print("Calculating feature importances ... \n")
feature_importances = pd.DataFrame(rf.feature_importances_,
index = X_train.columns,
columns=['importance']).sort_values('importance',
ascending=False)
print(feature_importances[1:important])
# Final:
print('The final prediction, up until 30.10.')
X_train = train.drop(['y', 'farmID'], axis = 1)
y_train = train['y']
X_test = test.drop(['y', 'farmID'], axis = 1)
y_test = test['y']
print(f"Shape of training set: {X_train.shape}")
if learner == 'rf':
rf = RandomForestRegressor(n_estimators=500, random_state=42, max_features = 'sqrt')
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
if savePred:
# merge with KUNTA or ELY data:
# check the path to dictionary for regional codes:
dictionary = load_intensities('/Users/myliheik/Documents/myCROPYIELD/data/farmID-elyt-Dict.pkl')
uusi = pd.DataFrame(y_pred)
uusi['farmID'] = test['farmID']
uusi['ELY'] = uusi['farmID'].copy()
for key in dictionary.keys():
uusi['ELY'] = uusi['ELY'].replace(key, dictionary[key])
print(f'Saving predictions on test set into {predfile}...\n')
with open(predfile, 'wb+') as outputfile:
pickle.dump(uusi, outputfile)
mse = mean_squared_error(y_test, y_pred)
print(f"RMSE: {sqrt(mse)}")
print(f"MSE: {mse}")
print(f"R2: {r2_score(y_test, y_pred)}")
print("Calculating feature importances ... \n")
feature_importances = pd.DataFrame(rf.feature_importances_,
index = X_train.columns,
columns=['importance']).sort_values('importance',
ascending=False)
print(feature_importances[1:important])
# HERE STARTS MAIN:
def main(args):
try:
if not args.train_dir or not args.test_dir :
raise Exception('Missing train set or test set directory argument. Try --help .')
print(f'\nrunClassifierPercentilesInSeason.py')
print(f'\nARD train set in: {args.train_dir}')
print(f'\nARD test set in: {args.test_dir}')
if args.use_2Dmeteo:
print(f'\nUsing ARD + meteorological features, for 2D only.')
trainfile = os.path.join(args.train_dir, 'ard2DpercentilesMeteo.pkl')
testfile = os.path.join(args.test_dir, 'ard2DpercentilesMeteo.pkl')
predfile = os.path.join(args.test_dir, 'ard2DpercentilesMeteoPreds.pkl')
else:
trainfile = os.path.join(args.train_dir, 'ard2Dpercentiles.pkl')
testfile = os.path.join(args.test_dir, 'ard2Dpercentiles.pkl')
predfile = os.path.join(args.test_dir, 'ard2DpercentilesPreds.pkl')
classify(trainfile, testfile, predfile, learner = 'rf', important = 30, savePred=args.savePreds)
print(f'\nDone.')
except Exception as e:
print('\n\nUnable to read input or write out statistics. Check prerequisites and see exception output below.')
parser.print_help()
raise e
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(__doc__))
parser.add_argument('-i', '--train_dir',
help='Directory for input directory.',
type=str,
default='.')
parser.add_argument('-t', '--test_dir',
help='Directory for target directory.',
type=str,
default='.')
parser.add_argument('-l', '--importance',
help='Number of the most important features to show, default 20.',
type=int,
default=20)
parser.add_argument('-m', '--use_2Dmeteo',
help='Use additional meteorological features.',
default=False,
action='store_true')
parser.add_argument('-n', '--savePreds',
help='Save test set predictions.',
default=False,
action='store_true')
parser.add_argument('--debug',
help='Verbose output for debugging.',
action='store_true')
args = parser.parse_args()
main(args)