-
Notifications
You must be signed in to change notification settings - Fork 2
/
reg_learning.py
234 lines (198 loc) · 9.84 KB
/
reg_learning.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
#================================================================================
#
# Class for creating random forest regressor learning/validation curves and grid search
#
#================================================================================
## General imports
import pandas as pd
import matplotlib.pyplot as plt
import numpy as N
## Ressources
import multiprocessing
import gc
## Sklearn
from sklearn.ensemble import RandomForestRegressor
from sklearn.learning_curve import learning_curve
from sklearn.learning_curve import validation_curve
from sklearn import grid_search
from rf2steps import RandomForestModel
class reg_learning(RandomForestModel):
"""
Class that will contain learning
"""
def learn_curve(self, col2fit, score='r2', maxdepth=8, nestimators=40, verbose=0):
"""
Plots the learning curve over raining data
"""
self.prepare_data(self.df_full, True, col2fit)
print 'Out of {} rows...'.format(self.df_full.shape[0])
##Drop the rows where it did not rain
self.df_full = self.df_full[self.df_full['rain'] >0]
print '...{} have rain and will be used for training'.format(self.df_full.shape[0])
train_values = self.df_full[col2fit].values
target_values = self.df_full['Expected'].values
##Create a list of nsize incresing #-of-sample to train on
nsizes = 10
train_sizes = [x / float(nsizes) for x in range(1, nsizes + 1)]
## Number of cpu to use
## Making sure there is one free unless there is only one
#njobs = max(1, int(multiprocessing.cpu_count()/2))
#njobs = max(1, int(0.75*multiprocessing.cpu_count()))
njobs = max(1, multiprocessing.cpu_count()-1)
print '\n\nlearning with njobs = {}\n...\n'.format(njobs)
train_sizes, train_scores, test_scores = learning_curve(RandomForestRegressor(n_estimators=nestimators, max_depth=maxdepth, verbose=verbose), train_values, target_values, cv=10, n_jobs=njobs, train_sizes=train_sizes, scoring=score)
## Plotting
fig = plt.figure()
plt.xlabel("Training examples")
plt.ylabel(score)
plt.title("Learning Curves (RandomForest n_estimators={0}, max_depth={1})".format(nestimators, maxdepth))
train_scores_mean = N.mean(train_scores, axis=1)
train_scores_std = N.std(train_scores, axis=1)
test_scores_mean = N.mean(test_scores, axis=1)
test_scores_std = N.std(test_scores, axis=1)
plt.grid()
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="r")
plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
label="Training score")
plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.legend(loc="best")
print 'Done'
fig.show()
#plt.savefig('learningcurve.png')
#print train_scores_mean
raw_input('press enter when finished...')
def valid_curve(self, col2fit, score='r2', verbose=0):
"""
Plots the learning curve over raining data
"""
self.prepare_data(self.df_full, True, col2fit)
print 'Out of {} rows...'.format(self.df_full.shape[0])
##Drop the rows where it did not rain
self.df_full = self.df_full[self.df_full['rain'] >0]
print '...{} have rain and will be used for training'.format(self.df_full.shape[0])
train_values = self.df_full[col2fit].values
target_values = self.df_full['Expected'].values
#paramater4validation = "max_depth"
#nestimators = 150
#param_range = [8, 10, 12, 14, 15, 16, 17, 18, 20, 24]
paramater4validation = "n_estimators"
maxdepth = 12
#param_range = [10, 50, 100, 150, 200, 250, 300, 400, 600, 1000]
#param_range = [40, 80, 100, 150, 200, 250,300,400,500,600,800,1000,1400]
param_range = [30, 50, 80, 120, 160, 200, 250, 300, 400, 600, 800, 1000, 1400]
print '\nValidating on {} with ranges:'.format(paramater4validation)
print param_range
njobs = max(2, multiprocessing.cpu_count()-1)
print '\n\nUsing with njobs = {}\n...\n'.format(njobs)
ncrossval = 10
#ncrossval = 5
print 'validating with {} cross validations...'.format(ncrossval)
train_scores, test_scores = validation_curve(
RandomForestRegressor(max_depth = maxdepth), train_values, target_values,
param_name=paramater4validation, param_range=param_range,cv=ncrossval,
scoring=score, verbose = verbose, n_jobs=njobs)
## plotting
train_scores_mean = N.mean(train_scores, axis=1)
train_scores_std = N.std(train_scores, axis=1)
test_scores_mean = N.mean(test_scores, axis=1)
test_scores_std = N.std(test_scores, axis=1)
fig = plt.figure()
plt.title("Validation Curve")
plt.xlabel(paramater4validation)
plt.ylabel(score)
plt.plot(param_range, train_scores_mean, label="Training score", color="r")
plt.fill_between(param_range, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.2, color="r")
plt.plot(param_range, test_scores_mean, label="Cross-validation score",
color="g")
plt.fill_between(param_range, test_scores_mean - test_scores_std,
test_scores_mean + test_scores_std, alpha=0.2, color="g")
plt.grid()
plt.legend(loc='best')
fig.show()
raw_input('press enter when finished...')
def grid_search(self, col2fit, score='r2'):
"""
Using grid search to find the best parameters
"""
#max_depths = [10,12,14,16,20,24,32,40,50]
#nestimators = [20, 50, 100, 150, 200, 250, 300, 400, 500]
max_depths = [10,11,12,13,14,15,16]
nestimators = [30, 50, 100, 150, 200, 250, 300, 400, 600, 1000]
parameters = {'max_depth': max_depths, 'n_estimators' : nestimators}
self.prepare_data(self.df_full, True, col2fit)
print 'Out of {} rows...'.format(self.df_full.shape[0])
##Drop the rows where it did not rain
self.df_full = self.df_full[self.df_full['rain'] >0]
print '...{} have rain and will be used for training'.format(self.df_full.shape[0])
train_values = self.df_full[col2fit].values
target_values = self.df_full['Expected'].values
njobs = max(1, int(multiprocessing.cpu_count() -1))
## Fit the grid
print 'fitting the grid with njobs = {}...'.format(njobs)
rf_grid = grid_search.GridSearchCV(RandomForestRegressor(),
parameters, scoring=score, verbose=2,n_jobs=njobs, cv=5)
rf_grid.fit(train_values, target_values)
## Get score
score_dict = rf_grid.grid_scores_
scores = [x[1] for x in score_dict]
scores = N.array(scores).reshape(len(max_depths), len(nestimators))
## Plot
fig = plt.figure()
plt.imshow(scores, interpolation='nearest', cmap=plt.cm.spectral)
plt.colorbar()
plt.ylabel('max_depths')
plt.yticks(N.arange(len(max_depths)), max_depths)
plt.xlabel('n_estimators')
plt.xticks(N.arange(len(nestimators)), nestimators)
plt.gca().invert_yaxis()
fig.show()
print '\n\n----------------------'
my_score_dic = {}
print score_dict
for iscore in score_dict:
#print iscore[1]
#print iscore[2]
my_score_dic[iscore[1]] = {'params': iscore[0], 'std': iscore[2].std()}
mean_score_list = my_score_dic.keys()
mean_score_list.sort()
for imeanscore in mean_score_list:
print '{}+-{}: md={}, ne={}'.format(round(imeanscore,2),
round(my_score_dic[imeanscore]['std'],3),
my_score_dic[imeanscore]['params']['max_depth'],
my_score_dic[imeanscore]['params']['n_estimators'])
print '\n----------------------'
print 'best parameters:'
print rf_grid.best_params_
print
print rf_grid.best_score_
raw_input('press enter to finished...')
if __name__=='__main__':
lrn = reg_learning('Data/train_2013.csv', 'all')
#lrn = reg_learning('Data/train_2013.csv', 700000)
coltofit = ['Avg_Reflectivity', 'Range_Reflectivity', 'Nval',
'Avg_DistanceToRadar', 'Avg_RadarQualityIndex', 'Range_RadarQualityIndex',
'Avg_RR1', 'Range_RR1','Avg_RR2', 'Range_RR2',
'Avg_RR3', 'Range_RR3', 'Avg_Zdr', 'Range_Zdr',
'Avg_Composite', 'Range_Composite','Avg_HybridScan', 'Range_HybridScan',
'Avg_Velocity', 'Range_Velocity', 'Avg_LogWaterVolume', 'Range_LogWaterVolume',
'Avg_MassWeightedMean', 'Range_MassWeightedMean',
'Avg_MassWeightedSD', 'Range_MassWeightedSD', 'Avg_RhoHV', 'Range_RhoHV'
]
#coltofit = ['Avg_Reflectivity', 'Range_Reflectivity', 'Nval',
# 'Avg_DistanceToRadar', 'Avg_RadarQualityIndex', 'Range_RadarQualityIndex',
# 'Avg_RR1', 'Range_RR1','Avg_RR2', 'Range_RR2',
# 'Avg_RR3', 'Range_RR3','Avg_Zdr','Range_Zdr'
# ]
#coltofit = ['Avg_Reflectivity', 'Range_Reflectivity', 'Nval',
# 'Avg_DistanceToRadar', 'Avg_RadarQualityIndex', 'Range_RadarQualityIndex',
# 'Range_RR1',
# ]
lrn.learn_curve(coltofit, 'r2', 12, 200,1)
#lrn.valid_curve(coltofit, 'r2',2)
#lrn.grid_search(coltofit)