Skip to content

Instantly share code, notes, and snippets.

@zaffnet
Last active August 6, 2016 10:16
Show Gist options
  • Select an option

  • Save zaffnet/04bfbbba23d2f9e3589f7aad022cf733 to your computer and use it in GitHub Desktop.

Select an option

Save zaffnet/04bfbbba23d2f9e3589f7aad022cf733 to your computer and use it in GitHub Desktop.
scikit learn Recursive Feature Elimination
# Feature Importance
from sklearn import datasets
from sklearn import metrics
from sklearn.ensemble import ExtraTreesClassifier
# load the iris datasets
dataset = datasets.load_iris()
# fit an Extra Trees model to the data
model = ExtraTreesClassifier()
model.fit(dataset.data, dataset.target)
# display the relative importance of each attribute
print(model.feature_importances_)
# Recursive Feature Elimination
from sklearn import datasets
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
# load the iris datasets
dataset = datasets.load_iris()
# create a base classifier used to evaluate a subset of attributes
model = LogisticRegression()
# create the RFE model and select 3 attributes
rfe = RFE(model, 3)
rfe = rfe.fit(dataset.data, dataset.target)
# summarize the selection of the attributes
print(rfe.support_)
print(rfe.ranking_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment