我写了一个 KNN 插补实现,我希望 StratifiedKFold 来检查使用什么 K 和什么距离矩阵。我收到一个错误:它似乎没有将我的估算器识别为回归器(“评分”函数用于回归)。我的代码:skf = StratifiedKFold(n_splits=10, shuffle=False, random_state=12)NN = KnnImputation() # my own functiongridSearchNN = 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”。有任何想法吗?
1 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
分层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
添加回答
举报
0/150
提交
取消