python - ValueError : non-broadcastable output operand with shape (11599, 1) 与广播形状 (11599,7) 不匹配

标签 python numpy scikit-learn array-broadcasting

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

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)

上面的文本显示了我收到的输出错误。我认为这与我 reshape 数据的方式有关。我正在尝试让 LSTM 来预测数据,但在 reshape 数据时遇到问题。

最佳答案

这里,当您尝试创建一个具有 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)) 

关于python - ValueError : non-broadcastable output operand with shape (11599, 1) 与广播形状 (11599,7) 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58807393/

相关文章:

python - AzureML列出大量文件

python - 如何在 scikit-learn 中正确执行交叉验证?

python - 如何获得分类模型的预测概率?

python: 是否可以检测我是否在 Paster shell 中?

python - 如何比较python opencv2.4中的surf特征

类似于 Python Pylons 的 PHP 框架

python - 在具有最小最近邻距离和最大密度的 3D 空间中随机采样给定点

python - 如何有效地将方形 numpy 数组旋转不同时间 `np.rot90` ?

python - pandas.Series operator/= 在 Python 2 和 Python 3 中的不同行为

Python:为什么我的线性回归图给出了许多凌乱的彩色线?