如何保留StratifierKfold等生成器的副本

时间:2018-10-25 22:35:13

标签: python-3.x scikit-learn cross-validation

我在笔记本上的两个不同的单元格中执行以下命令:

  • skf = StratifiedKFold(n_splits = 4).split(X,Y)
  • regrl = LinearRegression() mse = np.mean(cross_val_score(regrl, X, Y, cv = skf, scoring = 'mean_squared_error'))

cross_val_score的第一次执行没有错误,但第二次尝试返回:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-48-de4073ce654d> in <module>
      2 
      3 
----> 4     mse = np.mean(cross_val_score(regrl, X, Y, cv = skf, scoring = 'mean_squared_error'))
      5 mse

/opt/conda/lib/python3.6/site-packages/sklearn/model_selection/_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch)
    340                                 n_jobs=n_jobs, verbose=verbose,
    341                                 fit_params=fit_params,
--> 342                                 pre_dispatch=pre_dispatch)
    343     return cv_results['test_score']
    344 

/opt/conda/lib/python3.6/site-packages/sklearn/model_selection/_validation.py in cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, return_train_score)
    210         train_scores = _aggregate_score_dicts(train_scores)
    211     else:
--> 212         test_scores, fit_times, score_times = zip(*scores)
    213     test_scores = _aggregate_score_dicts(test_scores)
    214 

ValueError: not enough values to unpack (expected 3, got 0)

如果我再次执行:skf = StratifiedKFold(n_splits = 4).split(X,Y) 错误未返回,生成器skf在使用后变为空。 所以我会知道如何获得生成器的副本。

因为我需要在一个循环中尝试许多模型,但是目前我必须为每次迭代刷新skf,这需要太多时间。

1 个答案:

答案 0 :(得分:0)

代替此操作:

skf = StratifiedKFold(n_splits = 4).split(X,Y)
cross_val_score(regrl, X, Y, cv = skf, ...)

其中skf是您观察到的生成器,将仅执行一次。

您可以这样做:

from sklearn.model_selection import StratifiedKFold, cross_val_score

skf = StratifiedKFold(n_splits = 4)
cross_val_score(regrl, X, Y, cv = skf, ...)

这里skfStratifiedKFold对象,而不是代码之类的生成器。

cross_val_score(在较新版本的scikit-learn> 0.18中,来自model_selection包)可以在折叠迭代器上自动使用提供的数据(X,y)调用split()。因此,您不必显式执行该操作。