-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools_predict_generic.py
303 lines (162 loc) · 5.87 KB
/
tools_predict_generic.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
import os, sys
try:
import time
except:
os.system('pip install time')
import time
try:
import math
except:
os.system('pip install math')
import math
try:
import csv
except:
os.system("pip3 install csv")
import csv
try:
import requests
except:
os.system('pip install requests')
import requests
try:
import matplotlib as mpl
except:
os.system('pip3 install matplotlib')
import matplotlib as mpl
try:
import matplotlib.pyplot as plt
except:
os.system("pip3 install matplotlib")
import matplotlib.pyplot as plt
from matplotlib import style
try:
import pandas as pd
except:
os.system('pip3 install pandas')
import pandas as pd
try:
import numpy as np
except:
os.system("pip3 install numpy")
import numpy as np
try:
from sklearn.svm import SVR
except:
os.system('pip install sklearn.svm')
from sklearn.svm import SVR
try:
from sklearn.model_selection import train_test_split
except:
os.system('pip install sklearn.model_selection')
from sklearn.model_selection import train_test_split
try:
from sklearn.svm import SVR
except:
os.system('pip install sklearn.svm')
from sklearn.svm import SVR
try:
from sklearn.linear_model import LinearRegression
except:
os.system('pip import sklearn.linear_model')
from sklearn.linear_model import LinearRegression
try:
from sklearn.preprocessing import MinMaxScaler
except:
os.system('pip install sklearn.preprocessing')
from sklearn.preprocessing import MinMaxScaler
try:
import scipy
except:
os.system('pip install scipy')
import scipy
try:
import tensorflow
except:
os.system('pip install tensorflow')
import tensorflow
try:
from keras.models import Sequential
except:
os.system('pip install keras')
from keras.models import Sequential
from keras.layers import Dense, LSTM
style.use('fivethirtyeight')
plt.rcParams['axes.formatter.useoffset'] = False
pd.plotting.register_matplotlib_converters()
######################################
# Functions
######################################
#------------------------------------#
def predict_prices(dates, prices, x):
#------------------------------------#
print("tools_predict_generic: predict_prices ENTRY")
svr_lin = SVR(kernel = 'linear', C=1e3)
svr_rbf = SVR(kernel = 'rbf', C=1e3, gamma = 0.0004)
svr_poly = SVR(kernel = 'poly', C = 1e3, degree = 2)
#print("tools_predict_generic: predict_prices starting to svr_lin.fit data")
#svr_lin.fit(dates, prices)
print("tools_predict_generic: predict_prices starting to svr_poly.fit data")
svr_poly.fit(dates, prices)
poly_predict_feature = svr_poly.predict(x)[0]
print("tools_predict_generic: predict_prices starting to rbf_poly.fit data")
svr_rbf.fit(dates, prices)
rbf_predict_feature = svr_rbf.predict(x)[0]
plt.scatter(dates, prices, color='black', label = 'Actual Data')
plt.scatter( dates, svr_poly.predict(dates), color = 'green', label = 'Poly Model', alpha = 0.1)
plt.scatter( dates, svr_rbf.predict(dates), color = 'red', label = 'RBF Model')
plt.plot(x, poly_predict_feature, '*', color = 'green', label = 'Poly Predict_' + FEATURE_VARIABLE)
plt.plot( x, rbf_predict_feature, '*', color = 'blue', label = 'RBF Predict_' + FEATURE_VARIABLE)
plt.xlabel('Date')
plt.ylabel('Prices')
plt.title(ax1_subject + " Support Vector Regression")
plt.legend()
print("Poly answer for predicting the " + FEATURE_VARIABLE + " record", str(x), "is:",poly_predict_feature)
print("RBF answer for predicting the " + FEATURE_VARIABLE + " record", str(x), "is:", rbf_predict_feature )#svr_rbf.predict(x)[0])
plt.show()
return poly_predict_feature, rbf_predict_feature
# HOUSE KEEPING START HERE
######################################
currPath = os.getcwd() # Directory you are in NOW
savePath = 'askew' # We will be creating this new sub-directory
myPath = (currPath + '/' + savePath)# The full path of the new sub-dir
dir_path = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
df = pd.DataFrame()
if len(sys.argv) > 1:
if sys.argv[1]:
df = sys.argv[1]
else:
try:
os.chdir(myPath)
ax1_subject = 'JCP'
df = pd.read_csv((ax1_subject + '.csv'), parse_dates=True, index_col =0)
except Exception as e:
print("stocks_1.py did not find", ax1_subject, "information. As it's not in the datawarehouse, is the ticker symbol spelled correctly?")
print(e)
popupmsg("Symbol " + ax1_subject + " is not found! -- Spelling?")
sys.exit(0)
#######################################
# MAIN LOGIC
#######################################
DAYS_TO_REGRESS = 60
FEATURE_VARIABLE = 'Close'
df_save_last = df.tail(1)
#df = df.head(len(df) -1)
df = df.iloc[-(DAYS_TO_REGRESS):-1]
df.reset_index(inplace = True)
df_dates = df.loc[:, 'Date']
df_FEATURE = df.loc[:, FEATURE_VARIABLE]
dates = []
prices = []
#-------------------------------------#
# Use cnt instead of day of month, as
# we can read in more than 1 month
# or we start on mid-month (from 15nth
# thru the next 14nth.)
#-------------------------------------#
for x in range(len(df_dates)):
dates.append([x+1])
for open_price in df_FEATURE:
prices.append(float(open_price))
poly_predict_feature, rbf_predict_feature = predict_prices(dates, prices, [[(DAYS_TO_REGRESS + 1)]])