【问题标题】:ValueError: non-broadcastable output operand with shape (11599,1) doesn't match the broadcast shape (11599,7)ValueError:形状 (11599,1) 的不可广播输出操作数与广播形状 (11599,7) 不匹配
【发布时间】:2020-03-07 11:55:12
【问题描述】:

为什么会这样?解决方法是什么?我在发布之前尝试过搜索,但我没有找到为什么我的代码会发生这种情况。如果有人能看到它,那就太棒了。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
from sklearn import preprocessing
from sklearn.preprocessing import MinMaxScaler

dataset = pd.read_csv("msft.us.txt").fillna(0)
le = preprocessing.LabelEncoder()
dataset['Date'] = le.fit_transform(dataset['Date'])
train = dataset[:6386]
valid = dataset[6386:]

#converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)

x_train = []
y_train = []
for i in range(60,len(train)):
    x_train.append(scaled_data[i-60:i,0])
    y_train.append(scaled_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.shape[1],1))


model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=1, batch_size=16)

inputs = dataset[len(dataset) - len(valid) - 60:].values
inputs = inputs.reshape(-1,1)
inputs  = scaler.transform(inputs)

X_test = []
for i in range(60,inputs.shape[0]):
    X_test.append(inputs[i-60:i,0])
X_test = np.array(X_test)

X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
closing_price = model.predict(X_test)
closing_price = scaler.inverse_transform(closing_price)
dataset['Date'] = le.inverse_transform(dataset['Date'])
valid['Predictions'] = closing_price
plt.plot(train['Close'])
plt.plot(valid[['Close','Predictions']])

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Anthony/Desktop/Machine Learning/Machine Learning Final Project/RNN for Microsft Stock.py", line 39, in <module>
    inputs  = scaler.transform(inputs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 389, in transform
    X *= self.scale_

ValueError: non-broadcastable output operand with shape (11599,1) doesn't match the broadcast shape (11599,7)

上面的文字显示了我收到的输出错误。我认为这与我如何重塑数据有关。我正在尝试让 LSTM 来预测数据,但我在重塑数据时遇到了麻烦。

【问题讨论】:

    标签: python numpy scikit-learn array-broadcasting


    【解决方案1】:

    在这里,当您尝试创建具有 60 个时间戳和 1 个输出的数据结构时,您在 for 循环中犯了一个错误。在这里,您缩放了数据集,然后将其命名为 scaled_data。所以

    x_train = [] 
    y_train = [] 
    for i in range(60,len(scaled_data)): 
         x_train.append(scaled_data[i-60:i,0]) 
         y_train.append(scaled_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.shape[1],1)) 
    

    【讨论】:

      猜你喜欢
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2020-08-03
      • 1970-01-01
      相关资源
      最近更新 更多