在数据框列表上使用train_test_split

时间:2018-11-15 13:24:36

标签: python python-3.x scikit-learn sklearn-pandas train-test-split

我有12个特征数据帧,分别命名为X[0]X[1] ...到X[11],并与之对应的12个响应数据帧分别为y[0]至{{1 }}。我需要使用train_test_split函数将它们拆分为训练和测试数据帧。在处理空列表y[11]的简单分配过程中:

(X_train[], X_test[], y_train[] and y_test[])

给出此错误:

  

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

我不知道如何使用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 函数。 有人可以帮我吗?

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我这样做如下:

handleCorrect = () => {
  const { correct } = this.state;
  this.input.blur(); //-- this line close the keyboard
  this.setState({ correct: !correct },
    () => {
      setTimeout(() => this.input.focus(), 50);
      //-- above line re-launch keyboard after 50 milliseconds
      //-- this 50 milliseconds is for waiting to closing keyborad finish
    }
  );
};


<TextInput
  autoCorrect={correct}
  editable={isEditable}
  style={styles.textNameEditable}
  defaultValue={text}
  numberOfLines={1}
  ref={input => {
    this.input = input;
  }}
/>

答案 2 :(得分:0)

不需要使用for循环。只是写

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                            test_size=0.2, random_state=2)