-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathML_file (1).py.py
266 lines (197 loc) · 7.18 KB
/
ML_file (1).py.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
# -*- coding: utf-8 -*-
"""FYP_iteration_3_stcked_lstm_tingo.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1cY9h_7j7V6FoVIVdfhzaLlByvjs82bS7
### Stock Market Prediction And Forecasting Using Stacked LSTM
"""
# !pip install tf-nightly
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as pdr
import numpy as np
import tensorflow as tf
tf.__version__
# import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
# stock = 'RELIANCE.NS' # 1
# stock = 'HDFCBANK.NS' # 2
# stock = 'M&M.NS' # 3
# stock = 'TATAMOTORS.NS' # 4
# stock = 'BAJFINANCE.NS' # 5
# stock = 'INFY' # 6
# stock = 'ICICIBANK.NS' # 7
# stock = 'BHEL.NS' # 8
stock = 'ADANIPOWER.NS' # 9
# stock = '(TCS.NS') # 10
# stock_data = stock_data.loc['2018-5-31':]
# df = stock_data
# df.to_csv('/content/{}_dataset.csv'.format(stock), index=False)
stock_data = pd.read_csv('/content/{}_dataset.csv'.format(stock)) # put adress where csv files are stored
stock_data
# stock_data
stock_data=df.reset_index()['Close']
# stock_data #important
# plt.plot(stock_data) #imp
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler(feature_range=(0,1))
stock_data=scaler.fit_transform(np.array(stock_data).reshape(-1,1))
# print(df1)
##splitting dataset into train and test split
training_size=int(len(stock_data)*0.65)
test_size=len(stock_data)-training_size
train_data,test_data=stock_data[0:training_size,:],stock_data[training_size:len(stock_data),:1]
# training_size,test_size #important
#train_data
import numpy
# convert an array of values into a dataset matrix
def create_dataset(dataset, time_step=1):
dataX, dataY = [], []
for i in range(len(dataset)-time_step-1):
a = dataset[i:(i+time_step), 0] ###i=0, 0,1,2,3-----99 100
dataX.append(a)
dataY.append(dataset[i + time_step, 0])
return numpy.array(dataX), numpy.array(dataY)
# reshape into X=t,t+1,t+2,t+3 and Y=t+4
time_step = 100
X_train, y_train = create_dataset(train_data, time_step)
X_test, ytest = create_dataset(test_data, time_step)
# print(X_train.shape), print(y_train.shape)#important
# print(X_test.shape), print(ytest.shape) #important
# reshape input to be [samples, time steps, features] which is required for LSTM
X_train =X_train.reshape(X_train.shape[0],X_train.shape[1] , 1)
X_test = X_test.reshape(X_test.shape[0],X_test.shape[1] , 1)
### Create the Stacked LSTM model
model=Sequential()
model.add(LSTM(50,return_sequences=True,input_shape=(time_step,1)))
model.add(LSTM(50,return_sequences=True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss='mean_squared_error',optimizer='adam')
model.summary()
model.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=10,batch_size=64,verbose=1)
### Lets Do the prediction and check performance metrics
train_predict=model.predict(X_train)
test_predict=model.predict(X_test)
##Transformback to original form
train_predict=scaler.inverse_transform(train_predict)
test_predict=scaler.inverse_transform(test_predict)
### Calculate RMSE performance metrics
import math
from sklearn.metrics import mean_squared_error
math.sqrt(mean_squared_error(y_train,train_predict))
### Test Data RMSE
math.sqrt(mean_squared_error(ytest,test_predict))
### Plotting
# shift train predictions for plotting
look_back=time_step
trainPredictPlot = numpy.empty_like(stock_data)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(stock_data)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(train_predict)+(look_back*2)+1:len(stock_data)-1, :] = test_predict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(stock_data))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
l = len(test_data) #important
x_input=test_data[l-100:].reshape(1,-1)
x_input.shape
temp_input=list(x_input)
temp_input=temp_input[0].tolist()
#temp_input
days = 30
n_steps=100
# demonstrate prediction for next 10 days
from numpy import array
lst_output=[]
i=0
while(i<days):
if(len(temp_input)>100):
#print(temp_input)
x_input=np.array(temp_input[1:])
# print("{} day input {}".format(i,x_input))#important
x_input=x_input.reshape(1,-1)
x_input = x_input.reshape((1, n_steps, 1))
#print(x_input)
yhat = model.predict(x_input, verbose=0)
# print("{} day output {}".format(i,yhat))#important
temp_input.extend(yhat[0].tolist())
temp_input=temp_input[1:]
#print(temp_input)
lst_output.extend(yhat.tolist())
i=i+1
else:
x_input = x_input.reshape((1, n_steps,1))
yhat = model.predict(x_input, verbose=0)
# print(yhat[0])#important
temp_input.extend(yhat[0].tolist())
# print(len(temp_input))#important
lst_output.extend(yhat.tolist())
i=i+1
# print(lst_output)
day_new=np.arange(1,time_step +1)
day_pred=np.arange(time_step +1,time_step +1 + days)
l2 = len(stock_data)
plt.plot(day_new,scaler.inverse_transform(stock_data[l2-100:]))
plt.plot(day_pred,scaler.inverse_transform(lst_output))
stock_data_merged=stock_data.tolist()
stock_data_merged.extend(lst_output)
plt.plot(stock_data_merged[1200:])
stock_data_merged=scaler.inverse_transform(stock_data_merged).tolist()
plt.plot(stock_data_merged)
from datetime import datetime, timedelta
def generate_dates(start_date, n):
dates = []
current_date = start_date
for _ in range(n):
dates.append(current_date.strftime('%Y-%m-%d'))
current_date += timedelta(days=1)
return dates
# Example usage
start_date = datetime(2023, 5, 31) # Specify the start date
n = 10 # Specify the number of days
pred_date_list = generate_dates(start_date, n)
print(pred_date_list)
# original_dates = df['date'].to_list()
# original_dates
predicted_values = scaler.inverse_transform(lst_output)
# predicted_values
model.save('models/{}_stock-100_epochs.pb'.format(stock))
# #tflite
# model_for_lite = tf.keras.models.load_model('models/{}_stock-100_epochs.pb'.format(stock))
# # converter = tf.lite.TFLiteConverter.from_keras_model(model)
# converter._experimental_lower_tensor_list_ops = False
# converter = tf.lite.TFLiteConverter.from_keras_model(model_for_lite)
# tflite_model = converter.convert()
# # open("models/APPl_stock-100_epochs.tflite","wb").write(tflite_model)
# with open("models/APPl_stock-100_epochs.tflite","wb") as f:
# f.write(tflite_model)
# run_model = tf.function(lambda x: model(x))
# # This is important, let's fix the input size.
# BATCH_SIZE = 1
# STEPS = 100
# INPUT_SIZE = 50
# concrete_func = run_model.get_concrete_function(
# tf.TensorSpec([BATCH_SIZE, STEPS, INPUT_SIZE], model.inputs[0].dtype))
# # model directory.
# MODEL_DIR = "keras_lstm"
# model.save(MODEL_DIR, save_format="tf", signatures=concrete_func)
# converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_DIR)
# tflite_model = converter.convert()
close = df['Close']
open = df['Open']
high = df['High']
low = df['Low']
# close = close.to_list()
# type(close)
close
open
high
low
predictedpoints = scaler.inverse_transform(lst_output)