【问题标题】:Cross validation with specified number of training data?使用指定数量的训练数据进行交叉验证?
【发布时间】:2021-01-19 08:59:40
【问题描述】:

目标

我想执行 k 折交叉验证,但不是使用 k-1 数据集进行训练,使用 k 数据集进行测试,我想确定训练数据的数量,就像 train_test_splittrain_size .然后剩下的作为测试数据。

确切地说,我有二进制分类数据集,并且在进行交叉验证时我想要每个类的 10 个实例。

预期功能

假设我想做5倍简历:

cross_val_score(estimator=my_model, X, y, cv=5, train_size=20)

当然在这种情况下我的 X, y 应该有 >= 100 个实例。

我的尝试

我只是手动构建它们。我能得到的最接近的是迭代:

for _ in range (5):    
  X_tr, X_te, y_tr, y_te = train_test_split(X, y, train_size=20, stratified=y)  

但这会随机选择数据,可能会导致两个训练数据集相似,而且它不包含 cv。

注意

是的,这将导致某些数据集不用于训练集,但这正是我希望在当前工作中实现的目标。

有没有提供这个功能的python函数?

【问题讨论】:

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


    【解决方案1】:

    您仍然可以使用KFold,但需要额外的逻辑。

    确定测试数据量:test_amount = total_amount * test_size

    确定拆分数量n_splits = total_amount // test_amount

    使用 Kfolds:

    kf = KFold(n_splits=n_splits)
    for train_index, test_index in kf.split(X):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
    

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 2011-12-16
      • 2012-02-21
      • 2016-01-29
      • 2020-09-14
      • 2018-05-03
      • 2023-03-29
      • 2021-12-10
      • 2018-09-01
      相关资源
      最近更新 更多