Python: index 4557 is out of bounds for axis 0 with size 4557

Viewed 30

I am using lstm for weather prediction, in google colab using python and tensorflow & panda. The snippet of code that gives error is in the creation of univariate model as follows(image attached as well):

univariate_past_history = 20
univariate_future_target = 0

x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,
                                           univariate_past_history,
                                           univariate_future_target)
x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT, None,
                                       univariate_past_history,
                                       univariate_future_target)

The error is

IndexError                                Traceback (most recent call last)
<ipython-input-14-b832595cabf2> in <module>
     4 x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,
     5                                            univariate_past_history,
----> 6                                            univariate_future_target)
     7 x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT, None,
     8                                        univariate_past_history,

<ipython-input-5-fd6d6cf9af09> in univariate_data(dataset, start_index, end_index, history_size, target_size)
    11     # Reshape data from (history_size,) to (history_size, 1)
    12     data.append(np.reshape(dataset[indices], (history_size, 1)))
---> 13     labels.append(dataset[i+target_size])
    14   return np.array(data), np.array(labels)

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

[Error][1]

The function used above is as follows:

def univariate_data(dataset, start_index, end_index, history_size, target_size):
  data = []
  labels = []

  start_index = start_index + history_size
  if end_index is None:
    end_index = len(dataset) - target_size
  for i in range(start_index, end_index):
    indices = range(i-history_size, i)
    # Reshape data from (history_size,) to (history_size, 1)
    data.append(np.reshape(dataset[indices], (history_size, 1)))
    labels.append(dataset[i+target_size])
  return np.array(data), np.array(labels)

The data I am using is 4558*16 csv file in which some values are zeros and other are empty as well. Any help is appreciated. Thanks. [1]: https://i.stack.imgur.com/K3QIy.jpg

1 Answers

Here we can create a simple frame that will have 5 rows. Note that indexes start from zero in pandas.

df = pd.DataFrame([
    i for i in range(5)], columns=['x'])

Result:

x
0 0
1 1
2 2
3 3
4 4

If we check the size of this dataframe, we will get a response of 5 because there are five rows:

>>> df.size
5

What happens if we try to access something at the index position of 5?

>>> df.iloc[5]
IndexError: out-of-bounds

This is telling us that we are trying to access an index in out DataFrame that is outside what is acceptable. In our simple example here, the frame has a size of 5 and has 5x rows but it does not have an index position of five.

Related