-2

I have a 12 feature data frames named as X[0], X[1]... till X[11] and corresponding to it 12 response data frames as y[0] to y[11]. I need to split them into train and test data frames using the train_test_split function. As this processes empty lists (X_train[], X_test[], y_train[] and y_test[]) simple assignment:

b = 0    
while b < 12:
    X_train[b], X_test[b], y_train[b], y_test[b] = train_test_split(X[b], y[b], random_state=0)
    b = b + 1

gives this error:

IndexError: list assignment index out of range

I don't know how to use append() function here. Can anyone please help me out?

Aqueous Carlos
  • 445
  • 7
  • 20
Batman
  • 1
  • 4
  • Do all your feature data frames have the same features or does each feature data frame represent a different set of features? – vielkind Nov 15 '18 at 13:32

3 Answers3

1

There is no need to use a for loop. Just write

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                            test_size=0.2, random_state=2)
wleizny
  • 46
  • 5
0

I think you need:

X_train = []
X_test = []
y_train = []
y_test = []



for i in range(0,12):
    a, b, c, d = train_test_split(X[i], y[i], test_size=0.2, random_state=0)

    X_train.append(a)
    X_test.append(b)
    y_train.append(c)
    y_test.append(d)
Sociopath
  • 13,068
  • 19
  • 47
  • 75
0

I did it as follows:

while b < 12:
    X_t, X_te, y_t, y_te = train_test_split(X[b], y[b], random_state=0)
    X_train.append(X_t)
    X_test.append(X_te)
    y_train.append(y_t)
    y_test.append(y_te)

    b = b + 1
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
Batman
  • 1
  • 4