feature importances sklearn

Solutions on MaxInterview for feature importances sklearn by the best coders in the world

showing results for - "feature importances sklearn"
Aitana
15 Aug 2016
1print(__doc__)
2
3import numpy as np
4import matplotlib.pyplot as plt
5
6from sklearn.datasets import make_classification
7from sklearn.ensemble import ExtraTreesClassifier
8
9# Build a classification task using 3 informative features
10X, y = make_classification(n_samples=1000,
11                           n_features=10,
12                           n_informative=3,
13                           n_redundant=0,
14                           n_repeated=0,
15                           n_classes=2,
16                           random_state=0,
17                           shuffle=False)
18
19# Build a forest and compute the impurity-based feature importances
20forest = ExtraTreesClassifier(n_estimators=250,
21                              random_state=0)
22
23forest.fit(X, y)
24importances = forest.feature_importances_
25std = np.std([tree.feature_importances_ for tree in forest.estimators_],
26             axis=0)
27indices = np.argsort(importances)[::-1]
28
29# Print the feature ranking
30print("Feature ranking:")
31
32for f in range(X.shape[1]):
33    print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
34
35# Plot the impurity-based feature importances of the forest
36plt.figure()
37plt.title("Feature importances")
38plt.bar(range(X.shape[1]), importances[indices],
39        color="r", yerr=std[indices], align="center")
40plt.xticks(range(X.shape[1]), indices)
41plt.xlim([-1, X.shape[1]])
42plt.show()
43