-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForecastingML.py
More file actions
426 lines (397 loc) · 16.7 KB
/
ForecastingML.py
File metadata and controls
426 lines (397 loc) · 16.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
'''
This code finds the best algorithm to forecast whatever numeric data you want.
The algorithms used in the model are linear regression, neural network, support vector regressor, decision tree
and random forest
This model returns the best algorithm into a file with .pkl extension wich you can use then with the joblib library from
sklearn
'''
# Here all the used libraries are imported
from sklearn.linear_model import LinearRegression # linear regression algorithm
from sklearn.model_selection import train_test_split # datasplit algorithm to train your data
from sklearn.preprocessing import StandardScaler, Normalizer # Normalizer
from sklearn.decomposition import PCA # principal component analysis algorithm
from sklearn import tree # decision tree algorithm
from sklearn.ensemble import RandomForestRegressor # random forest algorithm
from sklearn.svm import SVR # support vector algorithm
from sklearn.neural_network import MLPRegressor # neural network algorithm
from sklearn import manifold # isomap algorithm
#With this function we find the best neural network for the given dataset
def bestMLP(X_train,X_test,y_train,y_test,n_components,MaxLayers):
number_Layers=1
best_score=0
best_PCA=0
best_Layers=0
best_Neurons=0
best_model=[] # Empty list for the best model
# Loop for number of layers
for number_Layers in range(1,MaxLayers+1):
#Loop for neurons for each layer
print("Layers: ",number_Layers)
number_Neurons=n_components
for number_Neurons in range(1,number_Layers+1):
print("Neurons: ",number_Neurons)
model2=MLPRegressor(hidden_layer_sizes=(number_Layers,number_Neurons ))
model2.fit(X_train,y_train)
score=model2.score(X_test,y_test)
if score > best_score:
best_score=score
best_PCA=n_components
best_Layers=number_Layers
best_Neurons=number_Neurons
best_model=model2
return best_score,best_PCA,best_Layers,best_Neurons,best_model
#With this function we find the best random forest for the given dataset
def bestRF(X_train,X_test,y_train,y_test,n_components,MaxDepth):
max_depth=1
best_score=0
best_depth=0
best_randomState=0
best_PCA=0
best_model=[] # Empty list for the best model
# Loop for the deep of the trees of the random forest
for max_depth in range(1,MaxDepth+1):
random_state=1
# Loop for values of the random state
for random_state in range(1,11):
model4=RandomForestRegressor(max_depth = max_depth, random_state = random_state)
model4.fit(X_train,y_train)
score=model4.score(X_test,y_test)
if score > best_score:
best_score=score
best_depth=max_depth
best_randomState=random_state
best_PCA=n_components
best_model=model4
return best_score,best_depth,best_randomState,best_PCA,best_model
#With this function we find the best decision tree for the given dataset
def bestDT(X_train,X_test,y_train,y_test,n_components,MaxDepth):
max_depth=1
best_score=0
best_depth=0
best_randomState=0
best_PCA=0
best_model=[] # Empty list for the bes model
# Loop for the deep of the trees of the decision tree
for max_depth in range(1,MaxDepth+1):
# Loop for values of the random state
random_state=1
for random_state in range(1,11):
model5=tree.DecisionTreeRegressor(max_depth = max_depth, random_state = random_state)
model5.fit(X_train,y_train)
score=model5.score(X_test,y_test)
if score > best_score:
best_score=score
best_depth=max_depth
best_randomState=random_state
best_PCA=n_components
best_model=model5
return best_score,best_depth,best_randomState,best_PCA,best_model
#This function uses the three functions from above to find the best model for each PCA result
def forecastingModel(Dataset,Label,MLP=False,MaxLayers=100,MaxDepth=10):
best_model_all=[]
best_score_all=0
intial_components=len(Dataset.columns)
#Here you set the PCA and try different values for n_components
n_components=1
best_MLP=0
best_PCA_MLP=0
best_Layers_MLP=0
best_Neurons_MLP=0
best_SVR=0
best_PCA_SVR=0
best_RandomForest=0
max_depth_RF=0
random_state_RF=0
best_PCA_RF=0
best_DecisionTree=0
max_depth_DT=0
random_state_DT=0
best_PCA_DT=0
best_LinearRegression=0
best_PCA_LinearRegression=0
# Without PCA
#Here you split the data
X_train, X_test, y_train, y_test = train_test_split(Dataset, Label, test_size=0.33)
#MLP
if MLP==True:
best_score,best_PCA,best_Layers,best_Neurons,best_model_MLP=bestMLP(X_train,X_test,y_train,y_test,intial_components,MaxLayers)
if best_score>best_MLP:
best_MLP=best_score
best_PCA_MLP=best_PCA
best_Layers_MLP=best_Layers
best_Neurons_MLP=best_Neurons
if best_score>best_score_all:
best_model_all=best_model_MLP
best_score_all=best_score
#SVR
model3=SVR()
model3.fit(X_train,y_train)
score=model3.score(X_test,y_test)
if score > best_SVR:
best_SVR=score
best_PCA_SVR=intial_components
if score > best_score_all:
best_model_all=model3
best_score_all=score
#RandomForestRegressor
best_score,best_depth,best_randomState,best_PCA, best_model_RF=bestRF(X_train,X_test,y_train,y_test,intial_components,MaxDepth)
if best_score>best_RandomForest:
best_RandomForest=best_score
best_PCA_RF=best_PCA
max_depth_RF=best_depth
random_state_RF=best_randomState
if best_score>best_score_all:
best_model_all=best_model_RF
best_score_all=best_score
#DecisionTree
best_score,best_depth,best_randomState,best_PCA, best_model_DT=bestDT(X_train,X_test,y_train,y_test,intial_components,MaxDepth)
if best_score>best_DecisionTree:
best_DecisionTree=best_score
best_PCA_DT=best_PCA
max_depth_DT=best_depth
random_state_DT=best_randomState
if best_score>best_score_all:
best_model_all=best_model_DT
best_score_all=best_score
#LinearRregression
model6=LinearRegression()
model6.fit(X_train,y_train)
score=model6.score(X_test,y_test)
if score > best_LinearRegression:
best_LinearRegression=score
best_PCA_LinearRegression=intial_components
if score > best_score_all:
best_model_all=model6
best_score_all=score
# With PCA
for n_components in range(1,len(Dataset.columns)):
model=PCA(n_components=n_components)
model.fit(Dataset)
T=model.transform(Dataset)
#Here you split the data
X_train, X_test, y_train, y_test = train_test_split(T, Label, test_size=0.33)
#MLP
if MLP==True:
best_score,best_PCA,best_Layers,best_Neurons,best_model_MLP=bestMLP(X_train,X_test,y_train,y_test,n_components,MaxLayers)
if best_score>best_MLP:
best_MLP=best_score
best_PCA_MLP=best_PCA
best_Layers_MLP=best_Layers
best_Neurons_MLP=best_Neurons
if best_score>best_score_all:
best_model_all=best_model_MLP
best_score_all=best_score
#SVR
model3=SVR()
model3.fit(X_train,y_train)
score=model3.score(X_test,y_test)
if score > best_SVR:
best_SVR=score
best_PCA_SVR=n_components
if score > best_score_all:
best_model_all=model3
best_score_all=score
#RandomForestRegressor
best_score,best_depth,best_randomState,best_PCA, best_model_RF=bestRF(X_train,X_test,y_train,y_test,n_components,MaxDepth)
if best_score>best_RandomForest:
best_RandomForest=best_score
best_PCA_RF=best_PCA
max_depth_RF=best_depth
random_state_RF=best_randomState
if best_score>best_score_all:
best_model_all=best_model_RF
best_score_all=best_score
#DecisionTree
best_score,best_depth,best_randomState,best_PCA, best_model_DT=bestDT(X_train,X_test,y_train,y_test,n_components,MaxDepth)
if best_score>best_DecisionTree:
best_DecisionTree=best_score
best_PCA_DT=best_PCA
max_depth_DT=best_depth
random_state_DT=best_randomState
if best_score>best_score_all:
best_model_all=best_model_DT
best_score_all=best_score
#LinearRregression
model6=LinearRegression()
model6.fit(X_train,y_train)
score=model6.score(X_test,y_test)
if score > best_LinearRegression:
best_LinearRegression=score
best_PCA_LinearRegression=n_components
if score > best_score_all:
best_model_all=model6
best_score_all=score
print('MLP:',best_MLP,'with ',best_PCA_MLP,' components, number of hidden layers: ',best_Layers_MLP,' and number of neurons: ',best_Neurons_MLP)
print('SVR:',best_SVR,'with ',best_PCA_SVR,' components')
print('Random forest:',best_RandomForest,' max_depth=',max_depth_RF,' random_state:',random_state_RF,'with ',best_PCA_RF,' components')
print('Decision tree:',best_DecisionTree,' max_depth=',max_depth_DT,' random_state:',random_state_DT,'with ',best_PCA_DT,' components')
print('Linear regression:',best_LinearRegression,'with ',best_PCA_LinearRegression,' components')
print(best_score_all)
return best_model_all,best_score_all
#This function uses the three functions from above to find the best model for each Isomap result
def forecastingModelISO(Dataset,Label,MLP=False,MaxLayers=100,neighbors=20,MaxDepth=10):
best_model_all=[]
best_score_all=0
intial_components=len(Dataset.columns)
#Here you set the Isomap and try different values for n_components
n_components=1
best_MLP=0
best_ISO_MLP=0
best_Layers_MLP=0
best_Neurons_MLP=0
best_neighbor_MLP=0
best_SVR=0
best_ISO_SVR=0
best_neighbor_SVR=0
best_RandomForest=0
max_depth_RF=0
random_state_RF=0
best_ISO_RF=0
best_neighbor_RF=0
best_DecisionTree=0
max_depth_DT=0
random_state_DT=0
best_ISO_DT=0
best_neighbor_DT=0
best_LinearRegression=0
best_ISO_LinearRegression=0
best_neighbor_LinearRregression=0
# Without Isomap
#Here you split the data
X_train, X_test, y_train, y_test = train_test_split(Dataset, Label, test_size=0.33)
#MLP
if MLP==True:
best_score,best_ISO,best_Layers,best_Neurons,best_model_MLP=bestMLP(X_train,X_test,y_train,y_test,intial_components,MaxLayers)
if best_score>best_MLP:
best_MLP=best_score
best_ISO_MLP=best_ISO
best_Layers_MLP=best_Layers
best_Neurons_MLP=best_Neurons
if best_score>best_score_all:
best_model_all=best_model_MLP
best_score_all=best_score
#SVR
model3=SVR()
model3.fit(X_train,y_train)
score=model3.score(X_test,y_test)
if score > best_SVR:
best_SVR=score
best_ISO_SVR=intial_components
if score > best_score_all:
best_model_all=model3
best_score_all=score
#RandomForestRegressor
best_score,best_depth,best_randomState,best_ISO, best_model_RF=bestRF(X_train,X_test,y_train,y_test,intial_components,MaxDepth)
if best_score>best_RandomForest:
best_RandomForest=best_score
best_ISO_RF=best_ISO
max_depth_RF=best_depth
random_state_RF=best_randomState
if best_score>best_score_all:
best_model_all=best_model_RF
best_score_all=best_score
#DecisionTree
best_score,best_depth,best_randomState,best_ISO, best_model_DT=bestDT(X_train,X_test,y_train,y_test,intial_components,MaxDepth)
if best_score>best_DecisionTree:
best_DecisionTree=best_score
best_ISO_DT=best_ISO
max_depth_DT=best_depth
random_state_DT=best_randomState
if best_score>best_score_all:
best_model_all=best_model_DT
best_score_all=best_score
#LinearRregression
model6=LinearRegression()
model6.fit(X_train,y_train)
score=model6.score(X_test,y_test)
if score > best_LinearRegression:
best_LinearRegression=score
best_ISO_LinearRegression=intial_components
if score > best_score_all:
best_model_all=model6
best_score_all=score
# With Isomap
for n_components in range(1,len(Dataset.columns)):
neighbor=1
for neighbor in range(1,neighbors+1):
model=manifold.Isomap(n_neighbors=neighbor, n_components=n_components)
model.fit(Dataset)
T=model.transform(Dataset)
#Here you split the data
X_train, X_test, y_train, y_test = train_test_split(T, Label, test_size=0.33)
#MLP
if MLP==True:
best_score,best_ISO,best_Layers,best_Neurons,best_model_MLP=bestMLP(X_train,X_test,y_train,y_test,n_components,MaxLayers)
if best_score>best_MLP:
best_MLP=best_score
best_ISO_MLP=best_ISO
best_Layers_MLP=best_Layers
best_Neurons_MLP=best_Neurons
best_neighbor_MLP=neighbor
if best_score>best_score_all:
best_model_all=best_model_MLP
best_score_all=best_score
#SVR
model3=SVR()
model3.fit(X_train,y_train)
score=model3.score(X_test,y_test)
if score > best_SVR:
best_SVR=score
best_ISO_SVR=n_components
best_neighbor_SVR=neighbor
if score > best_score_all:
best_model_all=model3
best_score_all=score
#RandomForestRegressor
best_score,best_depth,best_randomState,best_ISO,best_model_RF=bestRF(X_train,X_test,y_train,y_test,n_components,MaxDepth)
if best_score>best_RandomForest:
best_RandomForest=best_score
best_ISO_RF=best_ISO
max_depth_RF=best_depth
random_state_RF=best_randomState
best_neighbor_RF=neighbor
if best_score>best_score_all:
best_model_all=best_model_RF
best_score_all=best_score
#DecisionTree
best_score,best_depth,best_randomState,best_ISO,best_model_DT=bestDT(X_train,X_test,y_train,y_test,n_components,MaxDepth)
if best_score>best_DecisionTree:
best_DecisionTree=best_score
best_ISO_DT=best_ISO
max_depth_DT=best_depth
random_state_DT=best_randomState
best_neighbor_DT=neighbor
if best_score>best_score_all:
best_model_all=best_model_DT
best_score_all=best_score
#LinearRregression
model6=LinearRegression()
model6.fit(X_train,y_train)
score=model6.score(X_test,y_test)
if score > best_LinearRegression:
best_LinearRegression=score
best_ISO_LinearRegression=n_components
best_neighbor_LinearRregression=neighbor
if score>best_score_all:
best_model_all=model6
best_score_all=score
print('MLP:',best_MLP,'with ',best_ISO_MLP,' components, neighbors: ',best_neighbor_MLP,'number of hidden layers: ',best_Layers_MLP,' and number of neurons: ',best_Neurons_MLP)
print('SVR:',best_SVR,'with ',best_ISO_SVR,' components and neighbors: ',best_neighbor_SVR)
print('Random forest:',best_RandomForest,' max_depth=',max_depth_RF,' random_state:',random_state_RF,'with ',best_ISO_RF,' components and neighbors: ',best_neighbor_RF)
print('Decision tree:',best_DecisionTree,' max_depth=',max_depth_DT,' random_state:',random_state_DT,'with ',best_ISO_DT,' components and neighbors: ', best_neighbor_DT)
print('Linear regression:',best_LinearRegression,'with ',best_ISO_LinearRegression,' components and neighbors: ',best_neighbor_LinearRregression)
print(best_score_all)
return best_model_all, best_score_all
# This function uses the two functions from above and return the model with best score
def bestForecastModel(Dataset,label,neighbors=20,MLP=False,MaxLayers=100,MaxDepth=10):
bestModel=[]
bestScore=0
model1,score1=forecastingModel(Dataset,label,MLP,MaxLayers,MaxDepth)
model2,score2=forecastingModelISO(Dataset,label,MLP,MaxLayers,neighbors,MaxDepth)
if score1>score2:
bestModel=model1
bestScore=score1
else:
bestModel=model2
bestScore=score2
return bestModel,bestScore