Skip to content
Advertisement

What does ‘index 0 is out of bounds for axis 0 with size 0’ mean and how can I fix this error?

I am running a certain stoke prediction model code and getting the following error.

I have given below the code.

#import libraries
import streamlit as st
import numpy as np
import plotly.express as px
import plotly.io as pio
import yfinance as yf
import matplotlib.pyplot as plt
from keras import optimizers
from keras.callbacks import EarlyStopping
from keras.layers import Dense, LSTM
from keras.models import Sequential
from sklearn.preprocessing import MinMaxScaler
plt.style.use('fivethirtyeight')

# Get price for next days:
# Get the last timestep days closing price
new_df = stock_data.filter(['Open', 'High', 'Low', 'Close'])
last_timestep_days = new_df[-time_step:].values
pred_price = np.array([])
for day in range(1, 6):
     if day != 1:
          last_day_predicted_data = np.array(
             [round(pred_price[0][0], 2), round(pred_price[0][1], 2), round(pred_price[0][2], 2),
              round(pred_price[0][3], 2)])
         last_timestep_days = np.concatenate((last_timestep_days,   [last_day_predicted_data]))
         last_timestep_days = np.delete(last_timestep_days, 0,
                               axis=0)  # to remove the first row after adding lastest day predicted data

This is the error I am getting:

    IndexError                                Traceback (most recent call last)
    <ipython-input-42-3623c27dcfbb> in <module>
       7     if day != 1:
       8         last_day_predicted_data = np.array(
 ----> 9             [round(pred_price[0][0], 2), round(pred_price[0][1], 2),  round(pred_price[0][2], 2),
      10              round(pred_price[0][3], 2)])
      11         last_timestep_days = np.concatenate((last_timestep_days,  [last_day_predicted_data]))

      IndexError: index 0 is out of bounds for axis 0 with size 0

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement