简体   繁体   中英

AttributeError: 'list' object has no attribute 'shape'?

can somebody help me, I've been trying to run the script below

X_train = np.reshape(X_train, newshape = (X_train.shape[0], X_train.shape[1], 1))

but only to have an error like this

AttributeError Traceback (most recent call last)<ipython-input-48-9880c2146f81> in <module>()----> 1 X_train = np.reshape(X_train, newshape = (X_train.shape[0], X_train.shape[1], 1))
AttributeError: 'list' object has no attribute 'shape'

and here is the full script

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

downloads = pd.read_csv('Datatraining.csv')
training_set = downloads.iloc[:, 1:2].values

from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)

X_train = []
y_train = []
for i in range(72, len(training_set_scaled)):
    X_train.append(training_set_scaled[i-60: i, 0])
    y_train.append(training_set_scaled[i, 0])
    X_train, y_train = np.array(X_train), np.array(y_train)

X_train = np.reshape(X_train, newshape = (X_train.shape[0], X_train.shape[1], 1))

Your X_train is not a np array but a list . You first have to convert your list to a numpy array

X_train = np.asarray(X_train)

I had a similar error, I managed to solve it by changing everywhere X_train to x_train

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM