具有RFECV的KNN返回:“分类器不公开" coef_".或"feature_importances_";属性" [英] KNN with RFECV returns: "The classifier does not expose "coef_" or "feature_importances_" attributes"

查看:546
本文介绍了具有RFECV的KNN返回:“分类器不公开" coef_".或"feature_importances_";属性"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在KNeighborsClassifier上应用RFECV以消除微不足道的功能.为了使问题可重复,这是虹膜数据的示例:

I am trying to apply RFECV on KNeighborsClassifier to eliminate insignificant features. In order to make the issue repeatable, here is an example with iris data:

from sklearn.datasets import load_iris
from sklearn.feature_selection import RFECV
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
y = iris.target
X = iris.data
estimator = KNeighborsClassifier()
selector = RFECV(estimator, step=1, cv=5)
selector = selector.fit(X, y)

这将导致以下错误消息:

which results in the following error massage:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-27-19f0f2f0f0e7> in <module>()
      7 estimator = KNeighborsClassifier()
      8 selector = RFECV(estimator, step=1, cv=5)
----> 9 selector.fit(X, y)

C:...\Anaconda3\lib\site-packages\sklearn\feature_selection\rfe.py in fit(self, X, y)
    422                       verbose=self.verbose - 1)
    423 
--> 424             rfe._fit(X_train, y_train, lambda estimator, features:
    425                      _score(estimator, X_test[:, features], y_test, scorer))
    426             scores.append(np.array(rfe.scores_[::-1]).reshape(1, -1))

C:...\Anaconda3\lib\site-packages\sklearn\feature_selection\rfe.py in _fit(self, X, y, step_score)
    180                 coefs = estimator.feature_importances_
    181             else:
--> 182                 raise RuntimeError('The classifier does not expose '
    183                                    '"coef_" or "feature_importances_" '
    184                                    'attributes')

RuntimeError: The classifier does not expose "coef_" or "feature_importances_" attributes

如果我将分类器更改为SVC,则为:

If I change the classifier to a SVC as:

from sklearn.datasets import load_iris
from sklearn.feature_selection import RFECV
from sklearn.svm import SVC
iris = load_iris()
y = iris.target
X = iris.data
estimator = SVC(kernel="linear")
selector = RFECV(estimator, step=1, cv=5)
selector = selector.fit(X, y)

它将正常工作.关于如何解决该问题有什么建议吗?

it would work fine. Any suggestions on how to address the issue?

注意::我昨天更新了Anaconda,它也更新了sklearn.

NOTE: I updated Anaconda yesterday which also updated the sklearn.

推荐答案

错误是不言自明的-knn不提供进行特征选择的逻辑.您不能使用它(sklearn的实现)来实现此目标,除非您定义自己对KNN的功能重要性的度量.据我所知-没有这样的通用对象,所以-scikit-learn没有实现它.另一方面,像每个线性模型一样,SVM也提供此类信息.

Error is pretty self explanatory - knn does not provide logic to do feature selection. You cannot use it (sklearn's implementation) to achieve such goal, unless you define your own measure of feature importance for KNN. As far as I know - there is no such general object, and so - scikit-learn does not implement it. SVM on the other hand, like every linear model - provides such information.

这篇关于具有RFECV的KNN返回:“分类器不公开" coef_".或"feature_importances_";属性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