简体   繁体   English

运行以下代码时出现错误('DataFrame' 对象没有属性 'as_matrix')

[英]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.

相关问题 'DataFrame' object 没有属性 'as_matrix - 'DataFrame' object has no attribute 'as_matrix AttributeError: &#39;Series&#39; 对象没有属性 &#39;as_matrix&#39; 为什么会出错? - AttributeError: 'Series' object has no attribute 'as_matrix' Why is it error? 对于 python 中的以下代码,我收到错误--AttributeError: 'str' object has no attribute 'next' - For the following code in python, I am getting the error--AttributeError: 'str' object has no attribute 'next' 运行脚本时出现属性错误(AttributeError: &#39;super&#39; object has no attribute &#39;__getattr__&#39;) - I am getting an attribute error ( AttributeError: 'super' object has no attribute '__getattr__') while running my script 运行bemoss时出错,我得到AttributeError:&#39;gevent._event.AsyncResult&#39;对象没有属性&#39;ident&#39; - Error while running the bemoss, I am getting AttributeError: 'gevent._event.AsyncResult' object has no attribute 'ident' 我在 python 中运行下面的代码并收到错误 &#39;AttributeError: &#39;QgridWidget&#39; object has no attribute &#39;to_excel&#39;&#39; - I am running the code below in python and getting the error 'AttributeError: 'QgridWidget' object has no attribute 'to_excel'' 运行以下python代码时,出现以下错误“对于f(list)中的行:TypeError:&#39;_io.TextIOWrapper&#39;对象不可调用” - I am getting the following error“for line in f(list): TypeError: '_io.TextIOWrapper' object is not callable” while running the below python code 尝试使用 Python 和 OpenCV 加载图像时,我在以下代码中收到“属性错误”? - I am getting 'Attribute Error' in the following code while tried loading an image, using Python & OpenCV? 我在Boa构造函数GUI基础python程序中运行以下代码时遇到“文件”对象没有属性“ __getitem__”的错误 - i am getting an error of 'file' object has no attribute '__getitem__' when i run the following code in Boa constructor GUI base python program 在编译像这样的“NoneType”对象没有属性“find_all”的代码时出现错误 - In am getting error while compiling code like this 'NoneType' object has no attribute 'find_all'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM