0

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

2
  • the number of rows - window length will be zero right? so how can it in loop tell me? Commented Jul 15, 2022 at 7:53
  • I edit my code to fix the first error but still I got errors on label 1 Commented Jul 15, 2022 at 8:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yes. even if I change window_length to 982 so that 983-982=1, I still got errors when I do train[1]. why is that?
I edit my code to fix the first error but still I got errors on label 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.