Search code examples
pythonlstmprediction

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))

Solution

  • 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)