-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
337 lines (270 loc) · 12.1 KB
/
train.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
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
import yfinance as yf
import datetime
import pandas as pd
import numpy as np
from finta import TA
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import STL
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import confusion_matrix, classification_report
from sklearn import metrics
from sklearn.metrics import accuracy_score
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.svm import SVC
from sklearn.ensemble import AdaBoostClassifier
from sklearn import tree
def precisionSMP(values, prediction):
good = 0
total = 0
if(np.sum(prediction) == 0):
return 0
for it in range(len(values)):
if prediction[it] == 1 and values[it]==1:
good+=1
return good/np.sum(prediction)
def _train_KNN(X_train, y_train, X_test, y_test):
knn = KNeighborsClassifier()
# Create a dictionary of all values we want to test for n_neighbors
params_knn = {'n_neighbors': np.arange(1, 25)}
# Use gridsearch to test all values for n_neighbors
knn_gs = GridSearchCV(knn, params_knn, cv=5)
# Fit model to training data
knn_gs.fit(X_train, y_train)
# Save best model
knn_best = knn_gs.best_estimator_
# Check best n_neigbors value
#print(knn_gs.best_params_)
prediction = knn_best.predict(X_test)
#cm = confusion_matrix(y_test, prediction)
#disp = ConfusionMatrixDisplay(confusion_matrix=cm)
#disp.plot(cmap=plt.cm.Blues)
#plt.title("Confusion Matrix")
#plt.show()
#print(classification_report(y_test, prediction))
#print(confusion_matrix(y_test, prediction))
return knn_best
def train_svm(X_train, y_train, X_test, y_test):
svm = SVC()
# Create a dictionary of all values we want to test for C and gamma
params_svm = {'C': [0.1, 1, 10], 'gamma': [0.1, 1, 'scale', 'auto']}
# Use gridsearch to test all values for C and gamma
svm_gs = GridSearchCV(svm, params_svm, cv=5)
# Fit model to training data
svm_gs.fit(X_train, y_train)
# Save best model
svm_best = svm_gs.best_estimator_
# Check best C and gamma values
#print(svm_gs.best_params_)
prediction = svm_best.predict(X_test)
#cm = confusion_matrix(y_test, prediction)
#disp = ConfusionMatrixDisplay(confusion_matrix=cm)
#disp.plot(cmap=plt.cm.Blues)
#plt.title("Confusion Matrix")
#plt.show()
#print(classification_report(y_test, prediction))
#print(confusion_matrix(y_test, prediction))
return svm_best
def train_rf(X_train, y_train, X_test, y_test):
"""
Function that uses random forest classifier to train the model
:return:
"""
# Create a new random forest classifier
rf = RandomForestClassifier()
# Dictionary of all values we want to test for n_estimators
params_rf = {'n_estimators': [110,130,140,150,160,180,200]}
# Use gridsearch to test all values for n_estimators
rf_gs = GridSearchCV(rf, params_rf, cv=5)
# Fit model to training data
rf_gs.fit(X_train, y_train)
# Save best model
rf_best = rf_gs.best_estimator_
# Check best n_estimators value
#print(rf_gs.best_params_)
prediction = rf_best.predict(X_test)
#cm = confusion_matrix(y_test, prediction)
#disp = ConfusionMatrixDisplay(confusion_matrix=cm)
#disp.plot(cmap=plt.cm.Blues)
#plt.title("Confusion Matrix")
#plt.show()
#print(classification_report(y_test, prediction))
#print(confusion_matrix(y_test, prediction))
return rf_best
def train_dt(X_train, y_train, X_test, y_test):
"""
Function that uses random forest classifier to train the model
:return:
"""
# Create a new random forest classifier
dt = tree.DecisionTreeClassifier()
# Dictionary of all values we want to test for n_estimators
params_grid = {'max_depth':range(1,11), 'min_samples_split':range(1,11)}
# Use gridsearch to test all values for n_estimators
dt_gs = GridSearchCV(dt, params_grid, cv=5)
# Fit model to training data
dt_gs.fit(X_train, y_train)
# Save best model
dt_best = dt_gs.best_estimator_
# Check best n_estimators value
#print(rf_gs.best_params_)
prediction = dt_best.predict(X_test)
#cm = confusion_matrix(y_test, prediction)
#disp = ConfusionMatrixDisplay(confusion_matrix=cm)
#disp.plot(cmap=plt.cm.Blues)
#plt.title("Confusion Matrix")
#plt.show()
#print(classification_report(y_test, prediction))
#print(confusion_matrix(y_test, prediction))
return dt_best
def _ensemble_model(svm_model, dt_model, X_train, y_train, X_test, y_test):
# Create a dictionary of our models
estimators=[('svm', svm_model),('ds', dt_model)]
# Create AdaBoost classifier and fit it to the ensemble model
adaboost = AdaBoostClassifier(n_estimators= 700, random_state=42)
adaboost.fit(X_train, y_train)
# Test our model on the test data
#print(adaboost.score(X_test, y_test))
prediction = adaboost.predict(X_test)
#cm = confusion_matrix(y_test, prediction)
#disp = ConfusionMatrixDisplay(confusion_matrix=cm)
#disp.plot(cmap=plt.cm.Blues)
#plt.title("Confusion Matrix")
#plt.show()
#print(classification_report(y_test, prediction))
#print(confusion_matrix(y_test, prediction))
return adaboost
def cross_Validation_ADA(data):
# Split data into equal partitions of size len_train
num_train = 5 # Increment of how many starting points (len(data) / num_train = number of train-test sets)
len_train = 50 # Length of each train-test set
# Lists to store the results from each model
rf_RESULTS = []
knn_RESULTS = []
svm_RESULTS = []
dt_RESULTS = []
ensemble_RESULTS = []
i = 0
printer = 0
Best_Model = 0
Best_Model_rf = 0
Best_Model_knn = 0
Best_Model_svm = 0
Best_Model_dt = 0
Best_Model_ensemble = 0
Best_precision = 0
Best_precision_rf = 0
Best_precision_knn = 0
Best_precision_svm = 0
Best_precision_dt = 0
Best_precision_ensemble = 0
while True:
# Partition the data into chunks of size len_train every num_train days
df = data.iloc[i * num_train : (i * num_train) + len_train]
i += 1
if len(df) < 25:
break
y = df['pred']
features = [x for x in df.columns if x not in ['pred']]
X = df[features]
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size= 7 * len(X) // 10,shuffle=False)
try:
rf_model = train_rf(X_train, y_train, X_test, y_test)
knn_model = _train_KNN(X_train, y_train, X_test, y_test)
svm_model = train_svm(X_train, y_train, X_test, y_test)
dt_model = train_dt(X_train, y_train, X_test, y_test)
ensemble_model = _ensemble_model(svm_model, dt_model, X_train, y_train, X_test, y_test)
rf_prediction = rf_model.predict(X_test)
knn_prediction = knn_model.predict(X_test)
svm_prediction = svm_model.predict(X_test)
dt_prediction = dt_model.predict(X_test)
ensemble_prediction = ensemble_model.predict(X_test)
rf_precision = precisionSMP(y_test.values, rf_prediction)
knn_precision = precisionSMP(y_test.values, knn_prediction)
svm_precision = precisionSMP(y_test.values, svm_prediction)
dt_precision = precisionSMP(y_test.values, dt_prediction)
ensemble_precision = precisionSMP(y_test.values, ensemble_prediction)
except:
y_train[0] = 0
y_train[1] = 1
rf_model = train_rf(X_train, y_train, X_test, y_test)
knn_model = _train_KNN(X_train, y_train, X_test, y_test)
svm_model = train_svm(X_train, y_train, X_test, y_test)
dt_model = train_dt(X_train, y_train, X_test, y_test)
ensemble_model = _ensemble_model(svm_model, dt_model, X_train, y_train, X_test, y_test)
rf_prediction = rf_model.predict(X_test)
knn_prediction = knn_model.predict(X_test)
svm_prediction = svm_model.predict(X_test)
dt_prediction = dt_model.predict(X_test)
ensemble_prediction = ensemble_model.predict(X_test)
rf_precision = precisionSMP(y_test.values, rf_prediction)
knn_precision = precisionSMP(y_test.values, knn_prediction)
svm_precision = precisionSMP(y_test.values, svm_prediction)
dt_precision = precisionSMP(y_test.values, dt_prediction)
ensemble_precision = precisionSMP(y_test.values, ensemble_prediction)
if Best_precision < rf_precision:
Best_Model = rf_model
Best_precision = rf_precision
if Best_precision < knn_precision:
Best_Model = knn_model
Best_precision = knn_precision
if Best_precision < svm_precision:
Best_Model = svm_model
Best_precision = svm_precision
if Best_precision < dt_precision:
Best_Model = dt_model
Best_precision = dt_precision
if Best_precision < ensemble_precision:
Best_Model = ensemble_model
Best_precision = ensemble_precision
if Best_precision_rf < rf_precision:
Best_Model_rf = rf_model
Best_precision_rf = rf_precision
if Best_precision_knn < knn_precision:
Best_Model_knn = knn_model
Best_precision_knn = knn_precision
if Best_precision_svm < svm_precision:
Best_Model_svm = svm_model
Best_precision_svm = svm_precision
if Best_precision_dt < dt_precision:
Best_Model_dt = dt_model
Best_precision_dt = dt_precision
if Best_precision_ensemble < ensemble_precision:
Best_Model_ensemble = ensemble_model
Best_precision_ensemble = ensemble_precision
#if (printer%10 == 0):
#print(i * num_train, (i * num_train) + len_train)
#print('rf prediction is ', rf_prediction)
#print('knn prediction is ', knn_prediction)
#print('svm prediction is ', svm_prediction)
#print('ensemble prediction is ', ensemble_prediction)
#print('truth values are ', y_test.values)
#print(rf_precision, knn_precision, svm_precision, ensemble_precision)
rf_RESULTS.append(rf_precision)
knn_RESULTS.append(knn_precision)
svm_RESULTS.append(svm_precision)
dt_RESULTS.append(dt_precision)
ensemble_RESULTS.append(ensemble_precision)
printer = printer + 1
print('SVM precision = ' + str( sum(svm_RESULTS) / len(svm_RESULTS)))
print('RF precision = ' + str( sum(rf_RESULTS) / len(rf_RESULTS)))
print('KNN precision = ' + str( sum(knn_RESULTS) / len(knn_RESULTS)))
print('DT precision = ' + str( sum(dt_RESULTS) / len(dt_RESULTS)))
print('Ensemble precision = ' + str( sum(ensemble_RESULTS) / len(ensemble_RESULTS)))
if(Best_precision == Best_precision_ensemble):
return Best_Model_ensemble, Best_precision_ensemble
if(Best_precision == Best_precision_rf):
return Best_Model_rf, Best_precision_rf
if(Best_precision == Best_precision_knn):
return Best_Model_knn, Best_precision_knn
if(Best_precision == Best_precision_svm):
return Best_Model_svm, Best_precision_svm
if(Best_precision == Best_precision_dt):
return Best_Model_dt, Best_precision_dt
return Best_Model, Best_precision