-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunClassifierPercentiles.py
149 lines (112 loc) · 5.48 KB
/
runClassifierPercentiles.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
"""
2020-10-02
RUN:
runClassifierPercentiles.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/train1400 \
-t /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1400 -l 30
# Use -n to save predictions:
runClassifierPercentiles.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:
runClassifierPercentiles.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):
train = pd.read_pickle(trainfile)
test = pd.read_pickle(testfile)
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'\nrunClassifierPercentiles.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)