I am running a certain stoke prediction model code and getting the following error.
I have given below the code.
JavaScript
x
28
28
1
#import libraries
2
import streamlit as st
3
import numpy as np
4
import plotly.express as px
5
import plotly.io as pio
6
import yfinance as yf
7
import matplotlib.pyplot as plt
8
from keras import optimizers
9
from keras.callbacks import EarlyStopping
10
from keras.layers import Dense, LSTM
11
from keras.models import Sequential
12
from sklearn.preprocessing import MinMaxScaler
13
plt.style.use('fivethirtyeight')
14
15
# Get price for next days:
16
# Get the last timestep days closing price
17
new_df = stock_data.filter(['Open', 'High', 'Low', 'Close'])
18
last_timestep_days = new_df[-time_step:].values
19
pred_price = np.array([])
20
for day in range(1, 6):
21
if day != 1:
22
last_day_predicted_data = np.array(
23
[round(pred_price[0][0], 2), round(pred_price[0][1], 2), round(pred_price[0][2], 2),
24
round(pred_price[0][3], 2)])
25
last_timestep_days = np.concatenate((last_timestep_days, [last_day_predicted_data]))
26
last_timestep_days = np.delete(last_timestep_days, 0,
27
axis=0) # to remove the first row after adding lastest day predicted data
28
This is the error I am getting:
JavaScript
1
10
10
1
IndexError Traceback (most recent call last)
2
<ipython-input-42-3623c27dcfbb> in <module>
3
7 if day != 1:
4
8 last_day_predicted_data = np.array(
5
----> 9 [round(pred_price[0][0], 2), round(pred_price[0][1], 2), round(pred_price[0][2], 2),
6
10 round(pred_price[0][3], 2)])
7
11 last_timestep_days = np.concatenate((last_timestep_days, [last_day_predicted_data]))
8
9
IndexError: index 0 is out of bounds for axis 0 with size 0
10
Is there anyway to resolve this error? I will share more dependencies information if required.
Advertisement
Answer
pred_price
is an empty np.array
so trying to index it like you have done on line 9 (“pred_price[0]
“) will give an error. There has to be something in the array first.