【问题标题】:Actual data from KFold split indices来自 KFold 拆分索引的实际数据
【发布时间】:2021-12-17 00:48:09
【问题描述】:

假设我有以下数据:

y = np.ones(10)
y[-5:] = 0
X = pd.DataFrame({'a':np.random.randint(10,20, size=(10)),
                  'b':np.random.randint(80,90, size=(10))})
X    
    a   b
0   11  82
1   19  82
2   15  80
3   15  86
4   14  82
5   18  87
6   13  83
7   12  83
8   10  82
9   18  87

将其拆分为 5 倍给出以下索引:

kf =  KFold()
data = list(kf.split(X,y))
data
[(array([2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1])),
 (array([0, 1, 4, 5, 6, 7, 8, 9]), array([2, 3])),
 (array([0, 1, 2, 3, 6, 7, 8, 9]), array([4, 5])),
 (array([0, 1, 2, 3, 4, 5, 8, 9]), array([6, 7])),
 (array([0, 1, 2, 3, 4, 5, 6, 7]), array([8, 9]))]

但我想进一步准备data,以便将其组织为包含格式中的实际值:

data =
   [(train1,trainlabel1,test1,testlabel1),
    (train2,trainlabel2,test2,testlabel2),
     ..,
    (train5,trainlabel5,test5,testlabel5)]

预期输出(来自给定的 MWE):

[array([
        (array([[15,80],[15,86],[14,82],[18,87],[13,83],[12,83],[10,82],[18,87]]), array([[1],[1],[1],[0],[0],[0],[0],[0])]), #fold1 train/label
        (array([[11,82],[19,82]]), array([[1],[1]])),  #fold1 test/label

        (array([[11,82],[19,82],[14,82],[18,87],[13,83],[12,83],[10,82],[18,87]]),array([[1],[1],[1],[0],[0],[0],[0],[0]])), #fold2 train/label
        (array([[15,80],[15,86]]),array([[1],[1]])) #fold2 test/label

        ....
])]

【问题讨论】:

    标签: python machine-learning scikit-learn cross-validation k-fold


    【解决方案1】:

    如您所知,KFold().split(data) 按折叠返回选定的索引。 要选择带有索引列表的 Pandas.DataFrame 行,最简单的方法是 loc method

    for train_idx, test_idx in KFold(n_splits=2).split(X):
       x_train = X.loc[train_idx]
       x_test = X.loc[test_idx]
    
       y_train = y.loc[train_idx]
       y_test = y.loc[test_idx]
    

    然后您可以将子集数据框添加到列表中

    【讨论】:

    • 这给出了以下错误:AttributeError: 'numpy.ndarray' object has no attribute 'loc'
    • 要使用 Pandas.DataFrame.loc,它必须是一个 DataFrame。尝试转换它.. pd.DataFrame(y)
    【解决方案2】:

    其实@hotuagia 的回答是正确的。您收到此错误是因为您尝试访问 y 的元素,这是一个使用数据框属性 loc 的数组。一种方便的方法是将y 转换为熊猫DataframeSeries,然后再传递给KFold

    所以:

    y = np.ones(10) 
    y[-5:] = 0
    X = pd.DataFrame({'a':np.random.randint(10,20, size=(10)),
                      'b':np.random.randint(80,90, size=(10))})
    # y- array to pandas df or series
    y = pd.DataFrame(y) # or pd.Series(y)
    

    然后继续@hotuagia 的回答:

    for train_idx, test_idx in KFold(n_splits=2).split(X):
       x_train = X.loc[train_idx]
       x_test = X.loc[test_idx]
    
       y_train = y.loc[train_idx]
       y_test = y.loc[test_idx]
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2019-05-12
      • 2018-03-09
      • 1970-01-01
      • 2019-09-04
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      相关资源
      最近更新 更多