Importing all the libraries and the dataset of the tesla
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
df = pd.read_csv('TSLA.csv')
df.head()
| Date | Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|---|
| 0 | 2019-12-26 | 85.582001 | 86.695999 | 85.269997 | 86.188004 | 86.188004 | 53169500 |
| 1 | 2019-12-27 | 87.000000 | 87.061996 | 85.222000 | 86.075996 | 86.075996 | 49728500 |
| 2 | 2019-12-30 | 85.758003 | 85.800003 | 81.851997 | 82.940002 | 82.940002 | 62932000 |
| 3 | 2019-12-31 | 81.000000 | 84.258003 | 80.416000 | 83.666000 | 83.666000 | 51428500 |
| 4 | 2020-01-02 | 84.900002 | 86.139999 | 84.342003 | 86.052002 | 86.052002 | 47660500 |
Changing the index to date column to visulaise the line chart
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date',inplace=True)
df.head()
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2019-12-26 | 85.582001 | 86.695999 | 85.269997 | 86.188004 | 86.188004 | 53169500 |
| 2019-12-27 | 87.000000 | 87.061996 | 85.222000 | 86.075996 | 86.075996 | 49728500 |
| 2019-12-30 | 85.758003 | 85.800003 | 81.851997 | 82.940002 | 82.940002 | 62932000 |
| 2019-12-31 | 81.000000 | 84.258003 | 80.416000 | 83.666000 | 83.666000 | 51428500 |
| 2020-01-02 | 84.900002 | 86.139999 | 84.342003 | 86.052002 | 86.052002 | 47660500 |
According to the exploratory data analysis, the data has a shape of (504, 6), meaning that the dataset contains 506 rows and 6 columns. Furthermore, all of the columns have a float data type, except for the column "volume," it has an integer data type. Furthermore, in the last two years, the highest stock value is 1229.92 and the lowest is 72.2.
def EDA(x):
print(x.shape)
print('*'*50)
print(x.info())
print('*'*50)
print(x.describe())
EDA(df)
(504, 6)
**************************************************
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 504 entries, 2019-12-26 to 2021-12-23
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Open 504 non-null float64
1 High 504 non-null float64
2 Low 504 non-null float64
3 Close 504 non-null float64
4 Adj Close 504 non-null float64
5 Volume 504 non-null int64
dtypes: float64(5), int64(1)
memory usage: 27.6 KB
None
**************************************************
Open High ... Adj Close Volume
count 504.000000 504.000000 ... 504.000000 5.040000e+02
mean 524.973616 536.834630 ... 525.537733 5.174611e+07
std 293.116894 298.880181 ... 293.459521 3.802533e+07
min 74.940002 80.972000 ... 72.244003 9.800600e+06
25% 201.125004 203.827999 ... 201.600498 2.436525e+07
50% 601.644989 613.274994 ... 599.204986 3.904170e+07
75% 718.040024 731.750000 ... 718.569992 7.081100e+07
max 1234.410034 1243.489990 ... 1229.910034 3.046940e+08
[8 rows x 6 columns]
There are 0 null values and 0 duplicate values. Even though the values are duplicated, We cannot eliminate them since the stock value can be the same on a future day.
def missing_values(x):
print(x.isnull().sum())
print(x.duplicated().sum())
missing_values(df)
Open 0 High 0 Low 0 Close 0 Adj Close 0 Volume 0 dtype: int64 0
Visualizing the column "Close" since the column "close" would be the final price of the stock price for everyday. Additionally, the graph meantion that the stock value has a linear treand from the month January 2020 to febrauary 2021 in between it has small noise and dipped down in the month of march and had large noise had a linear trend till October 2021 and raised in November in the year 2021.
from matplotlib.pyplot import figure
plt.figure(figsize=(16,8))
plt.title('Close Price History')
plt.plot(df['Close'], color='red')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price USD', fontsize = 18)
plt.show()
The heatmap tells that every column has an autocorelation since every value is nearby 1.
sns.heatmap(df.corr(), annot = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7f382759a150>
df1=df.reset_index()['Close']
df1.head()
0 86.188004 1 86.075996 2 82.940002 3 83.666000 4 86.052002 Name: Close, dtype: float64
df1.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x7f38274f8f10>
LSTM is very much sensitive to the scale of the data. These particular dataset values are on a different scale. Hence, we are using minmaxscaler to scale the data in the range between 0 and 1
from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler(feature_range=(0,1))
df1=scaler.fit_transform(np.array(df1).reshape(-1,1))
df1.shape
(504, 1)
In Time-Series analysis it is very important to split the data since the data is specified in the date range. If we split the data with cross validation or random seed method, the training set would not have the sequence of the data and it would be impossible for us to predict the future. Additionally, every data of time series is dependent on the previous data. Hence, we need to split it by length of the data.
##splitting dataset into train and test split
training_size = int(len(df1)*0.70)
test_size = len(df1)- training_size
train_data,test_data = df1[0:training_size,:],df1[training_size:len(df1),:1]
print(train_data.shape)
print(test_data.shape)
(352, 1) (152, 1)
The above code gives me the perfect split of the initial 70% of the data as a training set and the below 30% of the data as a testing set. Now my shape of the training data is (352,1) and test data (152,1)
Data Pre-processing: In time-series, if we need to compute the next day data, we need to consider the n- number of previous days that need to be written as a time step. In our data set, I used time step = 10, which means I can find the pattern for the first 10 data and predict the 11th one. Secondly, the algorithm finds the patterns for the data 2 – 11 and predict the 12th one and go on.
# 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 np.array(dataX), np.array(dataY)
# reshape into X=t,t+1,t+2,t+3 and Y=t+4
time_step = 10
X_train, y_train = create_dataset(train_data, time_step)
X_test, ytest = create_dataset(test_data, time_step)
Additionally, we are adding the data into X_train, y_train, X_test and y_test with the method of the time step. Thus, the first 10 data would go to the X_train and the 11th would go to the y_train. This strategy is similar for testing datasets too.
Reshaping: Before implementing LSTM, we need to reshape all the values to 3 dimensional and the reshape input be the samples, time steps and features. We need to reshape the data because we give the LSTM input as time steps and features. Post reshaping my shape of the data as become X_train (341, 10, 1), X_test (141, 10, 1)
print(X_train.shape), print(y_train.shape)
print('*'*50)
print(X_test.shape), print(ytest.shape)
(341, 10) (341,) ************************************************** (141, 10) (141,)
(None, None)
# 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)
print(X_train.shape,X_test.shape)
(341, 10, 1) (141, 10, 1)
Model Building: The LSTM is the Long short term memory algorithm of recurrent neural network used in deep learning and it has feedback connections. LSTM consists of a cell, input gate and a forget gate. The three gates manage the flow of information into and out of the cell, and the cell remembers values across unlimited time intervals.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
To create a model on LSTM, we need to import Sequential, Dense and LSTM from the library tensorflow. We created the Sequential model with the LSTM neural network, with the 1st layer contains 50 hidden layers, and the 1st input shape should be your (X_train.shape[1],1) value which is (10,1) as per my model. Since it is a stacked LSTM model we used the 2nd and 3rd layers with 50 hidden layers. Finally, we have added the “Dense” layer which is the output. Finally, I have compiled the value with Mean squared error and optimizer I used is Adam.
model=Sequential()
model.add(LSTM(50,return_sequences=True,input_shape=(10,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: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 10, 50) 10400
lstm_1 (LSTM) (None, 10, 50) 20200
lstm_2 (LSTM) (None, 50) 20200
dense (Dense) (None, 1) 51
=================================================================
Total params: 50,851
Trainable params: 50,851
Non-trainable params: 0
_________________________________________________________________
The next step is to fit the model into the training data and considering my epochs =200. However, my 109th epoch gave me the minimal loss with the good forecasting LSTM model. The lower the rate of loss, the model works better.
model.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=120,batch_size=64,verbose=1)
Epoch 1/120 6/6 [==============================] - 7s 293ms/step - loss: 0.0772 - val_loss: 0.0717 Epoch 2/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0189 - val_loss: 0.0066 Epoch 3/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0111 - val_loss: 0.0411 Epoch 4/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0107 - val_loss: 0.0278 Epoch 5/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0041 - val_loss: 0.0053 Epoch 6/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0043 - val_loss: 0.0042 Epoch 7/120 6/6 [==============================] - 0s 36ms/step - loss: 0.0027 - val_loss: 0.0072 Epoch 8/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0021 - val_loss: 0.0053 Epoch 9/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0021 - val_loss: 0.0044 Epoch 10/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0017 - val_loss: 0.0038 Epoch 11/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0016 - val_loss: 0.0037 Epoch 12/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0016 - val_loss: 0.0043 Epoch 13/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0016 - val_loss: 0.0039 Epoch 14/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0016 - val_loss: 0.0036 Epoch 15/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0016 - val_loss: 0.0040 Epoch 16/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0016 - val_loss: 0.0038 Epoch 17/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0016 - val_loss: 0.0038 Epoch 18/120 6/6 [==============================] - 0s 37ms/step - loss: 0.0016 - val_loss: 0.0043 Epoch 19/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0016 - val_loss: 0.0039 Epoch 20/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0015 - val_loss: 0.0046 Epoch 21/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0016 - val_loss: 0.0040 Epoch 22/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0016 - val_loss: 0.0040 Epoch 23/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0016 - val_loss: 0.0042 Epoch 24/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0017 - val_loss: 0.0041 Epoch 25/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0015 - val_loss: 0.0055 Epoch 26/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0015 - val_loss: 0.0038 Epoch 27/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0015 - val_loss: 0.0051 Epoch 28/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0015 - val_loss: 0.0042 Epoch 29/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0015 - val_loss: 0.0041 Epoch 30/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0015 - val_loss: 0.0041 Epoch 31/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0015 - val_loss: 0.0044 Epoch 32/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0015 - val_loss: 0.0046 Epoch 33/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0015 - val_loss: 0.0045 Epoch 34/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0015 - val_loss: 0.0042 Epoch 35/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0015 - val_loss: 0.0048 Epoch 36/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0015 - val_loss: 0.0045 Epoch 37/120 6/6 [==============================] - 0s 28ms/step - loss: 0.0015 - val_loss: 0.0048 Epoch 38/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0015 - val_loss: 0.0044 Epoch 39/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0015 - val_loss: 0.0050 Epoch 40/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0014 - val_loss: 0.0045 Epoch 41/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0015 - val_loss: 0.0050 Epoch 42/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0015 - val_loss: 0.0047 Epoch 43/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0015 - val_loss: 0.0049 Epoch 44/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0014 - val_loss: 0.0046 Epoch 45/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0015 - val_loss: 0.0053 Epoch 46/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0015 - val_loss: 0.0046 Epoch 47/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0014 - val_loss: 0.0050 Epoch 48/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0014 - val_loss: 0.0046 Epoch 49/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0014 - val_loss: 0.0052 Epoch 50/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0014 - val_loss: 0.0045 Epoch 51/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0014 - val_loss: 0.0047 Epoch 52/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0014 - val_loss: 0.0052 Epoch 53/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0015 - val_loss: 0.0043 Epoch 54/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0015 - val_loss: 0.0057 Epoch 55/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0014 - val_loss: 0.0042 Epoch 56/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0014 - val_loss: 0.0048 Epoch 57/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0014 - val_loss: 0.0050 Epoch 58/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0014 - val_loss: 0.0064 Epoch 59/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0015 - val_loss: 0.0039 Epoch 60/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0015 - val_loss: 0.0078 Epoch 61/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0017 - val_loss: 0.0037 Epoch 62/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0015 - val_loss: 0.0066 Epoch 63/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0014 - val_loss: 0.0037 Epoch 64/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0014 - val_loss: 0.0058 Epoch 65/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0014 - val_loss: 0.0042 Epoch 66/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0013 - val_loss: 0.0061 Epoch 67/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0014 - val_loss: 0.0039 Epoch 68/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0014 - val_loss: 0.0067 Epoch 69/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0016 - val_loss: 0.0036 Epoch 70/120 6/6 [==============================] - 0s 36ms/step - loss: 0.0015 - val_loss: 0.0092 Epoch 71/120 6/6 [==============================] - 0s 37ms/step - loss: 0.0016 - val_loss: 0.0034 Epoch 72/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0016 - val_loss: 0.0074 Epoch 73/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0014 - val_loss: 0.0036 Epoch 74/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0015 - val_loss: 0.0057 Epoch 75/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0014 - val_loss: 0.0042 Epoch 76/120 6/6 [==============================] - 0s 36ms/step - loss: 0.0013 - val_loss: 0.0057 Epoch 77/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0013 - val_loss: 0.0037 Epoch 78/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0014 - val_loss: 0.0063 Epoch 79/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0014 - val_loss: 0.0037 Epoch 80/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0013 - val_loss: 0.0059 Epoch 81/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0014 - val_loss: 0.0039 Epoch 82/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0013 - val_loss: 0.0053 Epoch 83/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0013 - val_loss: 0.0042 Epoch 84/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0013 - val_loss: 0.0047 Epoch 85/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0013 - val_loss: 0.0045 Epoch 86/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0013 - val_loss: 0.0050 Epoch 87/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0013 - val_loss: 0.0041 Epoch 88/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0013 - val_loss: 0.0047 Epoch 89/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0013 - val_loss: 0.0049 Epoch 90/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0013 - val_loss: 0.0035 Epoch 91/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0013 - val_loss: 0.0050 Epoch 92/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0013 - val_loss: 0.0053 Epoch 93/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0015 - val_loss: 0.0031 Epoch 94/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0014 - val_loss: 0.0057 Epoch 95/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0013 - val_loss: 0.0052 Epoch 96/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0013 - val_loss: 0.0031 Epoch 97/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0013 - val_loss: 0.0068 Epoch 98/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0013 - val_loss: 0.0033 Epoch 99/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0013 - val_loss: 0.0048 Epoch 100/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0012 - val_loss: 0.0040 Epoch 101/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0012 - val_loss: 0.0045 Epoch 102/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0012 - val_loss: 0.0039 Epoch 103/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0012 - val_loss: 0.0065 Epoch 104/120 6/6 [==============================] - 0s 34ms/step - loss: 0.0013 - val_loss: 0.0036 Epoch 105/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0013 - val_loss: 0.0028 Epoch 106/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0013 - val_loss: 0.0061 Epoch 107/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0013 - val_loss: 0.0034 Epoch 108/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0011 - val_loss: 0.0048 Epoch 109/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0012 - val_loss: 0.0031 Epoch 110/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0012 - val_loss: 0.0028 Epoch 111/120 6/6 [==============================] - 0s 32ms/step - loss: 0.0013 - val_loss: 0.0061 Epoch 112/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0011 - val_loss: 0.0030 Epoch 113/120 6/6 [==============================] - 0s 38ms/step - loss: 0.0011 - val_loss: 0.0037 Epoch 114/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0012 - val_loss: 0.0055 Epoch 115/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0011 - val_loss: 0.0035 Epoch 116/120 6/6 [==============================] - 0s 31ms/step - loss: 0.0011 - val_loss: 0.0031 Epoch 117/120 6/6 [==============================] - 0s 30ms/step - loss: 0.0011 - val_loss: 0.0045 Epoch 118/120 6/6 [==============================] - 0s 35ms/step - loss: 0.0011 - val_loss: 0.0051 Epoch 119/120 6/6 [==============================] - 0s 33ms/step - loss: 0.0011 - val_loss: 0.0030 Epoch 120/120 6/6 [==============================] - 0s 29ms/step - loss: 0.0011 - val_loss: 0.0034
<keras.callbacks.History at 0x7f37b1b228d0>
Post finding the loss, we need to predict the model with X_train and X_test data and check how well the model has worked for this dataset.
# 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)
import math
from sklearn.metrics import mean_squared_error
math.sqrt(mean_squared_error(y_train,train_predict))
480.43697504178385
### Test Data RMSE
math.sqrt(mean_squared_error(ytest,test_predict))
795.6736758628309
### Plotting
# shift train predictions for plotting
look_back=10
trainPredictPlot = np.empty_like(df1)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
# shift test predictions for plotting
testPredictPlot = np.empty_like(df1)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(df1))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
len(test_data)
152
x_input = test_data[142:].reshape(1,-1)
x_input.shape
(1, 10)
temp_input = list(x_input)
temp_input = temp_input[0].tolist()
temp_input
[0.816112765426733, 0.7723868076422811, 0.7655627644480828, 0.7806620932112329, 0.7382750785748848, 0.7431556087525902, 0.7149695826222269, 0.748303917366994, 0.8090640710870093, 0.8592771752495172]
from numpy import array
lst_output=[]
n_steps=10
i=0
while(i<30):
if(len(temp_input)>10):
#print(temp_input)
x_input=np.array(temp_input[1:])
print("{} day input {}".format(i,x_input))
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))
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])
temp_input.extend(yhat[0].tolist())
print(len(temp_input))
lst_output.extend(yhat.tolist())
i=i+1
print(lst_output)
[0.71900856] 11 1 day input [0.77238681 0.76556276 0.78066209 0.73827508 0.74315561 0.71496958 0.74830392 0.80906407 0.85927718 0.71900856] 1 day output [[0.72514564]] 2 day input [0.76556276 0.78066209 0.73827508 0.74315561 0.71496958 0.74830392 0.80906407 0.85927718 0.71900856 0.72514564] 2 day output [[0.72663534]] 3 day input [0.78066209 0.73827508 0.74315561 0.71496958 0.74830392 0.80906407 0.85927718 0.71900856 0.72514564 0.72663534] 3 day output [[0.7243009]] 4 day input [0.73827508 0.74315561 0.71496958 0.74830392 0.80906407 0.85927718 0.71900856 0.72514564 0.72663534 0.72430092] 4 day output [[0.71983224]] 5 day input [0.74315561 0.71496958 0.74830392 0.80906407 0.85927718 0.71900856 0.72514564 0.72663534 0.72430092 0.71983224] 5 day output [[0.71244115]] 6 day input [0.71496958 0.74830392 0.80906407 0.85927718 0.71900856 0.72514564 0.72663534 0.72430092 0.71983224 0.71244115] 6 day output [[0.7043093]] 7 day input [0.74830392 0.80906407 0.85927718 0.71900856 0.72514564 0.72663534 0.72430092 0.71983224 0.71244115 0.70430928] 7 day output [[0.6949978]] 8 day input [0.80906407 0.85927718 0.71900856 0.72514564 0.72663534 0.72430092 0.71983224 0.71244115 0.70430928 0.69499779] 8 day output [[0.68691635]] 9 day input [0.85927718 0.71900856 0.72514564 0.72663534 0.72430092 0.71983224 0.71244115 0.70430928 0.69499779 0.68691635] 9 day output [[0.6816989]] 10 day input [0.71900856 0.72514564 0.72663534 0.72430092 0.71983224 0.71244115 0.70430928 0.69499779 0.68691635 0.68169892] 10 day output [[0.67947286]] 11 day input [0.72514564 0.72663534 0.72430092 0.71983224 0.71244115 0.70430928 0.69499779 0.68691635 0.68169892 0.67947286] 11 day output [[0.67415065]] 12 day input [0.72663534 0.72430092 0.71983224 0.71244115 0.70430928 0.69499779 0.68691635 0.68169892 0.67947286 0.67415065] 12 day output [[0.6690942]] 13 day input [0.72430092 0.71983224 0.71244115 0.70430928 0.69499779 0.68691635 0.68169892 0.67947286 0.67415065 0.6690942 ] 13 day output [[0.6644602]] 14 day input [0.71983224 0.71244115 0.70430928 0.69499779 0.68691635 0.68169892 0.67947286 0.67415065 0.6690942 0.66446018] 14 day output [[0.6602583]] 15 day input [0.71244115 0.70430928 0.69499779 0.68691635 0.68169892 0.67947286 0.67415065 0.6690942 0.66446018 0.66025829] 15 day output [[0.6564493]] 16 day input [0.70430928 0.69499779 0.68691635 0.68169892 0.67947286 0.67415065 0.6690942 0.66446018 0.66025829 0.65644932] 16 day output [[0.6529236]] 17 day input [0.69499779 0.68691635 0.68169892 0.67947286 0.67415065 0.6690942 0.66446018 0.66025829 0.65644932 0.65292358] 17 day output [[0.6496055]] 18 day input [0.68691635 0.68169892 0.67947286 0.67415065 0.6690942 0.66446018 0.66025829 0.65644932 0.65292358 0.64960551] 18 day output [[0.64640594]] 19 day input [0.68169892 0.67947286 0.67415065 0.6690942 0.66446018 0.66025829 0.65644932 0.65292358 0.64960551 0.64640594] 19 day output [[0.64330137]] 20 day input [0.67947286 0.67415065 0.6690942 0.66446018 0.66025829 0.65644932 0.65292358 0.64960551 0.64640594 0.64330137] 20 day output [[0.6403373]] 21 day input [0.67415065 0.6690942 0.66446018 0.66025829 0.65644932 0.65292358 0.64960551 0.64640594 0.64330137 0.64033729] 21 day output [[0.6375935]] 22 day input [0.6690942 0.66446018 0.66025829 0.65644932 0.65292358 0.64960551 0.64640594 0.64330137 0.64033729 0.63759351] 22 day output [[0.6349983]] 23 day input [0.66446018 0.66025829 0.65644932 0.65292358 0.64960551 0.64640594 0.64330137 0.64033729 0.63759351 0.63499832] 23 day output [[0.6325304]] 24 day input [0.66025829 0.65644932 0.65292358 0.64960551 0.64640594 0.64330137 0.64033729 0.63759351 0.63499832 0.63253039] 24 day output [[0.6301773]] 25 day input [0.65644932 0.65292358 0.64960551 0.64640594 0.64330137 0.64033729 0.63759351 0.63499832 0.63253039 0.63017732] 25 day output [[0.6279327]] 26 day input [0.65292358 0.64960551 0.64640594 0.64330137 0.64033729 0.63759351 0.63499832 0.63253039 0.63017732 0.62793273] 26 day output [[0.6257938]] 27 day input [0.64960551 0.64640594 0.64330137 0.64033729 0.63759351 0.63499832 0.63253039 0.63017732 0.62793273 0.62579381] 27 day output [[0.6237567]] 28 day input [0.64640594 0.64330137 0.64033729 0.63759351 0.63499832 0.63253039 0.63017732 0.62793273 0.62579381 0.62375671] 28 day output [[0.62181747]] 29 day input [0.64330137 0.64033729 0.63759351 0.63499832 0.63253039 0.63017732 0.62793273 0.62579381 0.62375671 0.62181747] 29 day output [[0.6199696]] [[0.7190085649490356], [0.7251456379890442], [0.7266353368759155], [0.7243009209632874], [0.7198322415351868], [0.7124411463737488], [0.7043092846870422], [0.6949977874755859], [0.6869163513183594], [0.6816989183425903], [0.6794728636741638], [0.6741506457328796], [0.6690942049026489], [0.6644601821899414], [0.6602582931518555], [0.6564493179321289], [0.652923583984375], [0.6496055126190186], [0.6464059352874756], [0.6433013677597046], [0.6403372883796692], [0.6375935077667236], [0.6349983215332031], [0.6325303912162781], [0.6301773190498352], [0.6279327273368835], [0.6257938146591187], [0.6237567067146301], [0.6218174695968628], [0.6199696063995361]]
day_new=np.arange(1,11)
day_pred=np.arange(11,41)
len(df1)
504
plt.plot(day_new,scaler.inverse_transform(df1[494:]))
plt.plot(day_pred,scaler.inverse_transform(lst_output))
[<matplotlib.lines.Line2D at 0x7f37ae6c4b90>]