-
Notifications
You must be signed in to change notification settings - Fork 3
/
prediction.py
271 lines (216 loc) · 9.73 KB
/
prediction.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
import urllib
import xlrd
import pandas as pd
import numpy as np
import streamlit as st
import yfinance as yf
import seaborn as sns
import plotly.graph_objs as go
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
#####################################################################################################################
companies = {}
xls = xlrd.open_workbook("cname.xls")
sh = xls.sheet_by_index(0)
for i in range(505):
cell_value_class = sh.cell(i, 0).value
cell_value_id = sh.cell(i, 1).value
companies[cell_value_class] = cell_value_id
############################################################################
def company_name():
company = st.sidebar.selectbox("Companies", list(companies.keys()), 0)
return company
# company = company_name()
############################################################################
@st.cache(suppress_st_warning=True)
def prediction_graph(algo, confidence, cdata):
st.header(algo + ', Confidence score is ' + str(round(confidence, 2)))
fig6 = go.Figure(data=[go.Scatter(x=list(cdata.index), y=list(cdata.Close), name='Close'),
# go.Scatter(x=list(chart_data.index), y=list(chart_data.Vclose), name='Vclose'),
go.Scatter(x=list(cdata.index), y=list(cdata.Vpredictions),
name='Predictions')])
fig6.update_layout(width=850, height=550)
fig6.update_xaxes(rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=30, label="30D", step="day", stepmode="backward"),
dict(count=60, label="60D", step="day", stepmode="backward"),
dict(count=90, label="90D", step="day", stepmode="backward"),
dict(count=120, label="120D", step="day", stepmode="backward"),
dict(count=150, label="150D", step="day", stepmode="backward"),
dict(step="all")
])
))
st.plotly_chart(fig6)
#############################################################################################
def prediction():
def data_download():
company = company_name()
data = yf.download(tickers=companies[company], period='200d', interval='1d')
def divide(j):
j = j / 1000000
return j
data['Volume'] = data['Volume'].apply(divide)
data.rename(columns={'Volume': 'Volume (in millions)'}, inplace=True)
return data
df = data_download()
pred = st.sidebar.radio("Regression Type", ["Tree Prediction", "Linear Regression", "SVR Prediction",
"RBF Prediction", "Polynomial Prediction", "Linear Regression 2"])
# removing index which is date
df['Date'] = df.index
df.reset_index(drop=True, inplace=True)
# rearranging the columns
df = df[['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume (in millions)']]
df['Close'] = scaler.fit_transform(df[['Close']])
df = df[['Close']]
# create a variable to predict 'x' days out into the future
future_days = 50
# create a new column( target) shifted 'x' units/days up
df['Prediction'] = df[['Close']].shift(-future_days)
# create the feature data set (x) and convet it to a numpy array and remove the last 'x' rows
x = np.array(df.drop(['Prediction'], 1))[:-future_days]
# create a new target dataset (y) and convert it to a numpy array and get all of the target values except the last'x' rows)
y = np.array(df['Prediction'])[:-future_days]
# split the data into 75% training and 25% testing
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
# create the models
# create the decision treee regressor model
tree = DecisionTreeRegressor().fit(x_train, y_train)
# create the linear regression model
lr = LinearRegression().fit(x_train, y_train)
# create the svr model
svr_rbf = SVR(C=1e3, gamma=.1)
svr_rbf.fit(x_train, y_train)
# create the RBF model
rbf_svr = SVR(kernel='rbf', C=1000.0, gamma=.85)
rbf_svr.fit(x_train, y_train)
# Create the polyomial model
poly_svr = SVR(kernel='poly', C=1000.0, degree=2)
poly_svr.fit(x_train, y_train)
# create the linear 2 model
lin_svr = SVR(kernel='linear', C=1000.0, gamma=.85)
lin_svr.fit(x_train, y_train)
# get the last x rows of the feature dataset
x_future = df.drop(['Prediction'], 1)[:-future_days]
x_future = x_future.tail(future_days)
x_future = np.array(x_future)
# show the model tree prediction
tree_prediction = tree.predict(x_future)
# show the model linear regression prediction
lr_prediction = lr.predict(x_future)
# show the model SVR prediction
SVR_prediction = svr_rbf.predict(x_future)
# show the model RBF prediction
RBF_prediction = rbf_svr.predict(x_future)
# show the model Polynomial prediction
poly_prediction = poly_svr.predict(x_future)
##show thw model linear regression2 prediction
lr2_prediction = lin_svr.predict(x_future)
if pred == "Linear Regression":
predictions = lr_prediction
valid = df[x.shape[0]:]
valid['predictions'] = predictions
# alter
data = {'Close': [], 'Vclose': [], 'Vpredictions': []}
mod = pd.DataFrame(data)
mod.set_index = 'index'
mod.Close = df.Close
# mod.Vclose = df.Close.loc[:747]
# mod.Vpredictions = df.Close.loc[:747]
# mod.Vclose.loc[148:] = valid.Close
mod.Vpredictions.loc[148:] = valid.predictions
# mod.Close = df.Close.loc[:150]
chart_data = mod
lin_confidence = lr.score(x_test, y_test)
prediction_graph(pred, lin_confidence, chart_data)
elif pred == "Tree Prediction":
predictions = tree_prediction
valid = df[x.shape[0]:]
valid['predictions'] = predictions
# alter
data = {'Close': [], 'Vclose': [], 'Vpredictions': []}
mod = pd.DataFrame(data)
mod.set_index = 'index'
mod.Close = df.Close
# mod.Vclose = df.Close.loc[:747]
# mod.Vpredictions = df.Close.loc[:747]
# mod.Vclose.loc[148:] = valid.Close
mod.Vpredictions.loc[148:] = valid.predictions
# mod.Close = df.Close.loc[:150]
chart_data = mod
tree_confidence = tree.score(x_test, y_test)
prediction_graph(pred, tree_confidence, chart_data)
elif pred == "SVR Prediction":
predictions = SVR_prediction
valid = df[x.shape[0]:]
valid['predictions'] = predictions
# alter
data = {'Close': [], 'Vclose': [], 'Vpredictions': []}
mod = pd.DataFrame(data)
mod.set_index = 'index'
mod.Close = df.Close
# mod.Vclose = df.Close.loc[:747]
# mod.Vpredictions = df.Close.loc[:747]
# mod.Vclose.loc[148:] = valid.Close
mod.Vpredictions.loc[148:] = valid.predictions
# mod.Close = df.Close.loc[:150]
chart_data = mod
svr_confidence = svr_rbf.score(x_test, y_test)
prediction_graph(pred, svr_confidence, chart_data)
elif pred == "RBF Prediction":
predictions = RBF_prediction
valid = df[x.shape[0]:]
valid['predictions'] = predictions
# alter
data = {'Close': [], 'Vclose': [], 'Vpredictions': []}
mod = pd.DataFrame(data)
mod.set_index = 'index'
mod.Close = df.Close
# mod.Vclose = df.Close.loc[:747]
# mod.Vpredictions = df.Close.loc[:747]
# mod.Vclose.loc[148:] = valid.Close
mod.Vpredictions.loc[148:] = valid.predictions
# mod.Close = df.Close.loc[:150]
chart_data = mod
rbf_confidence = rbf_svr.score(x_test, y_test)
prediction_graph(pred, rbf_confidence, chart_data)
elif pred == "Polynomial Prediction":
predictions = poly_prediction
valid = df[x.shape[0]:]
valid['predictions'] = predictions
# alter
data = {'Close': [], 'Vclose': [], 'Vpredictions': []}
mod = pd.DataFrame(data)
mod.set_index = 'index'
mod.Close = df.Close
# mod.Vclose = df.Close.loc[:747]
# mod.Vpredictions = df.Close.loc[:747]
# mod.Vclose.loc[148:] = valid.Close
mod.Vpredictions.loc[148:] = valid.predictions
# mod.Close = df.Close.loc[:150]
chart_data = mod
poly_confidence = poly_svr.score(x_test, y_test)
elif pred == "Linear Regression 2":
predictions = lr2_prediction
valid = df[x.shape[0]:]
valid['predictions'] = predictions
# alter
data = {'Close': [], 'Vclose': [], 'Vpredictions': []}
mod = pd.DataFrame(data)
mod.set_index = 'index'
mod.Close = df.Close
# mod.Vclose = df.Close.loc[:747]
# mod.Vpredictions = df.Close.loc[:747]
# mod.Vclose.loc[148:] = valid.Close
mod.Vpredictions.loc[148:] = valid.predictions
# mod.Close = df.Close.loc[:150]
chart_data = mod
linsvr_confidence = lin_svr.score(x_test, y_test)
prediction_graph(pred, linsvr_confidence, chart_data)
##################################################################################
if __name__ == "__main__":
prediction()