lundi 13 juin 2016

KNN with RFECV returns: "The classifier does not expose "coef_" or "feature_importances_" attributes"

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:...Anaconda3libsite-packagessklearnfeature_selectionrfe.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:...Anaconda3libsite-packagessklearnfeature_selectionrfe.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

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?

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

Aucun commentaire:

Enregistrer un commentaire