Content
Stock price prediction is the task of forecasting the future value of a given stock. Given the historical daily close price for S&P 500 Index, prepare and compare forecasting solutions. S&P 500 or Standard and Poor's 500 index is an index comprising of 500 stocks from different sectors of US economy and is an indicator of US equities. Other such indices are the Dow 30, NIFTY 50, Nikkei 225, etc. For the purpose of understanding, we are utilizing S&P500 index, concepts, and knowledge can be applied to other stocks as well.

Data
The historical stock price information is also publicly available. For our current use case, we will utilize the pandas_datareader library to get the required S&P 500 index history using Yahoo Finance databases. We utilize the closing price information from the data available though other information such as opening price, adjusted closing price, etc., are also available.

Features and Terminology
In stock trading, the high and low refer to the maximum and minimum prices in a given time period. Open and close are the prices at which a stock began and ended trading in the same period. Volume is the total amount of trading activity. Adjusted values factor in corporate actions such as dividends, stock splits, and new share issuance.

In [ ]:
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.optimizers import Adam
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.metrics import RootMeanSquaredError
from tensorflow.keras.models import load_model

Read data¶

In [ ]:
data = pd.read_csv('data/yahoo_stock.csv')

# Make the Date time column as the index
data.index = pd.to_datetime(data['Date'])
data.head(3)
Out[ ]:
Date High Low Open Close Volume Adj Close
Date
2015-11-23 2015-11-23 2095.610107 2081.389893 2089.409912 2086.590088 3.587980e+09 2086.590088
2015-11-24 2015-11-24 2094.120117 2070.290039 2084.419922 2089.139893 3.884930e+09 2089.139893
2015-11-25 2015-11-25 2093.000000 2086.300049 2089.300049 2088.870117 2.852940e+09 2088.870117

Exploratory Data Analysis¶

