/
train_model.py
147 lines (113 loc) · 4.1 KB
/
train_model.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
# This file directly implements the video https://www.youtube.com/watch?v=QIUxPv5PJOY
import pandas_datareader as web
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
import math as m
import GetStock as gs
from datetime import date
from tabulate import tabulate
from assistant import speak, takeCommand, wishme
import sys
plt.style.use('fivethirtyeight')
wishme()
speak('Which stock are you interested in?')
company = takeCommand()
if company is None:
speak('I suppose there was some noise while you were speaking. Would you give another try please?')
raise Exception('You need to repeat')
symbol = gs.getSymbol(company.lower())
stat = gs.getFuture(symbol)
if stat == 1:
feedback = f'{company} is a good choice for investing in for a few days to few weeks. \
I\'d recommend you have a look at the graph or the prediction chart to take your call.'
else:
feedback = f'It looks like it is not really a good idea to invest in {company}, \
but looking at graphs and history you can take you call.'
speak(feedback)
print(feedback)
def ask_about_stocks():
menu = 'You can either choose from looking at the graph, history or predictions.'
close = 'If you want to end this analysis, say "close session."'
speak(menu)
speak(close)
print(menu,close,sep='\n')
query = takeCommand()
print(query)
if 'show the graph' or 'show graph' in query:
plotGraph()
elif 'history' in query:
print(tabulate(df,headers = 'keys', tablefmt='fancy_grid'))
elif 'predictions' in query:
plotPredicted()
print(tabulate(valid,headers = 'keys', tablefmt='fancy_grid'))
elif 'close session' in query:
sys.exit()
else:
fdbk = 'Sorry, didn\'t catch you!'
speak(fdbk)
print(fdbk)
df = web.DataReader(symbol, data_source = 'yahoo',start ='2016-01-01',end= date.today())
data = df.filter(['Close']).values
def plotGraph(title = symbol,dataFrame = df,column = 'Close'):
plt.figure(figsize=(12,6))
plt.xlabel('Date',fontsize=10)
plt.ylabel('Rs. ',fontsize=10)
plt.plot(dataFrame[column])
plt.show()
# plotGraph()
data = df.filter(['Close'])
dataset = data.values
training_data_len = m.ceil(len(dataset)*0.8)
#scaling
scaler = MinMaxScaler(feature_range=(0,1))
scaler_data = scaler.fit_transform(dataset)
# training dataset
train_data = scaler_data[0:training_data_len,]
x_train, y_train = [],[]
for i in range(60,len(train_data)):
x_train.append(train_data[i-60:i,0])
y_train.append(train_data[i,0])
x_train , y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
# Build a LSTM model
def buildModel(x_train):
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape = (x_train.shape[1],1)))
model.add(LSTM(50, return_sequences= False))
model.add(Dense(25))
model.add(Dense(1))
model.compile(optimizer='adam',loss = 'mean_squared_error')
model.fit(x_train,y_train,batch_size=1, epochs=2)
return model
# Test Dataset
test_data = scaler_data[training_data_len-60:,:]
x_test, y_test = [], dataset[training_data_len:,:]
for i in range(60,len(test_data)):
x_test.append(test_data[i-60:i,0])
x_test = np.array(x_test)
x_test = np.reshape(x_test,(x_test.shape[0], x_test.shape[1],1))
# Get Predicted values from model
predictions = buildModel(x_train).predict(x_test)
predictions = scaler.inverse_transform(predictions)
# Plotting data
train = data[:training_data_len]
valid = data[training_data_len:]
valid['Predictions'] = predictions
# Plotting graph
def plotPredicted(title=f'Predictions/{symbol}',train=train,valid=valid):
plt.figure(figsize=(12,6))
plt.title('Predictions')
plt.xlabel('Date',fontsize=16)
plt.ylabel('Close Price (Rs.) ', fontsize=16)
plt.plot(train['Close'])
plt.plot(valid[['Close','Predictions']])
plt.legend(['Train','Val','Predictions'],loc='upper left')
plt.show()
# plotPredicted()
while True:
#Say Close Session for exiting the program
ask_about_stocks()