From a6664f24309e5032e99fd1784ad20b25f30aa822 Mon Sep 17 00:00:00 2001 From: Venkatachalam Date: Mon, 25 May 2020 07:57:44 +0530 Subject: [PATCH 1/2] sorting feature wrt imp --- .../plot_release_highlights_0_22_0.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/release_highlights/plot_release_highlights_0_22_0.py b/examples/release_highlights/plot_release_highlights_0_22_0.py index 0d53dea9f7640..5eab010e53398 100644 --- a/examples/release_highlights/plot_release_highlights_0_22_0.py +++ b/examples/release_highlights/plot_release_highlights_0_22_0.py @@ -100,18 +100,24 @@ # The :func:`inspection.permutation_importance` can be used to get an # estimate of the importance of each feature, for any fitted estimator: +import numpy as np +import matplotlib.pyplot as plt +from sklearn.datasets import make_classification from sklearn.ensemble import RandomForestClassifier from sklearn.inspection import permutation_importance -X, y = make_classification(random_state=0, n_features=5, n_informative=3) +X, y = make_classification(random_state=0, n_features=5, + n_informative=3) rf = RandomForestClassifier(random_state=0).fit(X, y) result = permutation_importance(rf, X, y, n_repeats=10, random_state=0, n_jobs=-1) +feature_names = np.array([f'x_{i}' for i in range(X.shape[1])]) + fig, ax = plt.subplots() sorted_idx = result.importances_mean.argsort() ax.boxplot(result.importances[sorted_idx].T, - vert=False, labels=range(X.shape[1])) + vert=False, labels=feature_names[sorted_idx]) ax.set_title("Permutation Importance of each feature") ax.set_ylabel("Features") fig.tight_layout() From f1e06722d6854709b49be5e40957d8792cfe8d7d Mon Sep 17 00:00:00 2001 From: Venkatachalam Date: Mon, 25 May 2020 08:03:33 +0530 Subject: [PATCH 2/2] re ordering --- .../release_highlights/plot_release_highlights_0_22_0.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/release_highlights/plot_release_highlights_0_22_0.py b/examples/release_highlights/plot_release_highlights_0_22_0.py index 5eab010e53398..3e01cebbc60fd 100644 --- a/examples/release_highlights/plot_release_highlights_0_22_0.py +++ b/examples/release_highlights/plot_release_highlights_0_22_0.py @@ -106,14 +106,13 @@ from sklearn.ensemble import RandomForestClassifier from sklearn.inspection import permutation_importance -X, y = make_classification(random_state=0, n_features=5, - n_informative=3) +X, y = make_classification(random_state=0, n_features=5, n_informative=3) +feature_names = np.array([f'x_{i}' for i in range(X.shape[1])]) + rf = RandomForestClassifier(random_state=0).fit(X, y) result = permutation_importance(rf, X, y, n_repeats=10, random_state=0, n_jobs=-1) -feature_names = np.array([f'x_{i}' for i in range(X.shape[1])]) - fig, ax = plt.subplots() sorted_idx = result.importances_mean.argsort() ax.boxplot(result.importances[sorted_idx].T,