Stock Price Prediction Using LSTM CodingSaathi
Stock Price Prediction Using LSTM CodingSaathi
Import Libraries
In [2]: df = pd.read_csv("Stocks_dataset.csv")
df.head()
Out[2]:
symbol date close high low open volume adjClose adjHigh adjLow adjOpen adjVolume divC
2016-06-14
0 GOOG 718.27 722.47 713.1200 716.48 1306065 718.27 722.47 713.1200 716.48 1306065
00:00:00+00:00
2016-06-15
1 GOOG 718.92 722.98 717.3100 719.00 1214517 718.92 722.98 717.3100 719.00 1214517
00:00:00+00:00
2016-06-16
2 GOOG 710.36 716.65 703.2600 714.91 1982471 710.36 716.65 703.2600 714.91 1982471
00:00:00+00:00
2016-06-17
3 GOOG 691.72 708.82 688.4515 708.65 3402357 691.72 708.82 688.4515 708.65 3402357
00:00:00+00:00
2016-06-20
4 GOOG 693.71 702.48 693.4100 698.77 2082538 693.71 702.48 693.4100 698.77 2082538
00:00:00+00:00
Out[4]:
close high low open volume adjClose adjHigh adjLow adjOpe
count 1258.000000 1258.000000 1258.000000 1258.000000 1.258000e+03 1258.000000 1258.000000 1258.000000 1258.00000
mean 1216.317067 1227.430934 1204.176430 1215.260779 1.601590e+06 1216.317067 1227.430936 1204.176436 1215.26077
std 383.333358 387.570872 378.777094 382.446995 6.960172e+05 383.333358 387.570873 378.777099 382.44699
min 668.260000 672.300000 663.284000 671.000000 3.467530e+05 668.260000 672.300000 663.284000 671.00000
25% 960.802500 968.757500 952.182500 959.005000 1.173522e+06 960.802500 968.757500 952.182500 959.00500
50% 1132.460000 1143.935000 1117.915000 1131.150000 1.412588e+06 1132.460000 1143.935000 1117.915000 1131.15000
75% 1360.595000 1374.345000 1348.557500 1361.075000 1.812156e+06 1360.595000 1374.345000 1348.557500 1361.07500
max 2521.600000 2526.990000 2498.290000 2524.920000 6.207027e+06 2521.600000 2526.990000 2498.290000 2524.92000
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 1/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1258 entries, 0 to 1257
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 symbol 1258 non-null object
1 date 1258 non-null object
2 close 1258 non-null float64
3 high 1258 non-null float64
4 low 1258 non-null float64
5 open 1258 non-null float64
6 volume 1258 non-null int64
7 adjClose 1258 non-null float64
8 adjHigh 1258 non-null float64
9 adjLow 1258 non-null float64
10 adjOpen 1258 non-null float64
11 adjVolume 1258 non-null int64
12 divCash 1258 non-null float64
13 splitFactor 1258 non-null float64
dtypes: float64(10), int64(2), object(2)
memory usage: 137.7+ KB
Out[6]: symbol 0
date 0
close 0
high 0
low 0
open 0
volume 0
adjClose 0
adjHigh 0
adjLow 0
adjOpen 0
adjVolume 0
divCash 0
splitFactor 0
dtype: int64
Out[7]:
open close
date
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 2/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
Data Preprocessing
In [9]: # we'll normalizing all the values of all columns using MinMaxScaler
from sklearn.preprocessing import MinMaxScaler
mms = MinMaxScaler()
df[df.columns] = mms.fit_transform(df)
df.head()
Out[9]:
open close
date
Out[10]: 944
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 3/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
start_idx = 0
Out[19]: ((894, 50, 2), (894, 2), (264, 50, 2), (264, 2))
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 50, 50) 10600
=================================================================
Total params: 30902 (120.71 KB)
Trainable params: 30902 (120.71 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 4/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
In [28]: # fitting the model by iterating the dataset over 100 times(100 epochs)
model.fit(train_seq, train_label, epochs = 100, validation_data = (test_seq, test_label), verbose = 1
Epoch 1/100
28/28 [==============================] - 4s 46ms/step - loss: 0.0082 - mean_absolute_error: 0.0618
- val_loss: 0.0116 - val_mean_absolute_error: 0.0830
Epoch 2/100
28/28 [==============================] - 1s 21ms/step - loss: 7.3860e-04 - mean_absolute_error: 0.
0216 - val_loss: 0.0037 - val_mean_absolute_error: 0.0466
Epoch 3/100
28/28 [==============================] - 1s 22ms/step - loss: 4.4218e-04 - mean_absolute_error: 0.
0155 - val_loss: 0.0027 - val_mean_absolute_error: 0.0403
Epoch 4/100
28/28 [==============================] - 1s 22ms/step - loss: 4.5139e-04 - mean_absolute_error: 0.
0154 - val_loss: 0.0033 - val_mean_absolute_error: 0.0446
Epoch 5/100
28/28 [==============================] - 1s 22ms/step - loss: 4.2917e-04 - mean_absolute_error: 0.
0151 - val_loss: 0.0032 - val_mean_absolute_error: 0.0439
Epoch 6/100
28/28 [==============================] - 1s 22ms/step - loss: 4.1802e-04 - mean_absolute_error: 0.
0150 - val_loss: 0.0034 - val_mean_absolute_error: 0.0449
Epoch 7/100
28/28 [ ] 1 22 / t l 4 0603 04 b l t 0
In [29]: # predicting the values after running the model
test_predicted = model.predict(test_seq)
test_predicted[:5]
Out[34]:
open close open_predicted close_predicted
date
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 5/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
In [35]: # plotting the actual open and predicted open prices on date index
df_merge[['open','open_predicted']].plot(figsize=(10,6))
plt.xticks(rotation=45)
plt.xlabel('Date',size=15)
plt.ylabel('Stock Price',size=15)
plt.title('Actual vs Predicted for open price',size=15)
plt.show()
In [36]: # plotting the actual close and predicted close prices on date index
df_merge[['close','close_predicted']].plot(figsize=(10,6))
plt.xticks(rotation=45)
plt.xlabel('Date',size=15)
plt.ylabel('Stock Price',size=15)
plt.title('Actual vs Predicted for close price',size=15)
plt.show()
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 6/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
Out[37]:
open close open_predicted close_predicted
In [38]: # creating a DataFrame and filling values of open and close column
upcoming_prediction = pd.DataFrame(columns=['open','close'],index=df_merge.index)
upcoming_prediction.index=pd.to_datetime(upcoming_prediction.index)
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 7/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 8/9
10/14/23, 1:53 PM Stock_Price_Prediction_Using_LSTM - Jupyter Notebook
localhost:8888/notebooks/Stock_Price_Prediction_Using_LSTM.ipynb# 9/9