Stock Prediction Web App - Jupyter Notebook
Stock Prediction Web App - Jupyter Notebook
Stock Prediction Web App - Jupyter Notebook
In [9]:
import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
yf.pdr_override()
import datetime as dt
import json
import tensorflow as tf
from tensorflow import keras
In [10]:
start = '2010-01-01'
end = '2022-12-31'
[*********************100%***********************] 1 of 1 completed
Out[10]:
Date
In [11]:
df.tail()
Out[11]:
Date
In [12]:
df = df.reset_index()
df.head()
Out[12]:
In [13]:
Out[13]:
In [16]:
plt.plot(df.Close)
Out[16]:
[<matplotlib.lines.Line2D at 0x1825bfc77c0>]
In [18]:
df
Out[18]:
In [19]:
Out[19]:
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
...
3267 150.515600
3268 150.157800
3269 149.764699
3270 149.412100
3271 149.062199
Name: Close, Length: 3272, dtype: float64
In [25]:
plt.figure(figsize = (12,6))
plt.plot(df.Close, label = 'Closing Price')
plt.plot(ma100,'r', label='Moving 100 day average')
plt.legend()
plt.show()
In [26]:
Out[26]:
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
...
3267 152.1331
3268 152.0096
3269 151.8867
3270 151.7593
3271 151.6110
Name: Close, Length: 3272, dtype: float64
In [11]:
plt.figure(figsize = (12,6))
plt.plot(df.Close,label='Closing Price')
plt.plot(ma100,'r',label = 'Moving 100 day average')
plt.plot(ma200,'g',label = 'Moving 200 day average')
plt.legend()
plt.show()
In [12]:
df.shape
Out[12]:
(3272, 5)
In [13]:
data_training = pd.DataFrame(df['Close'][0:int(len(df)*0.70)])
data_testing = pd.DataFrame(df['Close'][int(len(df)*0.70):int(len(df))])
print(data_training.shape)
print(data_testing.shape)
(2290, 1)
(982, 1)
In [14]:
data_training.head()
Out[14]:
Close
0 7.643214
1 7.656429
2 7.534643
3 7.520714
4 7.570714
In [15]:
from sklearn.preprocessing import MinMaxScaler #data scaling = reduces the difference between the points in the data
# which results in greater accuracy. It comes under Data Preproccesing
scaler = MinMaxScaler(feature_range=(0,1))
In [16]:
data_training_array = scaler.fit_transform(data_training)
data_training_array
Out[16]:
array([[0.01533047],
[0.01558878],
[0.01320823],
...,
[0.71710501],
[0.71739828],
[0.70127194]])
In [17]:
data_training_array.shape
Out[17]:
(2290, 1)
In [18]:
x_train = [] # this is the steps we take, for example 100 days data
y_train = [] #this is the predicted value, ie value on 101 day after analysing 100 days.
In [19]:
x_train.shape
Out[19]:
(2190, 100, 1)
In [20]:
y_train.shape
Out[20]:
(2190,)
In [21]:
#ML Model
In [22]:
In [23]:
model = Sequential()
model.add(LSTM(units = 50, activation = 'relu', return_sequences= True, input_shape = (x_train.shape[1],1)))
model.add(Dropout(0.2))
In [24]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 100, 50) 10400
=================================================================
Total params: 178,761
Trainable params: 178,761
Non-trainable params: 0
_________________________________________________________________
In [25]:
Epoch 1/50
69/69 [==============================] - 20s 206ms/step - loss: 0.0347
Epoch 2/50
69/69 [==============================] - 14s 199ms/step - loss: 0.0070
Epoch 3/50
69/69 [==============================] - 14s 201ms/step - loss: 0.0065
Epoch 4/50
69/69 [==============================] - 14s 199ms/step - loss: 0.0063
Epoch 5/50
69/69 [==============================] - 14s 204ms/step - loss: 0.0054
Epoch 6/50
69/69 [==============================] - 15s 217ms/step - loss: 0.0047
Epoch 7/50
69/69 [==============================] - 15s 215ms/step - loss: 0.0049
Epoch 8/50
69/69 [==============================] - 14s 207ms/step - loss: 0.0049
Epoch 9/50
69/69 [==============================] - 16s 234ms/step - loss: 0.0048
Epoch 10/50
69/69 [==============================] - 15s 218ms/step - loss: 0.0040
Epoch 11/50
69/69 [==============================] - 14s 206ms/step - loss: 0.0038
Epoch 12/50
69/69 [==============================] - 14s 209ms/step - loss: 0.0038
Epoch 13/50
69/69 [==============================] - 15s 220ms/step - loss: 0.0033
Epoch 14/50
69/69 [==============================] - 17s 243ms/step - loss: 0.0035
Epoch 15/50
69/69 [==============================] - 15s 219ms/step - loss: 0.0037
Epoch 16/50
69/69 [==============================] - 14s 204ms/step - loss: 0.0039
Epoch 17/50
69/69 [==============================] - 15s 213ms/step - loss: 0.0034
Epoch 18/50
69/69 [==============================] - 15s 214ms/step - loss: 0.0033
Epoch 19/50
69/69 [==============================] - 14s 208ms/step - loss: 0.0031
Epoch 20/50
69/69 [==============================] - 15s 218ms/step - loss: 0.0028
Epoch 21/50
69/69 [==============================] - 15s 220ms/step - loss: 0.0030
Epoch 22/50
69/69 [==============================] - 16s 227ms/step - loss: 0.0029
Epoch 23/50
69/69 [==============================] - 15s 218ms/step - loss: 0.0022
Epoch 24/50
69/69 [==============================] - 15s 224ms/step - loss: 0.0024
Epoch 25/50
69/69 [==============================] - 15s 221ms/step - loss: 0.0025
Epoch 26/50
69/69 [==============================] - 15s 211ms/step - loss: 0.0024
Epoch 27/50
69/69 [==============================] - 14s 209ms/step - loss: 0.0023
Epoch 28/50
69/69 [==============================] - 15s 223ms/step - loss: 0.0022
Epoch 29/50
69/69 [==============================] - 15s 222ms/step - loss: 0.0022
Epoch 30/50
69/69 [==============================] - 16s 229ms/step - loss: 0.0020
Epoch 31/50
69/69 [==============================] - 16s 228ms/step - loss: 0.0021
Epoch 32/50
69/69 [==============================] - 16s 237ms/step - loss: 0.0022
Epoch 33/50
69/69 [==============================] - 16s 233ms/step - loss: 0.0023
Epoch 34/50
69/69 [==============================] - 16s 232ms/step - loss: 0.0020
Epoch 35/50
69/69 [==============================] - 16s 226ms/step - loss: 0.0018
Epoch 36/50
69/69 [==============================] - 16s 233ms/step - loss: 0.0018
Epoch 37/50
69/69 [==============================] - 15s 215ms/step - loss: 0.0020
Epoch 38/50
69/69 [==============================] - 16s 238ms/step - loss: 0.0020
Epoch 39/50
69/69 [==============================] - 15s 224ms/step - loss: 0.0021
Epoch 40/50
69/69 [==============================] - 16s 235ms/step - loss: 0.0017
Epoch 41/50
69/69 [==============================] - 16s 225ms/step - loss: 0.0017
Epoch 42/50
69/69 [==============================] - 14s 204ms/step - loss: 0.0018
Epoch 43/50
69/69 [==============================] - 15s 220ms/step - loss: 15.4308
<keras.callbacks.History at 0x20de7492d90>
In [26]:
model.save('keras_model4.keras')
In [27]:
data_testing.head()
Out[27]:
Close
2290 42.602501
2291 42.357498
2292 42.722500
2293 42.544998
2294 42.700001
In [28]:
past_100_days = data_training.tail(100)
In [29]:
In [30]:
final_df.head()
Out[30]:
Close
0 55.959999
1 54.470001
2 54.560001
3 54.592499
4 55.007500
In [31]:
input_data = scaler.fit_transform(final_df)
input_data
Out[31]:
array([[0.13937014],
[0.1291969 ],
[0.1298114 ],
...,
[0.61785443],
[0.64222927],
[0.64441407]])
In [32]:
input_data.shape
Out[32]:
(1082, 1)
In [33]:
x_test = []
y_test = []
In [34]:
(982, 100, 1)
(982,)
In [35]:
#Making Predictions
y_predicted = model.predict(x_test)
In [36]:
y_predicted.shape
Out[36]:
(982, 1)
In [37]:
y_test
Out[37]:
In [38]:
y_predicted
Out[38]:
array([[0.09207357],
[0.09277508],
[0.09351471],
[0.09425803],
[0.09497693],
[0.09565249],
[0.09627241],
[0.09683166],
[0.09733434],
[0.09778409],
[0.09819143],
[0.09857252],
[0.09894121],
[0.09930849],
[0.09967425],
[0.10003999],
[0.1004099 ],
[0.1007849 ],
In [42]:
scaler.scale_ #gives the factor with which the above data is scaled down so that we can scale it up again
Out[42]:
array([0.00682769])
In [44]:
scale_factor = 1/0.00682769
y_predicted = y_predicted * scale_factor
y_test = y_test * scale_factor
In [45]:
plt.figure(figsize=(12,6))
plt.plot(y_test, 'b', label = "Original Price")
plt.plot(y_predicted, 'r', label = "Predicted Price")
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
plt.show()
In [ ]: