How to run RFECV with SVC in sklearn

2020-04-08 12:30发布

I am trying to perform Recursive Feature Elimination with Cross Validation (RFECV) with GridSearchCV as follows using SVC as the classifier.

My code is as follows.

X = df[my_features]
y = df['gold_standard']

x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0)

k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)

clf = SVC(class_weight="balanced")
rfecv = RFECV(estimator=clf, step=1, cv=k_fold, scoring='roc_auc')

param_grid = {'estimator__C': [0.001, 0.01, 0.1, 0.25, 0.5, 0.75, 1.0, 10.0, 100.0, 1000.0],
              'estimator__gamma': [0.001, 0.01, 0.1, 1.0, 2.0, 3.0, 10.0, 100.0, 1000.0],
              'estimator__kernel':('rbf', 'sigmoid', 'poly')
       }

CV_rfc = GridSearchCV(estimator=rfecv, param_grid=param_grid, cv= k_fold, scoring = 'roc_auc', verbose=10)

CV_rfc.fit(x_train, y_train)

However, I got an error saying: RuntimeError: The classifier does not expose "coef_" or "feature_importances_" attributes

Is there a way to resolve this error? If not what are the other feature selection techniques that I can use with SVC?

I am happy to provide more details if needed.

2条回答
老娘就宠你
2楼-- · 2020-04-08 13:02

emmm...in sklearn 0.19.2,The problem seems to have been solved.My code is similar to yours, but it works:

           svc = SVC(                    
            kernel = 'linear',
            probability = True,
            random_state = 1 ) 
       rfecv = RFECV(
               estimator = svc,
               scoring = 'roc_auc'  
               )

       rfecv.fit(train_values,train_Labels)     
       selecInfo = rfecv.support_                      
       selecIndex = np.where(selecInfo==1)         
查看更多
贼婆χ
3楼-- · 2020-04-08 13:07

To look at more feature selection implementations you can have a look at:

https://scikit-learn.org/stable/modules/classes.html#module-sklearn.feature_selection

As an example, in the next link they use PCA with k-best feature selection and svc.

https://scikit-learn.org/stable/auto_examples/compose/plot_feature_union.html#sphx-glr-auto-examples-compose-plot-feature-union-py

An example of use would be, modified form the previous link for more simplicity:

iris = load_iris()

X, y = iris.data, iris.target

# Maybe some original features where good, too?
selection = SelectKBest()

# Build SVC
svm = SVC(kernel="linear")

# Do grid search over k, n_components and C:

pipeline = Pipeline([("features", selection), ("svm", svm)])

param_grid = dict(features__k=[1, 2],
                  svm__C=[0.1, 1, 10])

grid_search = GridSearchCV(pipeline, param_grid=param_grid, cv=5, verbose=10)
grid_search.fit(X, y)
print(grid_search.best_estimator_)
查看更多
登录 后发表回答