特征选择错误:The classifier does not expose "coef_" or "feature_importances_" attributes

在利用RFE进行特征筛选的时候出现问题,源代码如下:

from sklearn.svm import SVR
model_SVR = SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
    kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
selector = RFE(estimator = model_SVR, step=1)
selector = selector.fit(df_train_X, df_train_y.values.ravel())
selector.support_
selector.ranking_

错误如下:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-49-ca6dfcbf43ee> in <module>
     38     kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
     39 selector = RFE(estimator = model_SVR, step=1)
---> 40 selector = selector.fit(df_train_X, df_train_y.values.ravel())
     41 selector.support_
     42 selector.ranking_

~/anaconda3/envs/tensorflow/lib/python3.7/site-packages/sklearn/feature_selection/rfe.py in fit(self, X, y)
    142             The target values.
    143         """
--> 144         return self._fit(X, y)
    145 
    146     def _fit(self, X, y, step_score=None):

~/anaconda3/envs/tensorflow/lib/python3.7/site-packages/sklearn/feature_selection/rfe.py in _fit(self, X, y, step_score)
    189                 coefs = getattr(estimator, 'feature_importances_', None)
    190             if coefs is None:
--> 191                 raise RuntimeError('The classifier does not expose '
    192                                    '"coef_" or "feature_importances_" '
    193                                    'attributes')

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

经过百度发现问题,原文在stackflow,链接如下:
KNN with RFECV returns: “The classifier does not expose ”coef_“ or ”feature_importances_“ attributes”

“knn不提供进行特征选择的逻辑。除非您为KNN定义自己的特性重要性度量,否则您不能使用它(sklearn的实现)来实现这样的目标。据我所知-没有这样的通用对象,所以-scikit learn没有实现它。另一方面,支持向量机,像每一个线性模型-提供这样的信息。”
也就是用rbf核进行筛选是不可以的,SVR中不提供rbf特征选择的逻辑,对于支持向量回归可以用linear进行筛选

你可能感兴趣的:(python)