【问题标题】:How to create actual dataframes out of k-fold-stratified in Python如何在 Python 中使用 k-fold-stratified 创建实际的数据帧
【发布时间】:2020-04-03 16:40:54
【问题描述】:

在我从 sklearn Stratifiednfold 返回的索引中,如何从每个折叠创建相应的数据帧?

skf = StratifiedKFold(n_splits=10)
skf.get_n_splits(X, y)

for train_index, test_index in skf.split(X, y):
print("TRAIN:", train_index, "TEST:", test_index) 

打印出带有索引的列表。如何将这些映射回我原来的 Dataframe?

我需要它们,因为我想在训练数据上运行我的 texclassification 模型之前将我的增强数据添加到训练数据中。

【问题讨论】:

    标签: python-3.x pandas machine-learning scikit-learn text-classification


    【解决方案1】:

    您可以使用以下索引列表过滤原始数据框:

    df = pd.DataFrame({'foo': ['a', 'b', 'c', 'd', 'e']})
    indices = [0, 2, 4]
    df = df[df.index.isin(indices)]
    

    输出:

      foo
    0   a
    2   c
    4   e
    

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2019-09-04
      • 2014-05-19
      • 2019-11-09
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 2020-11-13
      • 2020-07-09
      相关资源
      最近更新 更多