2011-12-19 20:10:44 +08:00
|
|
|
"""
|
|
|
|
|
=========================================
|
|
|
|
|
Feature importances with forests of trees
|
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
|
|
This examples shows the use of forests of trees to evaluate the importance of
|
2020-02-01 20:18:11 +08:00
|
|
|
features on an artificial classification task. The red bars are
|
|
|
|
|
the impurity-based feature importances of the forest,
|
|
|
|
|
along with their inter-trees variability.
|
2011-12-19 20:10:44 +08:00
|
|
|
|
2012-07-19 17:42:04 +08:00
|
|
|
As expected, the plot suggests that 3 features are informative, while the
|
|
|
|
|
remaining are not.
|
2020-02-05 22:20:20 +08:00
|
|
|
|
2020-07-30 19:19:06 +08:00
|
|
|
.. warning::
|
|
|
|
|
Impurity-based feature importances can be misleading for high cardinality
|
|
|
|
|
features (many unique values). See
|
|
|
|
|
:func:`sklearn.inspection.permutation_importance` as an alternative.
|
2011-12-19 20:10:44 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-12-19 20:10:44 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-02-27 17:22:21 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-12-19 20:10:44 +08:00
|
|
|
|
|
|
|
|
from sklearn.datasets import make_classification
|
2011-12-20 02:00:43 +08:00
|
|
|
from sklearn.ensemble import ExtraTreesClassifier
|
2011-12-19 20:10:44 +08:00
|
|
|
|
|
|
|
|
# Build a classification task using 3 informative features
|
|
|
|
|
X, y = make_classification(n_samples=1000,
|
2015-10-12 14:09:39 +08:00
|
|
|
n_features=10,
|
2011-12-19 20:10:44 +08:00
|
|
|
n_informative=3,
|
|
|
|
|
n_redundant=0,
|
|
|
|
|
n_repeated=0,
|
|
|
|
|
n_classes=2,
|
|
|
|
|
random_state=0,
|
|
|
|
|
shuffle=False)
|
|
|
|
|
|
2020-02-01 20:18:11 +08:00
|
|
|
# Build a forest and compute the impurity-based feature importances
|
2011-12-28 00:54:25 +08:00
|
|
|
forest = ExtraTreesClassifier(n_estimators=250,
|
|
|
|
|
random_state=0)
|
|
|
|
|
|
2011-12-19 20:10:44 +08:00
|
|
|
forest.fit(X, y)
|
2011-12-28 00:54:25 +08:00
|
|
|
importances = forest.feature_importances_
|
2012-09-05 00:24:01 +08:00
|
|
|
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
|
|
|
|
|
axis=0)
|
2011-12-19 20:10:44 +08:00
|
|
|
indices = np.argsort(importances)[::-1]
|
|
|
|
|
|
|
|
|
|
# Print the feature ranking
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Feature ranking:")
|
2011-12-19 20:10:44 +08:00
|
|
|
|
2015-10-12 14:09:39 +08:00
|
|
|
for f in range(X.shape[1]):
|
2013-02-01 22:04:03 +08:00
|
|
|
print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
|
2011-12-19 20:10:44 +08:00
|
|
|
|
2020-02-01 20:18:11 +08:00
|
|
|
# Plot the impurity-based feature importances of the forest
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.figure()
|
|
|
|
|
plt.title("Feature importances")
|
2015-10-12 14:09:39 +08:00
|
|
|
plt.bar(range(X.shape[1]), importances[indices],
|
2020-02-05 22:20:20 +08:00
|
|
|
color="r", yerr=std[indices], align="center")
|
2015-10-12 14:09:39 +08:00
|
|
|
plt.xticks(range(X.shape[1]), indices)
|
|
|
|
|
plt.xlim([-1, X.shape[1]])
|
2014-02-27 17:22:21 +08:00
|
|
|
plt.show()
|