9951 explained code solutions for 126 technologies


python-scikit-learnFeature selection using RFE


from sklearn import datasets, svm, feature_selection

X, y = datasets.make_friedman1(n_samples=50, n_features=10)

selector = feature_selection.RFECV(svm.SVR(kernel="linear"), step=1, cv=5)
selector = selector.fit(X, y)

ranks = selector.ranking_ctrl + c
from sklearn import

import module from scikit-learn

.make_friedman1(

creates Friedman #1 regression dataset

.RFECV(

creates RFE feature selection model

svm.SVR(

we use SVR model as estimator for RFE

.fit(

train model

selector.ranking_

returns features rankings


Usage example

from sklearn import datasets, svm, feature_selection

X, y = datasets.make_friedman1(n_samples=50, n_features=10)

selector = feature_selection.RFECV(svm.SVR(kernel="linear"), step=1, cv=5)
selector = selector.fit(X, y)
print(selector.ranking_)
output
[1 1 3 1 1 1 1 2 1 1]