我写了一个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”。
有什么想法吗?
答案 0 :(得分:1)
StratifiedKFold
将组信息考虑在内以避免构建 折叠具有不平衡的类分布(对于二进制或多类 分类任务)。
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