-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
279 lines (213 loc) · 8.71 KB
/
models.py
File metadata and controls
279 lines (213 loc) · 8.71 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
from sklearn.model_selection import TimeSeriesSplit
from sklearn.decomposition import PCA
from sklearn.model_selection import GridSearchCV
from sklearn import svm
from sklearn.tree import DecisionTreeRegressor
from sklearn.feature_selection import RFE
from sklearn.feature_selection import RFECV
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.preprocessing import StandardScaler
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neural_network import MLPRegressor
import numpy as np
import pandas as pd
def DTR_model(X, y):
""" Performs grid search over the 'max_depth' parameter for a
decision tree regressor trained on the input data [X, y]. """
# Create cross-validation sets from the training data
cv_sets = TimeSeriesSplit(n_splits = 10)
cv_sets.split(X,y)
# Create a decision tree regressor object
regressor = DecisionTreeRegressor()
# Create a dictionary for the parameter 'max_depth' with a range from 1 to 100
params = {'max_depth':range(5,60), 'max_features':['auto']}
# Transform 'performance_metric' into a scoring function using 'make_scorer'
#scoring_fnc = make_scorer(performance_metric)
# Create the grid search object
grid = GridSearchCV(regressor, params, cv=cv_sets)
# Fit the grid search object to the data to compute the optimal model
grid = grid.fit(X, y)
# Return the optimal model after fitting the data
return grid.best_estimator_
def RFR_model(X, y):
""" Performs grid search over the 'n_estimators' parameter for a
random forest regressor trained on the input data [X, y]. """
# Create cross-validation sets from the training data
cv_sets = TimeSeriesSplit(n_splits = 10)
cv_sets.split(X,y)
# Create a decision tree regressor object
regressor = RandomForestRegressor()
# Create a dictionary for the parameter 'max_depth' with a range from 1 to 100
params = {'n_estimators':range(1,len(X.columns))}
# Transform 'performance_metric' into a scoring function using 'make_scorer'
#scoring_fnc = make_scorer(performance_metric)
# Create the grid search object
grid = GridSearchCV(regressor, params, cv=cv_sets)
# Fit the grid search object to the data to compute the optimal model
grid = grid.fit(X, y)
# Return the optimal model after fitting the data
return grid.best_estimator_
def SGD_model(X, y):
#scaler = StandardScaler()
#names = X_names
# Create cross-validation sets from the training data
cv_sets = TimeSeriesSplit(n_splits = 10)
cv_sets.split(X,y)
# Create a decision tree regressor object
regressor = linear_model.SGDRegressor(n_iter=600, random_state = 749)
# Create a dictionary for the parameter 'max_depth' with a range from 1 to 100
params = {'loss':['squared_loss','huber'],'penalty':['none','l2','l1'],'alpha':[.0001,.001,.01,.1],'l1_ratio':[.15,.30,.5,.65]}
# Transform 'performance_metric' into a scoring function using 'make_scorer'
#scoring_fnc = make_scorer(performance_metric)
# Create the grid search object
grid = GridSearchCV(regressor, params, cv=cv_sets)
# Fit the grid search object to the data to compute the optimal model
grid = grid.fit(X, y)
# Return the optimal model after fitting the data
return grid.best_estimator_
def LASSO_model2(X,y):
cv_sets = TimeSeriesSplit(n_splits = 10)
cv_sets.split(X,y)
regressor = Lasso()
params = {'alpha':[.3,.4,.5]}
grid = GridSearchCV(regressor, params, cv=cv_sets)
grid = grid.fit(X, y)
# Return the optimal model after fitting the data
return grid.best_estimator_
def ENSEMBLE_model(X,y):
regressor = LinearRegression()
return regressor.fit(X,y)
def ENSEMBLE_model_svr(X,y):
regressor = SVR(kernel = 'rbf')
return regressor.fit(X,y)
def ENSEMBLE_model_knr(X,y):
regressor = KNeighborsRegressor(n_neighbors=3)
return regressor.fit(X,y)
def ENSEMBLE_model_NN(X,y):
regressor = MLPRegressor(random_state = 749, max_iter = 500)
return regressor.fit(X,y)
def LASSO_model(X,y):
n = 3
names = list(X.columns)
#use linear regression as the model
lr = LinearRegression()
#scaler = StandardScaler()
#rank all features, i.e continue the elimination until the last one
rfe = RFE(lr, n_features_to_select=1)
rfe.fit(X,y)
X_train_top = sorted(zip(map(lambda x: round(x, 4), rfe.ranking_), names))[:n]
X_names = ['Day_N']
for i in range(n):
X_names = X_names+[X_train_top[i][1]]
names = X_names
X_few = X[names]
lasso = Lasso(alpha=.3)
lasso_model = lasso.fit(X_few, y)
return lasso_model, names
def SVR_model(X, y):
# Create cross-validation sets from the training data
cv_sets = TimeSeriesSplit(n_splits = 10)
cv_sets.split(X,y)
# Create a decision tree regressor object
regressor = svm.SVR()
# Create a dictionary for the parameters
params = {'kernel':['rbf'],
'C':range(20,30),
'epsilon':[.2,.3,.4]}
# Create the grid search object
grid = GridSearchCV(regressor, params, cv=cv_sets)
# Fit the grid search object to the data to compute the optimal model
grid = grid.fit(X, y)
# Return the optimal model after fitting the data
return grid.best_estimator_
def KNR_model(X, y):
# Create cross-validation sets from the training data
cv_sets = TimeSeriesSplit(n_splits = 10)
cv_sets.split(X,y)
# Create a regressor object
regressor = KNeighborsRegressor()
# Create a dictionary for the parameters
params = {'n_neighbors':range(1,5),
'weights':['uniform','distance']}
# Create the grid search object
grid = GridSearchCV(regressor, params, cv=cv_sets)
# Fit the grid search object to the data to compute the optimal model
grid = grid.fit(X, y)
# Return the optimal model after fitting the data
return grid.best_estimator_
def benchmark_model(X, y):
# Create a decision tree regressor object
regressor = linear_model.LinearRegression()
# Fit the benchmark model to the data
benchmark = regressor.fit(X, y)
return benchmark
def choose_components(X):
components = range(1,10)
scores = []
for n in components:
pca = PCA(n_components=n)
fit = pca.fit(X)
# summarize components
#print("Explained Variance: %s") % fit.explained_variance_ratio_
scores.append(sum(fit.explained_variance_ratio_))
values = [scores[0]]
for i in range(1,len(scores)):
additional_value = (scores[i]-scores[i-1])
if additional_value <.01:
n_components = i
break
values.append(additional_value)
return n_components
def feature_ranking(X,y):
names = list(X.columns)
#use linear regression as the model
lr = LinearRegression()
cv_sets = TimeSeriesSplit(n_splits = 10)
#scaler = StandardScaler()
#rank all features, i.e continue the elimination until the last one
rfecv = RFECV(lr, cv = cv_sets)
rfecv.fit(X,y)
feature_ranks = sorted(zip(map(lambda x: round(x, 4), rfecv.ranking_), names))
feature_ranks_df = pd.DataFrame(feature_ranks)
top_features = list(feature_ranks_df[1])
return top_features[:21]
def pretty_print_linear(coefs, names = None, sort = False):
if names == None:
names = ["X%s" % x for x in range(len(coefs))]
lst = zip(coefs, names)
if sort:
lst = sorted(lst, key = lambda x:-np.abs(x[0]))
return " + ".join("%s * %s" % (round(coef, 4), name) for coef, name in lst)
def visualize_data():
## code from https://pythonprogramming.net/stock-price-correlation-table-python-programming-for-finance/
## after going through his videos for ideas, I found that this was a useful visualization
import matplotlib.pyplot as plt
df = pd.read_csv('sp500_joined_closes.csv')
#df['AAPL'].plot()
#plt.show()
df_corr = df.corr()
#print(df_corr.head())
df_corr.to_csv('sp500corr.csv')
data1 = df_corr.values
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
heatmap1 = ax1.pcolor(data1, cmap=plt.cm.RdYlGn)
fig1.colorbar(heatmap1)
ax1.set_xticks(np.arange(data1.shape[1]) + 0.5, minor=False)
ax1.set_yticks(np.arange(data1.shape[0]) + 0.5, minor=False)
ax1.invert_yaxis()
ax1.xaxis.tick_top()
column_labels = df_corr.columns
row_labels = df_corr.index
ax1.set_xticklabels(column_labels)
ax1.set_yticklabels(row_labels)
plt.xticks(rotation=90)
heatmap1.set_clim(-1,1)
plt.tight_layout()
#plt.savefig("correlations.png", dpi = (300))
plt.show()
#visualize_data()