【问题标题】:Using train_test_split over a list of dataframes在数据帧列表上使用 train_test_split
【发布时间】:2019-04-18 14:48:42
【问题描述】:

我有 12 个特征数据帧,分别命名为 X[0]X[1]... 直到 X[11],对应于它的 12 个响应数据帧为 y[0]y[11]。我需要使用 train_test_split 函数将它们分成训练和测试数据帧。由于这处理空列表(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() 函数。 谁能帮帮我?

【问题讨论】:

  • 您的所有特征数据框是否都具有相同的特征,或者每个特征数据框是否代表一组不同的特征?

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


【解决方案1】:

无需使用 for 循环。随便写

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

【讨论】:

    【解决方案2】:

    我是这样做的:

    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
    

    【讨论】:

      【解决方案3】:

      我认为你需要:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-21
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        • 2017-06-27
        相关资源
        最近更新 更多