Reputation: 580
I tried to do recursive feature selection in scikit learn with following code.
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)
But it gives following error
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
Please some one can help me to solve this or guide me to another solution
Upvotes: 2
Views: 1015
Reputation: 970
Change your kernel to linear and your code would work.
Besides, svm.OneClassSVM
is used for unsupervised outlier detection. Are you sure that you want to use it as estimator? Or perhaps you want to use svm.SVC()
. Look the following link for documentation.
http://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html
Lastly, iris data set is already available in sklearn. You have imported the sklearn.datasets
. So you can simply load iris as:
iris = datasets.load_iris()
X = iris.data
y = iris.target
Upvotes: 2