'Trying to copy a LSTM model - it's not working
I'm trying to copy a LSTM model that I found from here: Stock Market-Predict volume with LSTM model
I'm getting stuck on the last line of code. Specifically, this is what it tells me:
I literally know next to nothing about code just basic python.
If you want to, I can add you to like the google drive collaboratory so you could look at the whole code.
Solution 1:[1]
The arrays aren't the same size, which is required for the pyplot.plot function to work. So just cut less items out of the data array like so from:
plt.plot(data.index[-640:], test_y, color='blue',label='Actual')
plt.plot(data.index[-640:], yPredict, alpha=0.7, color='red',label='Predict')
to
plt.plot(data.index[-300:], test_y[-300:], color='blue',label='Actual')
plt.plot(data.index[-300:], yPredict[-300:], alpha=0.7, color='red',label='Predict')
just to explain this even more what its saying is that the size of the arrays aren't the same, so what I did was cut out the items equally + less because it said that test_y had 372 items, however I don't know if this is the same for yPredict, it could be even smaller. good luck.
Solution 2:[2]
To add to the other answer, the train and test sets are built here:
num = int(data_reframed.shape[0]*0.8)
value = data_reframed.values
train = value[:num, :]
test = value[num:, :]
train_x, train_y = train[:, :-1], train[:, -1]
test_x, test_y = test[:, :-1], test[:, -1]
While yPredict is calculated here:
yPredict = Model.predict(test_x)
Hence, the shape of yPredict
and test_y
has to be the same, and that has to be (data_reframed.shape[0] - num,)
, since num
is the size of the training set. Then, due to series_to_supervised?,
datahas one row more than
data_reframed`. So, you can write:
plt.plot(data.index[num+1:], test_y, color='blue',label='Actual')
plt.plot(data.index[num+1:], yPredict, alpha=0.7, color='red',label='Predict')
And plot everything.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sebastian Bilek |
Solution 2 |