gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:00:07 25 4
gpt4 key购买 nike

我正在尝试将 LSTM 模型拟合到我的数据集上。当我运行代码时,它给出 ValueError: 形状为 (879,1) 的不可广播输出操作数与广播形状 (879,18) 不匹配。

Traceback (most recent call last):
File "C:/Users/SCS/Desktop/RNN/RecurentNueralNetwork.py", line 54, in <module>
trainPredict = scaler.inverse_transform(trainPredict)
File "C:\Users\SCS\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 406, in inverse_transform
X -= self.min_
ValueError: non-broadcastable output operand with shape (879,1) doesn't match the broadcast shape (879,18)

代码在这里:

# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(look_back, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=4, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX) // here is error
testPredict = model.predict(testX)
print("Train predict shape",testPredict.shape)
# invert

trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])

最佳答案

请在第 54 行之前尝试以下几行:

train_predict_new = np.zeros(shape=(len(train_predict), 18))
train_predict_new[:,0] = train_predict[:,0]
trainPredict = scaler.inverse_transform(train_predict_new )[:,0]

希望这有帮助。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com