Lim
Lim

Reputation: 11

pandad reader :TypeError: only integer scalar arrays can be converted to a scalar index

I am facing an error on this, how to I solve it? I think its on the reshape part but I'm not sure about it.

    dataset = data.values
    print(len(dataset))

    training_data_size = math.ceil(len(dataset)*.7)
    scaler = MinMaxScaler(feature_range=(0,1))
    scaled_data = scaler.fit_transform(dataset)

    train_data = scaled_data[0:training_data_size,:]
    x_train = []
    y_train = []

    for i in range(60,len(train_data)):
        x_train.append(train_data[i-60:i,0])
        y_train.append(train_data[i,0])

    x_train,y_train = np.array(x_train),np.array(y_train)
    x_train = np.reshape(x_train,(x_train.shape[0],x_train[1],1))

The error is as below:

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Roaming\Python\Python36\site-packages\numpy\core\fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
TypeError: only integer scalar arrays can be converted to a scalar index

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/USER/PycharmProjects/Predict Stock Price/main.py", line 105, in <module>
    LSTM_predidction()
  File "C:/Users/USER/PycharmProjects/Predict Stock Price/main.py", line 72, in LSTM_predidction
    x_train = np.reshape(x_train,(x_train.shape[0],x_train[1],1))
  File "<__array_function__ internals>", line 6, in reshape
  File "C:\Users\USER\AppData\Roaming\Python\Python36\site-packages\numpy\core\fromnumeric.py", line 299, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "C:\Users\USER\AppData\Roaming\Python\Python36\site-packages\numpy\core\fromnumeric.py", line 67, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "C:\Users\USER\AppData\Roaming\Python\Python36\site-packages\numpy\core\fromnumeric.py", line 44, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: only integer scalar arrays can be converted to a scalar index

Process finished with exit code 1

Any suggestions would be appreciated

Upvotes: 0

Views: 111

Answers (1)

oussama Seffai
oussama Seffai

Reputation: 197

you are missing shape in the second element of tuple

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

correct :

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

Upvotes: 1

Related Questions