In [ ]:
# information of data
data.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1825 entries, 2015-11-23 to 2020-11-20
Data columns (total 7 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   Date       1825 non-null   object 
 1   High       1825 non-null   float64
 2   Low        1825 non-null   float64
 3   Open       1825 non-null   float64
 4   Close      1825 non-null   float64
 5   Volume     1825 non-null   float64
 6   Adj Close  1825 non-null   float64
dtypes: float64(6), object(1)
memory usage: 114.1+ KB
In [ ]:
data.describe()
Out[ ]:
High Low Open Close Volume Adj Close
count 1825.000000 1825.000000 1825.000000 1825.000000 1.825000e+03 1825.000000
mean 2660.718673 2632.817580 2647.704751 2647.856284 3.869627e+09 2647.856284
std 409.680853 404.310068 407.169994 407.301177 1.087593e+09 407.301177
min 1847.000000 1810.099976 1833.400024 1829.079956 1.296540e+09 1829.079956
25% 2348.350098 2322.250000 2341.979980 2328.949951 3.257950e+09 2328.949951
50% 2696.250000 2667.840088 2685.489990 2683.340088 3.609740e+09 2683.340088
75% 2930.790039 2900.709961 2913.860107 2917.520020 4.142850e+09 2917.520020
max 3645.989990 3600.159912 3612.090088 3626.909912 9.044690e+09 3626.909912
In [ ]:
# Data Correlation: Relationship between columns
data[['Open','Close','Adj Close','High','Low']].corr()
Out[ ]:
Open Close Adj Close High Low
Open 1.000000 0.998344 0.998344 0.999328 0.998794
Close 0.998344 1.000000 1.000000 0.998958 0.999020
Adj Close 0.998344 1.000000 1.000000 0.998958 0.999020
High 0.999328 0.998958 0.998958 1.000000 0.998154
Low 0.998794 0.999020 0.999020 0.998154 1.000000
In [ ]:
# Visualization of correlation result with seaborn library heatmap.
f, ax = plt.subplots(figsize = (6,6))
sns.heatmap(data[['Open','Close','Adj Close','High','Low']].corr(), annot = True, linewidths=0.5, linecolor = "black", fmt = ".3f", ax = ax)
plt.show()
No description has been provided for this image
In [ ]:
date = data.index

open_price = data.loc[:, ["Open"]]

plt.plot(date, open_price, label="opening prices")
plt.xlabel("Date")
plt.ylabel("Stock Prices")
plt.legend()
plt.title('Opening Stock Prices')
plt.show()
No description has been provided for this image

Retrieve Opening stock prices¶

In [ ]:
data = data.loc[:, ["Open"]].values

Scale the data¶

In [ ]:
# Feature Scaling
sc = MinMaxScaler(feature_range=(0,1))
data = sc.fit_transform(data)

Split data into training, validation and test sets¶

In [ ]:
train_size = int(len(data) * 0.70)
remainder = round((len(data) - train_size)/2)
val_size = train_size + remainder

print(f"train_size === {train_size}; val_size and test_size === {val_size}")

train = data[0:train_size, :]
val = data[train_size:val_size, :]
test = data[val_size:len(data), :]
#test = data[train_size:len(data), :]
print(f"length of train === {len(train)}; length of val == {len(val)}; length of test === {len(test)}")
train_size === 1277; val_size and test_size === 1551
length of train === 1277; length of val == 274; length of test === 274

Structure the dataset for LSTM model¶

In [ ]:
def create_dataset(dataset, time_steps):
    dataX = []
    dataY = []
    for i in range(len(dataset) - time_steps - 1):
        a = dataset[i: (i + time_steps), 0]
        dataX.append(a)
        dataY.append(dataset[i + time_steps, 0])
    return np.array(dataX), np.array(dataY)
In [ ]:
# reshape into X=t and Y=t+1
time_steps = 50
n_features = 1 
X_train, y_train = create_dataset(dataset=train, time_steps=time_steps)
X_val, y_val = create_dataset(dataset=val, time_steps=time_steps)
X_test, y_test = create_dataset(dataset=test, time_steps=time_steps)
print(f"X_train shape === {X_train.shape}; y_train shape === {y_train.shape}")
print(f"X_val shape === {X_val.shape}; y_val shape === {y_val.shape}")
print(f"X_test shape === {X_test.shape}; y_test shape === {y_test.shape}")
X_train shape === (1226, 50); y_train shape === (1226,)
X_val shape === (223, 50); y_val shape === (223,)
X_test shape === (223, 50); y_test shape === (223,)

Define the layers of the LSTM¶

In [ ]:
# Initialize LSTM network
model = Sequential()
# Add 1st layer LSTM and some Dropout regularisation
model.add(LSTM(units=60, return_sequences=True, input_shape=(time_steps, n_features)))
model.add(Dropout(0.1))
# Add 2nd layer LSTM and some Dropout regularisation
#model.add(LSTM(units= 60, return_sequences=True))
#model.add(Dropout(0.1))
# Add a third LSTM layer and some Dropout regularisation
model.add(LSTM(units= 60, return_sequences=False))
model.add(Dropout(0.1))
# Add the output layer
model.add(Dense(1))

#model summary
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 50, 60)            14880     
                                                                 
 dropout (Dropout)           (None, 50, 60)            0         
                                                                 
 lstm_1 (LSTM)               (None, 60)                29040     
                                                                 
 dropout_1 (Dropout)         (None, 60)                0         
                                                                 
 dense (Dense)               (None, 1)                 61        
                                                                 
=================================================================
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 50, 60)            14880     
                                                                 
 dropout (Dropout)           (None, 50, 60)            0         
                                                                 
 lstm_1 (LSTM)               (None, 60)                29040     
                                                                 
 dropout_1 (Dropout)         (None, 60)                0         
                                                                 
 dense (Dense)               (None, 1)                 61        
                                                                 
=================================================================
Total params: 43,981
Trainable params: 43,981
Non-trainable params: 0
_________________________________________________________________

Create the model and save the best model only¶

