在python中使用连续目标在自己的估计器上进行网格搜索

我写了一个 KNN 插补实现,我希望 StratifiedKFold 来检查使用什么 K 和什么距离矩阵。我收到一个错误:它似乎没有将我的估算器识别为回归器(“评分”函数用于回归)。


我的代码:


skf = StratifiedKFold(n_splits=10, shuffle=False, random_state=12)

NN = KnnImputation() # my own function

gridSearchNN = GridSearchCV(NN, param_grid=params, scoring='mean_squared_error', n_jobs=numIter,

                            cv=skf.split(xTrain, yTrain), verbose=verbose)

gridSearchNN.fit(xTrain, yTrain)

错误:


  File "........\dataImputation.py", line 63, in knnImputationMethod

    gridSearchNN.fit(xTrain, yTrain)

  File "C:\Users\...\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py", line 651, in fit

    cv = check_cv(self.cv, y, classifier=is_classifier(estimator))

  File "C:\Users\....\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 2068, in check_cv

    return _CVIterableWrapper(cv)

  File "C:\Users\....\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 1966, in __init__

    self.cv = list(cv)

  File "C:\Users\...\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 331, in split

    for train, test in super(_BaseKFold, self).split(X, y, groups):

  File "C:\Users\...\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 100, in split

    for test_index in self._iter_test_masks(X, y, groups):

  File "C:\Users\...\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 681, in _iter_test_masks

    test_folds = self._make_test_folds(X, y)

  File "C:\Users\...\Anaconda3\lib\site-packages\sklearn\model_selection\_split.py", line 636, in _make_test_folds

    allowed_target_types, type_of_target_y))

ValueError: Supported target types are: ('binary', 'multiclass'). Got 'continuous' instead.

在“GridSearchCV”过程中,我看到它进入“is_classifier”而不是“is_regressor”。


有任何想法吗?


ABOUTYOU
浏览 171回答 1
1回答

米琪卡哇伊

分层KFold考虑组信息以避免构建具有不平衡类分布的折叠(对于二元或多类分类任务)。StratifiedKFold 仅适用于分类数据,不适用于回归。https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.KFold.html替换StratifiedKFold为KFold您可以在此处查看来源:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/model_selection/_split.py#L570
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python