Python ValueError:无法将输入数组从形状(90742,1)广播到形状(240742,1)

Python ValueError:无法将输入数组从形状(90742,1)广播到形状(240742,1),python,time-series,lstm,Python,Time Series,Lstm,我遇到ValueError:无法将输入数组从形状(90742,1)广播到形状(240742,1)。 这是我的密码: # shift train predictions for plotting train_predict_plot = np.empty_like(data) train_predict_plot[:, :] np.nan train_predict_plot[lags:len(train_predict)+lags, :] = train_predict # shift te

我遇到
ValueError:无法将输入数组从形状(90742,1)广播到形状(240742,1)。

这是我的密码:

# shift train predictions for plotting 
train_predict_plot = np.empty_like(data)
train_predict_plot[:, :] np.nan
train_predict_plot[lags:len(train_predict)+lags, :] = train_predict

# shift test predictions for plotting
test_predict_plot = np.empty_like(data)
test_predict_plot[:, :] = np.nan
test_predict_plot[len(train_predict) + (lags * 2)+1:len(data)-1, :] = test_predict

# plot observation and predictions 
plt.plot(data, label='Observed', color='#006699'); 
plt.plot(train_predict_plot, label='Prediction for Train Set', color='#006699', alpha=0.5); 
plt.plot(test_predict_plot, label='Prediction for Test Set', color='#ff0066'); 
plt.legend(loc='upper left') 
plt.title('LSTM Recurrent Neural Net') 
plt.show()
以下是错误:


我已经看了前面的问题,但它们都不同。有人能告诉我如何解决这个问题,特别是在我的情况下?非常感谢。

因此,您的
测试预测。形状必须是
(90742,1)
您所面临的错误,因为
测试预测图[240742,:]=test\u predict
这里的输入

test\u predict\u plot[75003:315745,:]
的形状变为
(240742,1)
,并输入一个比此形状更短或不同的数组将抛出错误

我试图通过运行您在链接中提供的代码来重现错误,但没有出现错误,原因如下:
len(train\u predict)=116
len(data)=144
len(test\u predict)=20
,这与代码的计算非常吻合。在这一行代码中,我所说的计算是指:

    test_predict_plot[len(train_predict)+(lags*2)+1:len(data)-1, :] = test_predict
这将成为:

    test_predict_plot[116+6+1:144-1, :] = test_predict 
因此,您需要对代码进行更改,以使输入的长度匹配。 您也可以通过以下代码更改尝试:

    test_predict_plot[<len(test_predict)>, :] = test_predict
您需要在此处进行更改:

    train_predict_plot = np.empty_like(data[150000:, :])
    train_predict_plot[:, :] = np.nan
    train_predict_plot[lags: len(train_predict) + lags, :] = train_predict

    # shift test predictions for plotting
    test_predict_plot = np.empty_like(data[150000:, :])
    test_predict_plot[:, :] = np.nan
    test_predict_plot[len(train_predict)+(lags*2)+1:len(data[150000:, :])-1, :] = test_predict

你真的应该把你的代码放在你的问题里面,并且正确地格式化它,而不是仅仅发布一张图片。人们更倾向于这样帮助你。数据的形状是什么?我认为数据的长度小于列车的长度,这就是为什么它是抛出错误的原因。通过
len(data)
len(train\u predict)
进行检查,您是如何定义
滞后的?@rahlf23我试着发布我的代码。。。但是有一个缩进错误。我稍后会设法解决这个问题。我在这个网站上用LSTM代码跟踪了时间序列:是的,lags=3谢谢你的回复,发布你的代码,如果缩进错误存在,人们可以帮助解决,但是你真的应该发布重现问题所需的所有代码(即所需的所有变量),我还不明白。怎么能让你看看我的全部代码?@mattcoder79我已经编辑了答案。你现在能理解了吗?@mattcoder79如果你能把你的代码推到git上,那就告诉我。@mattcoder79发现了问题:
train=dataset[150000:225000,:]test=dataset[225000:,:]
你需要在这里做些修改:
train\u predict\u plot=np.empty\u like(data[150000:,:])train\u predict\u plot[:,:]=np.nan训练预测图[lags:len(训练预测)+lags,:]=train#U预测图的移位测试预测绘制测试预测图=np.empty#like(数据[150000:,:])测试预测图[:,:]=np.nan测试预测图[len(训练预测)+(lags*2)+1:len([150000:,:)-1,:]=测试预测
    train_predict_plot = np.empty_like(data[150000:, :])
    train_predict_plot[:, :] = np.nan
    train_predict_plot[lags: len(train_predict) + lags, :] = train_predict

    # shift test predictions for plotting
    test_predict_plot = np.empty_like(data[150000:, :])
    test_predict_plot[:, :] = np.nan
    test_predict_plot[len(train_predict)+(lags*2)+1:len(data[150000:, :])-1, :] = test_predict