[英]I am getting error('DataFrame' object has no attribute 'as_matrix') while running following code
基本 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())
拆分訓練和測試數據集並展開 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)
功能定義
將數據集拆分為長短期記憶模型的訓練和測試特征
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
使用不同的窗口進行測試和訓練,以防止數據中的信息泄漏
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)
錯誤
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'
我錯誤地導入了這個 sklearn.preprocessing.StandardScaler。 刪除這行代碼后,一切順利
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.