特征选择

Feature Selection

我尝试使用以下代码在 scikit 学习中进行递归特征选择。

from sklearn import datasets, svm
from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.feature_selection import RFE
import numpy as np

input_file_iris = "/home/anuradha/Project/NSL_KDD_master/Modified/iris.csv"
dataset = np.loadtxt(input_file_iris, delimiter=",")
X = dataset[:,0:4]
y = dataset[:,4]

estimator= svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)

selector = RFE(estimator,3, step=1)
selector = selector.fit(X,y)

但它给出以下错误

Traceback (most recent call last):
File "/home/anuradha/PycharmProjects/LearnPython/Scikit-learn/univariate.py", line 30, in <module>
File "/usr/local/lib/python2.7/dist-packages/sklearn/feature_selection/rfe.py", line 131, in fit
return self._fit(X, y)


File "/usr/local/lib/python2.7/dist-packages/sklearn/feature_selection/rfe.py", line 182, in _fit



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

请有人可以帮助我解决这个问题或指导我找到另一种解决方案

将您的内核更改为线性,您的代码就可以工作了。

此外,svm.OneClassSVM用于无监督异常值检测。您确定要将其用作估算器吗?或者您可能想使用 svm.SVC()。查看以下 link 以获取文档。

http://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html

最后,鸢尾花数据集已经在 sklearn 中可用。您已导入 sklearn.datasets。所以你可以简单地加载 iris 为:

iris = datasets.load_iris()
X = iris.data
y = iris.target