[英]I am getting error('DataFrame' object has no attribute 'as_matrix') while running following code
Basic LSTM Model Import keras libraries基本 LSTM 模型导入 keras 库
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拆分训练和测试数据集并展开 lstm 模型的训练和测试数据
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'
I have wrongly import this sklearn.preprocessing.StandardScaler.我错误地导入了这个 sklearn.preprocessing.StandardScaler。 After removing this line of code, everything runs smooth
删除这行代码后,一切顺利
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.