Basic LSTM Model Import keras libraries
import math
import pandas as pd
import numpy as np
from IPython.display import display
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.metrics import mean_squared_error
from sklearn.model_selection import StratifiedKFold
import lstm, time #helper libraries
import visualize as vs
import stock_data as sd
import LinearRegressionModel
stocks = pd.read_csv('E:/DBSOM DATA\FOM_Sem 2/Analyses of S&U Data/Project work/Stock-Price-Prediction-master/google_preprocessed.csv')
stocks_data = stocks.drop(['Item'], axis =1)
display(stocks_data.head())
Spliting train and test data sets and Unroll train and test data for lstm model
X_train, X_test,y_train, y_test = sd.train_test_split_lstm(stocks_data, 5)
unroll_length = 50
X_train = sd.unroll(X_train, unroll_length)
X_test = sd.unroll(X_test, unroll_length)
y_train = y_train[-X_train.shape[0]:]
y_test = y_test[-X_test.shape[0]:]
print("x_train", X_train.shape)
print("y_train", y_train.shape)
print("x_test", X_test.shape)
print("y_test", y_test.shape)
Function definition
Split the data set into training and testing feature for Long Short Term Memory Model
def train_test_split_lstm(stocks, prediction_time=1, test_data_size=450, unroll_length=50):
# training data
test_data_cut = test_data_size + unroll_length + 1
x_train = stocks[0:-prediction_time - test_data_cut].values
y_train = stocks[prediction_time:-test_data_cut]['Close'].values
# test data
x_test = stocks[0 - test_data_cut:-prediction_time].values
y_test = stocks[prediction_time - test_data_cut:]['Close'].values
return x_train, x_test, y_train, y_test
Use different windows for testing and training to stop from leak of information in the data
def unroll(data, sequence_length=24):
result = []
for index in range(len(data) - sequence_length):
result.append(data[index: index + sequence_length])
return np.asarray(result)
Error
AttributeError Traceback (most recent call last)
<ipython-input-52-59aa6ad29ad5> in <module>
----> 1 X_train, X_test,y_train, y_test = sd.train_test_split_lstm(stocks_data, 5)
2
3 unroll_length = 50
4 X_train = sd.unroll(X_train, unroll_length)
5 X_test = sd.unroll(X_test, unroll_length)
~\Stock Price Prediction\stock_data.py in train_test_split_lstm(stocks, prediction_time, test_data_size, unroll_length)
77 test_data_cut = test_data_size + unroll_length + 1
78
---> 79 x_train = stocks[0:-prediction_time - test_data_cut].to_numpy()
80 y_train = stocks[prediction_time:-test_data_cut]['Close'].to_numpy()
81
~\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5128 if
self._info_axis._can_hold_identifiers_and_holds_name(name):
5129 return self[name]
-> 5130 return object.__getattribute__(self, name)
5131
5132 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'as_matrix'