anagnam
anagnam

Reputation: 457

How to fix IndexErrors in Python

I know theres tons of similar question titles but none of them solved my particular question.

So I have this code:

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

# my_list contains 983 list items
df = pd.DataFrame(np.array(my_list), columns=list('ABCDEF'))

df contains 983 items composed of lists of list

df.head()

A   B   C   D   E   F
0   47  5   17  16  57  58
1   6   23  34  21  46  37
2   57  5   53  42  18  55
3   43  24  36  16  39  22
4   32  53  5   18  34  29


scaler = StandardScaler().fit(df.values)
transformed_dataset = scaler.transform(df.values)
transformed_df = pd.DataFrame(data=transformed_dataset, index=df.index)
number_of_rows = df.values.shape[0] # all our lists
window_length = 983 # amount of past number list we need to take in consideration for prediction
number_of_features = df.values.shape[1] # number count
train = np.empty([number_of_rows-window_length, window_length, number_of_features], dtype=float)
label = np.empty([number_of_rows-window_length, number_of_features], dtype=float)
window_length = 982
for i in range(0, number_of_rows-window_length):
    train[i]=transformed_df.iloc[i:i+window_length,0:number_of_features]
    label[i]=transformed_df.iloc[i:i+window_length:i+window_length+1,0:number_of_features]

train.shape

(0, 983, 6)

label.shape

(0, 6)

train[0] is working fine but when I do train[1] I got this error:

train[1]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-43-e73aed9430c6> in <module>
----> 1 train[1]

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

also when I do label[0], its fine. but when I do label[1] I got this error:

label[1]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-45-1e13a70afa10> in <module>
----> 1 label[1]

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

how to fix IndexErrors

Upvotes: 0

Views: 92

Answers (1)

Marko Kn&#246;bl
Marko Kn&#246;bl

Reputation: 512

You're creating an array whose first dimension has size 0 - that's why you're getting these errors

You're using the value number_of_rows - window_length for the first dimension - which is 0. I guess that's not what you want.

Upvotes: 1

Related Questions