VivekPa / IntroNeuralNetworks

Introducing neural networks to predict stock prices
MIT License
735 stars 205 forks source link

Getting this error while running as it is. #3

Open sampatel012 opened 5 years ago

sampatel012 commented 5 years ago

[*100%***] 1 of 1 downloaded Traceback (most recent call last): File "LSTM_model.py", line 37, in X_predict = np.array(stock).reshape((1, 10, 1)) / 200 ValueError: cannot reshape array of size 9 into shape (1,10,1)

alexw92 commented 5 years ago

You could just add another (trading) day to the time interval by modifying either the start or end date. Then it should fit the shape.

Chudvan commented 6 months ago

The problem occurs because you are trying to change the shape of the stock array to (1, 10, 1), but the size of the stock array is 9, which is incompatible with the required shape (1, 10, 1).

Your code uses a stock array that contains stock price data. In this particular case, it is initialized as follows:

data = pdr.get_data_yahoo("AAPL", "2017-12-19", "2018-01-03")
stock = data["Adj Close"]

The error occurs because the stock price data retrieved by pdr.get_data_yahoo only contains 9 elements, and your code is trying to change the shape of this array to (1, 10, 1), which requires 10 elements.

To fix this, you can change the size of the data obtained from Yahoo Finance so that the number of data is 10. For example, you can change the date range to get more data.

Here's an example fix:

data = pdr.get_data_yahoo("AAPL", "2017-12-18", "2018-01-04") # Change the date range
stock = data["Adj Close"]

Now stock contains 10 days worth of stock price data and the code should work without errors.