特征选择

时间:2016-08-11 08:51:18

标签: python scikit-learn 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

请有人帮我解决这个问题或指导我另一个解决方案

1 个答案:

答案 0 :(得分:2)

将内核更改为线性,您的代码就可以使用。

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

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

最后,irle数据集已在sklearn中提供。您已导入sklearn.datasets。因此,您只需将虹膜加载为:

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