In [ ]:
cp = ModelCheckpoint('ysp_models/', save_best_only=True)
model.compile(loss=MeanSquaredError(), optimizer=Adam(learning_rate=0.001), metrics=[RootMeanSquaredError()])
In [ ]:
model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=100, batch_size=32, verbose=2, callbacks=[cp])
Epoch 1/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 11s - loss: 0.0145 - root_mean_squared_error: 0.1203 - val_loss: 0.0091 - val_root_mean_squared_error: 0.0955 - 11s/epoch - 285ms/step
Epoch 2/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 0.0015 - root_mean_squared_error: 0.0381 - val_loss: 0.0019 - val_root_mean_squared_error: 0.0434 - 7s/epoch - 174ms/step
Epoch 3/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 0.0010 - root_mean_squared_error: 0.0318 - val_loss: 5.8686e-04 - val_root_mean_squared_error: 0.0242 - 7s/epoch - 183ms/step
Epoch 4/100
39/39 - 1s - loss: 0.0011 - root_mean_squared_error: 0.0326 - val_loss: 9.2534e-04 - val_root_mean_squared_error: 0.0304 - 1s/epoch - 32ms/step
Epoch 5/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 0.0011 - root_mean_squared_error: 0.0327 - val_loss: 4.7012e-04 - val_root_mean_squared_error: 0.0217 - 7s/epoch - 184ms/step
Epoch 6/100
39/39 - 1s - loss: 9.9894e-04 - root_mean_squared_error: 0.0316 - val_loss: 6.7568e-04 - val_root_mean_squared_error: 0.0260 - 1s/epoch - 32ms/step
Epoch 7/100
39/39 - 1s - loss: 9.4816e-04 - root_mean_squared_error: 0.0308 - val_loss: 5.5344e-04 - val_root_mean_squared_error: 0.0235 - 1s/epoch - 33ms/step
Epoch 8/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 9.2077e-04 - root_mean_squared_error: 0.0303 - val_loss: 4.1804e-04 - val_root_mean_squared_error: 0.0204 - 7s/epoch - 185ms/step
Epoch 9/100
39/39 - 1s - loss: 9.4210e-04 - root_mean_squared_error: 0.0307 - val_loss: 5.7929e-04 - val_root_mean_squared_error: 0.0241 - 1s/epoch - 34ms/step
Epoch 10/100
39/39 - 1s - loss: 9.0604e-04 - root_mean_squared_error: 0.0301 - val_loss: 0.0014 - val_root_mean_squared_error: 0.0373 - 1s/epoch - 36ms/step
Epoch 11/100
39/39 - 1s - loss: 8.7880e-04 - root_mean_squared_error: 0.0296 - val_loss: 5.6590e-04 - val_root_mean_squared_error: 0.0238 - 1s/epoch - 34ms/step
Epoch 12/100
39/39 - 1s - loss: 8.0760e-04 - root_mean_squared_error: 0.0284 - val_loss: 0.0010 - val_root_mean_squared_error: 0.0320 - 1s/epoch - 37ms/step
Epoch 13/100
39/39 - 1s - loss: 8.1497e-04 - root_mean_squared_error: 0.0285 - val_loss: 0.0018 - val_root_mean_squared_error: 0.0424 - 1s/epoch - 32ms/step
Epoch 14/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 8.1098e-04 - root_mean_squared_error: 0.0285 - val_loss: 4.1605e-04 - val_root_mean_squared_error: 0.0204 - 7s/epoch - 189ms/step
Epoch 15/100
39/39 - 1s - loss: 6.9124e-04 - root_mean_squared_error: 0.0263 - val_loss: 5.0263e-04 - val_root_mean_squared_error: 0.0224 - 1s/epoch - 34ms/step
Epoch 16/100
39/39 - 1s - loss: 7.5576e-04 - root_mean_squared_error: 0.0275 - val_loss: 0.0013 - val_root_mean_squared_error: 0.0355 - 1s/epoch - 33ms/step
Epoch 17/100
39/39 - 1s - loss: 7.7023e-04 - root_mean_squared_error: 0.0278 - val_loss: 7.0411e-04 - val_root_mean_squared_error: 0.0265 - 1s/epoch - 36ms/step
Epoch 18/100
39/39 - 1s - loss: 7.3887e-04 - root_mean_squared_error: 0.0272 - val_loss: 9.3054e-04 - val_root_mean_squared_error: 0.0305 - 1s/epoch - 32ms/step
Epoch 19/100
39/39 - 1s - loss: 9.1325e-04 - root_mean_squared_error: 0.0302 - val_loss: 0.0027 - val_root_mean_squared_error: 0.0522 - 1s/epoch - 33ms/step
Epoch 20/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 7.7403e-04 - root_mean_squared_error: 0.0278 - val_loss: 3.5627e-04 - val_root_mean_squared_error: 0.0189 - 7s/epoch - 190ms/step
Epoch 21/100
39/39 - 1s - loss: 6.8167e-04 - root_mean_squared_error: 0.0261 - val_loss: 6.8606e-04 - val_root_mean_squared_error: 0.0262 - 1s/epoch - 34ms/step
Epoch 22/100
39/39 - 1s - loss: 6.7628e-04 - root_mean_squared_error: 0.0260 - val_loss: 0.0017 - val_root_mean_squared_error: 0.0407 - 1s/epoch - 34ms/step
Epoch 23/100
39/39 - 1s - loss: 6.7625e-04 - root_mean_squared_error: 0.0260 - val_loss: 3.6141e-04 - val_root_mean_squared_error: 0.0190 - 1s/epoch - 37ms/step
Epoch 24/100
39/39 - 1s - loss: 6.3973e-04 - root_mean_squared_error: 0.0253 - val_loss: 0.0010 - val_root_mean_squared_error: 0.0322 - 1s/epoch - 35ms/step
Epoch 25/100
39/39 - 1s - loss: 6.2438e-04 - root_mean_squared_error: 0.0250 - val_loss: 3.6078e-04 - val_root_mean_squared_error: 0.0190 - 1s/epoch - 35ms/step
Epoch 26/100
39/39 - 1s - loss: 5.9108e-04 - root_mean_squared_error: 0.0243 - val_loss: 4.7764e-04 - val_root_mean_squared_error: 0.0219 - 1s/epoch - 34ms/step
Epoch 27/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 7.1548e-04 - root_mean_squared_error: 0.0267 - val_loss: 3.4014e-04 - val_root_mean_squared_error: 0.0184 - 7s/epoch - 192ms/step
Epoch 28/100
39/39 - 1s - loss: 6.7014e-04 - root_mean_squared_error: 0.0259 - val_loss: 4.5214e-04 - val_root_mean_squared_error: 0.0213 - 1s/epoch - 34ms/step
Epoch 29/100
39/39 - 1s - loss: 6.6318e-04 - root_mean_squared_error: 0.0258 - val_loss: 3.6252e-04 - val_root_mean_squared_error: 0.0190 - 1s/epoch - 36ms/step
Epoch 30/100
39/39 - 2s - loss: 6.0548e-04 - root_mean_squared_error: 0.0246 - val_loss: 0.0016 - val_root_mean_squared_error: 0.0402 - 2s/epoch - 39ms/step
Epoch 31/100
39/39 - 1s - loss: 6.3131e-04 - root_mean_squared_error: 0.0251 - val_loss: 6.1551e-04 - val_root_mean_squared_error: 0.0248 - 1s/epoch - 35ms/step
Epoch 32/100
39/39 - 1s - loss: 5.7391e-04 - root_mean_squared_error: 0.0240 - val_loss: 3.5693e-04 - val_root_mean_squared_error: 0.0189 - 1s/epoch - 36ms/step
Epoch 33/100
39/39 - 1s - loss: 5.6554e-04 - root_mean_squared_error: 0.0238 - val_loss: 6.8744e-04 - val_root_mean_squared_error: 0.0262 - 1s/epoch - 37ms/step
Epoch 34/100
39/39 - 1s - loss: 5.8865e-04 - root_mean_squared_error: 0.0243 - val_loss: 4.2658e-04 - val_root_mean_squared_error: 0.0207 - 1s/epoch - 37ms/step
Epoch 35/100
39/39 - 1s - loss: 5.5730e-04 - root_mean_squared_error: 0.0236 - val_loss: 6.7748e-04 - val_root_mean_squared_error: 0.0260 - 1s/epoch - 36ms/step
Epoch 36/100
39/39 - 1s - loss: 5.8872e-04 - root_mean_squared_error: 0.0243 - val_loss: 5.1022e-04 - val_root_mean_squared_error: 0.0226 - 1s/epoch - 35ms/step
Epoch 37/100
39/39 - 2s - loss: 5.8969e-04 - root_mean_squared_error: 0.0243 - val_loss: 4.6551e-04 - val_root_mean_squared_error: 0.0216 - 2s/epoch - 40ms/step
Epoch 38/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 5.1157e-04 - root_mean_squared_error: 0.0226 - val_loss: 3.1386e-04 - val_root_mean_squared_error: 0.0177 - 7s/epoch - 190ms/step
Epoch 39/100
39/39 - 1s - loss: 5.1815e-04 - root_mean_squared_error: 0.0228 - val_loss: 0.0013 - val_root_mean_squared_error: 0.0363 - 1s/epoch - 37ms/step
Epoch 40/100
39/39 - 2s - loss: 5.9266e-04 - root_mean_squared_error: 0.0243 - val_loss: 7.7861e-04 - val_root_mean_squared_error: 0.0279 - 2s/epoch - 41ms/step
Epoch 41/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 5.0273e-04 - root_mean_squared_error: 0.0224 - val_loss: 2.6234e-04 - val_root_mean_squared_error: 0.0162 - 8s/epoch - 194ms/step
Epoch 42/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 5.3761e-04 - root_mean_squared_error: 0.0232 - val_loss: 2.5171e-04 - val_root_mean_squared_error: 0.0159 - 7s/epoch - 190ms/step
Epoch 43/100
39/39 - 2s - loss: 5.0266e-04 - root_mean_squared_error: 0.0224 - val_loss: 3.1004e-04 - val_root_mean_squared_error: 0.0176 - 2s/epoch - 39ms/step
Epoch 44/100
39/39 - 2s - loss: 4.7745e-04 - root_mean_squared_error: 0.0219 - val_loss: 5.0821e-04 - val_root_mean_squared_error: 0.0225 - 2s/epoch - 41ms/step
Epoch 45/100
39/39 - 2s - loss: 4.9197e-04 - root_mean_squared_error: 0.0222 - val_loss: 7.2784e-04 - val_root_mean_squared_error: 0.0270 - 2s/epoch - 40ms/step
Epoch 46/100
39/39 - 1s - loss: 4.6839e-04 - root_mean_squared_error: 0.0216 - val_loss: 3.5365e-04 - val_root_mean_squared_error: 0.0188 - 1s/epoch - 38ms/step
Epoch 47/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 5.0270e-04 - root_mean_squared_error: 0.0224 - val_loss: 2.3472e-04 - val_root_mean_squared_error: 0.0153 - 8s/epoch - 198ms/step
Epoch 48/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 4.9879e-04 - root_mean_squared_error: 0.0223 - val_loss: 2.2919e-04 - val_root_mean_squared_error: 0.0151 - 7s/epoch - 187ms/step
Epoch 49/100
39/39 - 1s - loss: 6.0880e-04 - root_mean_squared_error: 0.0247 - val_loss: 2.5441e-04 - val_root_mean_squared_error: 0.0160 - 1s/epoch - 35ms/step
Epoch 50/100
39/39 - 1s - loss: 4.8530e-04 - root_mean_squared_error: 0.0220 - val_loss: 4.3853e-04 - val_root_mean_squared_error: 0.0209 - 1s/epoch - 36ms/step
Epoch 51/100
39/39 - 1s - loss: 4.6390e-04 - root_mean_squared_error: 0.0215 - val_loss: 4.6067e-04 - val_root_mean_squared_error: 0.0215 - 1s/epoch - 37ms/step
Epoch 52/100
39/39 - 1s - loss: 5.3167e-04 - root_mean_squared_error: 0.0231 - val_loss: 5.1467e-04 - val_root_mean_squared_error: 0.0227 - 1s/epoch - 35ms/step
Epoch 53/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 4.7277e-04 - root_mean_squared_error: 0.0217 - val_loss: 2.2480e-04 - val_root_mean_squared_error: 0.0150 - 7s/epoch - 189ms/step
Epoch 54/100
39/39 - 1s - loss: 4.1854e-04 - root_mean_squared_error: 0.0205 - val_loss: 7.9601e-04 - val_root_mean_squared_error: 0.0282 - 1s/epoch - 37ms/step
Epoch 55/100
39/39 - 1s - loss: 5.1893e-04 - root_mean_squared_error: 0.0228 - val_loss: 2.3950e-04 - val_root_mean_squared_error: 0.0155 - 1s/epoch - 37ms/step
Epoch 56/100
39/39 - 1s - loss: 5.3716e-04 - root_mean_squared_error: 0.0232 - val_loss: 3.0770e-04 - val_root_mean_squared_error: 0.0175 - 1s/epoch - 37ms/step
Epoch 57/100
39/39 - 2s - loss: 4.5817e-04 - root_mean_squared_error: 0.0214 - val_loss: 7.7138e-04 - val_root_mean_squared_error: 0.0278 - 2s/epoch - 40ms/step
Epoch 58/100
39/39 - 1s - loss: 4.6247e-04 - root_mean_squared_error: 0.0215 - val_loss: 2.4875e-04 - val_root_mean_squared_error: 0.0158 - 1s/epoch - 37ms/step
Epoch 59/100
39/39 - 1s - loss: 4.4638e-04 - root_mean_squared_error: 0.0211 - val_loss: 8.4840e-04 - val_root_mean_squared_error: 0.0291 - 1s/epoch - 37ms/step
Epoch 60/100
39/39 - 1s - loss: 4.4395e-04 - root_mean_squared_error: 0.0211 - val_loss: 3.6101e-04 - val_root_mean_squared_error: 0.0190 - 1s/epoch - 38ms/step
Epoch 61/100
39/39 - 2s - loss: 4.2642e-04 - root_mean_squared_error: 0.0206 - val_loss: 0.0011 - val_root_mean_squared_error: 0.0337 - 2s/epoch - 39ms/step
Epoch 62/100
39/39 - 1s - loss: 4.8754e-04 - root_mean_squared_error: 0.0221 - val_loss: 2.8713e-04 - val_root_mean_squared_error: 0.0169 - 1s/epoch - 36ms/step
Epoch 63/100
39/39 - 1s - loss: 4.2709e-04 - root_mean_squared_error: 0.0207 - val_loss: 2.6586e-04 - val_root_mean_squared_error: 0.0163 - 1s/epoch - 36ms/step
Epoch 64/100
39/39 - 2s - loss: 4.0785e-04 - root_mean_squared_error: 0.0202 - val_loss: 2.4535e-04 - val_root_mean_squared_error: 0.0157 - 2s/epoch - 39ms/step
Epoch 65/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 7s - loss: 5.1455e-04 - root_mean_squared_error: 0.0227 - val_loss: 1.9814e-04 - val_root_mean_squared_error: 0.0141 - 7s/epoch - 188ms/step
Epoch 66/100
39/39 - 2s - loss: 5.5639e-04 - root_mean_squared_error: 0.0236 - val_loss: 0.0012 - val_root_mean_squared_error: 0.0347 - 2s/epoch - 40ms/step
Epoch 67/100
39/39 - 1s - loss: 3.8347e-04 - root_mean_squared_error: 0.0196 - val_loss: 2.4404e-04 - val_root_mean_squared_error: 0.0156 - 1s/epoch - 38ms/step
Epoch 68/100
39/39 - 1s - loss: 3.4551e-04 - root_mean_squared_error: 0.0186 - val_loss: 2.0833e-04 - val_root_mean_squared_error: 0.0144 - 1s/epoch - 37ms/step
Epoch 69/100
39/39 - 1s - loss: 3.6102e-04 - root_mean_squared_error: 0.0190 - val_loss: 9.6031e-04 - val_root_mean_squared_error: 0.0310 - 1s/epoch - 38ms/step
Epoch 70/100
39/39 - 2s - loss: 3.7305e-04 - root_mean_squared_error: 0.0193 - val_loss: 5.2987e-04 - val_root_mean_squared_error: 0.0230 - 2s/epoch - 41ms/step
Epoch 71/100
39/39 - 1s - loss: 3.7501e-04 - root_mean_squared_error: 0.0194 - val_loss: 2.4603e-04 - val_root_mean_squared_error: 0.0157 - 1s/epoch - 37ms/step
Epoch 72/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 3.9527e-04 - root_mean_squared_error: 0.0199 - val_loss: 1.8789e-04 - val_root_mean_squared_error: 0.0137 - 8s/epoch - 196ms/step
Epoch 73/100
39/39 - 2s - loss: 3.3432e-04 - root_mean_squared_error: 0.0183 - val_loss: 3.7950e-04 - val_root_mean_squared_error: 0.0195 - 2s/epoch - 39ms/step
Epoch 74/100
39/39 - 2s - loss: 3.4911e-04 - root_mean_squared_error: 0.0187 - val_loss: 3.3700e-04 - val_root_mean_squared_error: 0.0184 - 2s/epoch - 39ms/step
Epoch 75/100
39/39 - 2s - loss: 3.5969e-04 - root_mean_squared_error: 0.0190 - val_loss: 2.4918e-04 - val_root_mean_squared_error: 0.0158 - 2s/epoch - 40ms/step
Epoch 76/100
39/39 - 2s - loss: 3.6128e-04 - root_mean_squared_error: 0.0190 - val_loss: 4.1308e-04 - val_root_mean_squared_error: 0.0203 - 2s/epoch - 39ms/step
Epoch 77/100
39/39 - 2s - loss: 3.7716e-04 - root_mean_squared_error: 0.0194 - val_loss: 2.5055e-04 - val_root_mean_squared_error: 0.0158 - 2s/epoch - 39ms/step
Epoch 78/100
39/39 - 2s - loss: 3.4802e-04 - root_mean_squared_error: 0.0187 - val_loss: 3.0118e-04 - val_root_mean_squared_error: 0.0174 - 2s/epoch - 40ms/step
Epoch 79/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 3.3579e-04 - root_mean_squared_error: 0.0183 - val_loss: 1.8117e-04 - val_root_mean_squared_error: 0.0135 - 8s/epoch - 197ms/step
Epoch 80/100
39/39 - 2s - loss: 3.6188e-04 - root_mean_squared_error: 0.0190 - val_loss: 1.8252e-04 - val_root_mean_squared_error: 0.0135 - 2s/epoch - 43ms/step
Epoch 81/100
39/39 - 2s - loss: 3.6420e-04 - root_mean_squared_error: 0.0191 - val_loss: 5.6658e-04 - val_root_mean_squared_error: 0.0238 - 2s/epoch - 45ms/step
Epoch 82/100
39/39 - 2s - loss: 3.6607e-04 - root_mean_squared_error: 0.0191 - val_loss: 4.9536e-04 - val_root_mean_squared_error: 0.0223 - 2s/epoch - 43ms/step
Epoch 83/100
39/39 - 2s - loss: 3.3888e-04 - root_mean_squared_error: 0.0184 - val_loss: 4.0198e-04 - val_root_mean_squared_error: 0.0200 - 2s/epoch - 43ms/step
Epoch 84/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 3.3356e-04 - root_mean_squared_error: 0.0183 - val_loss: 1.7437e-04 - val_root_mean_squared_error: 0.0132 - 8s/epoch - 199ms/step
Epoch 85/100
39/39 - 2s - loss: 2.9468e-04 - root_mean_squared_error: 0.0172 - val_loss: 3.1526e-04 - val_root_mean_squared_error: 0.0178 - 2s/epoch - 46ms/step
Epoch 86/100
39/39 - 2s - loss: 2.8713e-04 - root_mean_squared_error: 0.0169 - val_loss: 2.0257e-04 - val_root_mean_squared_error: 0.0142 - 2s/epoch - 52ms/step
Epoch 87/100
39/39 - 2s - loss: 3.1224e-04 - root_mean_squared_error: 0.0177 - val_loss: 2.9093e-04 - val_root_mean_squared_error: 0.0171 - 2s/epoch - 47ms/step
Epoch 88/100
39/39 - 2s - loss: 3.0469e-04 - root_mean_squared_error: 0.0175 - val_loss: 4.8967e-04 - val_root_mean_squared_error: 0.0221 - 2s/epoch - 46ms/step
Epoch 89/100
39/39 - 2s - loss: 2.9810e-04 - root_mean_squared_error: 0.0173 - val_loss: 1.8899e-04 - val_root_mean_squared_error: 0.0137 - 2s/epoch - 47ms/step
Epoch 90/100
39/39 - 2s - loss: 2.9972e-04 - root_mean_squared_error: 0.0173 - val_loss: 3.3795e-04 - val_root_mean_squared_error: 0.0184 - 2s/epoch - 45ms/step
Epoch 91/100
39/39 - 2s - loss: 3.0353e-04 - root_mean_squared_error: 0.0174 - val_loss: 2.0795e-04 - val_root_mean_squared_error: 0.0144 - 2s/epoch - 46ms/step
Epoch 92/100
39/39 - 2s - loss: 3.6997e-04 - root_mean_squared_error: 0.0192 - val_loss: 2.9944e-04 - val_root_mean_squared_error: 0.0173 - 2s/epoch - 48ms/step
Epoch 93/100
39/39 - 2s - loss: 3.0995e-04 - root_mean_squared_error: 0.0176 - val_loss: 1.8726e-04 - val_root_mean_squared_error: 0.0137 - 2s/epoch - 46ms/step
Epoch 94/100
39/39 - 2s - loss: 2.9663e-04 - root_mean_squared_error: 0.0172 - val_loss: 3.6128e-04 - val_root_mean_squared_error: 0.0190 - 2s/epoch - 46ms/step
Epoch 95/100
39/39 - 2s - loss: 2.8397e-04 - root_mean_squared_error: 0.0169 - val_loss: 2.9652e-04 - val_root_mean_squared_error: 0.0172 - 2s/epoch - 47ms/step
Epoch 96/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 2.7671e-04 - root_mean_squared_error: 0.0166 - val_loss: 1.7058e-04 - val_root_mean_squared_error: 0.0131 - 8s/epoch - 193ms/step
Epoch 97/100
WARNING:absl:Found untraced functions such as _update_step_xla, lstm_cell_layer_call_fn, lstm_cell_layer_call_and_return_conditional_losses, lstm_cell_1_layer_call_fn, lstm_cell_1_layer_call_and_return_conditional_losses while saving (showing 5 of 5). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: ysp_models\assets
INFO:tensorflow:Assets written to: ysp_models\assets
39/39 - 8s - loss: 3.0074e-04 - root_mean_squared_error: 0.0173 - val_loss: 1.6961e-04 - val_root_mean_squared_error: 0.0130 - 8s/epoch - 208ms/step
Epoch 98/100
39/39 - 1s - loss: 2.9828e-04 - root_mean_squared_error: 0.0173 - val_loss: 1.9891e-04 - val_root_mean_squared_error: 0.0141 - 1s/epoch - 36ms/step
Epoch 99/100
39/39 - 1s - loss: 2.7318e-04 - root_mean_squared_error: 0.0165 - val_loss: 1.9371e-04 - val_root_mean_squared_error: 0.0139 - 1s/epoch - 36ms/step
Epoch 100/100
39/39 - 1s - loss: 2.8705e-04 - root_mean_squared_error: 0.0169 - val_loss: 3.7780e-04 - val_root_mean_squared_error: 0.0194 - 1s/epoch - 35ms/step
Out[ ]:
<keras.callbacks.History at 0x2a80ca51a20>

load the saved model¶

In [ ]:
model = load_model('ysp_models/')
In [ ]:
predicted_stock_price = model.predict(X_test)

# invert predictions
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
y_test = sc.inverse_transform([y_test])
y_test = y_test.reshape(-1, 1)
7/7 [==============================] - 1s 13ms/step
In [ ]:
plt.plot(y_test, color = "red", label = "Real Stock Price")
plt.plot(predicted_stock_price, color = "blue", label = "Predicted Stock Price")
plt.title("Yahoo Stock Price Prediction")
plt.xlabel("Time")
plt.ylabel("Stock Price")
plt.legend()
plt.show()
No description has been provided for this image

Mean Squared Error¶

In [ ]:
mse = mean_squared_error(y_test, predicted_stock_price)
print(f"MSE === {mse}")
MSE === 1497.1658832307894