-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdemo.py
97 lines (77 loc) · 3.3 KB
/
demo.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
from joblib import dump, load
import pandas as pd
import numpy as np
import os
import pandas_datareader as web
import math
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential, load_model
from keras.layers import Dense, LSTM
import webbrowser
while(True):
#Print menu and get ticker/date inputs
print('Welcome to the Stock Predictor App. Please Follow Instructions:')
print('1. Linear Regression Model ')
print('2. LSTM Model ')
print('3. Exit ')
modelType = str(input ("Select an option: "))
if(modelType == '1'):
ticker = str(input ("Enter Stock Ticker Name: "))
date=str(input ("Enter Date (yyyy-mm-dd): "))
# Load linear regression model and stock data
lr = load('models/models_%s.joblib'%ticker)
df = pd.read_csv('data/%s.csv'%ticker, sep = ',', header = 0)
# Get index of specified date and print close price for that day
current_row=df.loc[df['Date'] == date]
#print(current_row)
index=current_row.index.tolist()
x_forecast = np.array(current_row.drop(['Date', 'Prediction'],1))
print('\nPrice for %s'%date,':',x_forecast[0][3])
# Predict close price of next day
lr_prediction = lr.predict(x_forecast)
print('Prediction for 1 day out:', lr_prediction[0])
# Get actual next day price
actual=df.iloc[index[0]+1]
print('Actual Price for 1 day out:',actual['Close'],'\n')
# Display figure
webbrowser.open('figs/%s.png'%ticker)
elif(modelType == '2'):
ticker = str(input ("Enter Stock Ticker Name: "))
date=str(input ("Enter Date Before 2020-10-01 (yyyy-mm-dd): "))
# LSTM
# Load in LSTM model
lstm = load_model('lstm_models/' + ticker + '.h5')
# Pull stock data for ticker
df = web.DataReader(ticker, data_source = 'yahoo', start = '2015-01-01', end = '2020-10-01')
data=df['Adj Close']
# Reshape data
dataset = np.array(data.values)
dataset = np.reshape(dataset, (-1, 1))
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
# Get index of specified date
df.reset_index(inplace=True,drop=False)
current_row=df.loc[df['Date'] == date]
index=current_row.index.tolist()
# Get training data from scaled
train_data = scaled_data[index[0]-60:index[0]]
#train_data = np.array(train_data)
train_data = np.array([train_data])
# Reshape training data
#np.reshape(train_data, shape = (1, 60, 1))
# Get row for specified data and print close price
x_forecast = np.array(current_row.drop(['Date'],1))
print('\nPrice for %s'%date,':',x_forecast[0][5])
# Predict close price for next day
lstm_prediction = lstm.predict(train_data)
lstm_prediction = scaler.inverse_transform(lstm_prediction)
print('Prediction for 1 day out:', lstm_prediction[0][0])
# Get actual next day price
actual=df.iloc[index[0]+1]
print('Actual Price for 1 day out:',actual['Adj Close'],'\n')
# Display figure
webbrowser.open('lstm_figs/%s.png'%ticker)
elif (modelType == '3'):
break
else:
print('Incorrect Input. Please Enter a Number 1-3.\n')