ValueError: k-fold cross-validation requires at least one train/test split by setting n_splits=2 or more, got n_splits=1

ValueError: k-fold cross-validation requires at least one train/test split by setting n_splits=2 or more, got n_splits=1

我遇到了错误

ValueError: k-fold cross-validation requires at least one train/test split by setting n_splits=2 or more, got n_splits=1

有相同问题

对我来说

我使用了 python 3.8.6 和 scikit-learn==0.23.2,一切正常,

我使用 scikit-learn==0.24.2 更新到 python 3.9.5,遇到这个错误,我在 X_test 中有 191 个样本。我不确定为什么库版本会导致此问题。

使用 cv=3 和总共 1000 条记录的数据集

完整代码

X_train, X_test, y_train, y_test = train_test_split(features,
                                                    labels,
                                                    test_size=0.2,
                                                    random_state=200)
smote_enn = SMOTEENN(sampling_strategy='all', random_state=127)
X_train_senn, y_train_senn = smote_enn.fit_resample(X_train, y_train)
lgbmclassifier = LGBMClassifier(boosting_type='gbdt',
                                max_depth=-1,
                                device_type=deviceType,
                                verbose=0,
                                objective='binary',
                                class_weight='balanced',
                                force_row_wise=True,
                                subsample_for_bin=200000,
                                min_child_samples=20,
                                random_state=50)
lgbmgrid = CVGrid(lgbmclassifier, FE_Hyperparamerters)
lgbmgrid_result = lgbmgrid.fit(X_train,
                               y_train,
                               eval_metric='auc',
                               eval_set=[(X_test, y_test)],
                               early_stopping_rounds=ESR,
                               verbose=1)

错误

 File "C:\prg\utils.py", line 851, in feHPTuning
    lgbmgrid_result = lgbmgrid.fit(X_train,
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\model_selection\_search.py", line 762, in fit
    cv_orig = check_cv(self.cv, y, classifier=is_classifier(estimator))
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\model_selection\_split.py", line 2062, in check_cv
    return StratifiedKFold(cv)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\model_selection\_split.py", line 636, in __init__
    super().__init__(n_splits=n_splits, shuffle=shuffle,
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\sklearn\model_selection\_split.py", line 280, in __init__
    raise ValueError(
ValueError: k-fold cross-validation requires at least one train/test split by setting n_splits=2 or more, got n_splits=1.

拟合函数产生错误

这个错误非常简单。您无法执行只有 1 次拆分的 Kfold 拆分。

Kfold 文档指出 n_splits 是折叠数,必须至少为 2。

如果您只想执行一次拆分,您应该使用 sklearn.model_selection.train_test_split