-2

我有一个 12 个特征数据帧,命名为X[0], X[1]... 直到它对应X[11]12 个响应数据帧。我需要使用 train_test_split 函数将它们分成训练和测试数据帧。由于这处理空列表简单的分配:y[0]y[11](X_train[], X_test[], y_train[] and y_test[])

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

给出这个错误:

IndexError:列表分配索引超出范围

我不知道如何在append()这里使用函数。谁能帮帮我?

4

3 回答 3

1

无需使用 for 循环。写吧

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                            test_size=0.2, random_state=2)
于 2018-11-16T09:59:03.683 回答
0

我认为你需要:

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)
于 2018-11-15T13:32:29.447 回答
0

我这样做如下:

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
于 2018-11-15T13:38:36.147 回答