Created
May 30, 2020 10:54
-
-
Save cereniyim/93907f4cb245c67fc98df616a3c15f23 to your computer and use it in GitHub Desktop.
Plot feature importances of a ML model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def PlotFeatureImportances(model, feature_names): | |
feature_importances = (pd | |
.DataFrame( | |
{'feature': feature_names, | |
'importance': model | |
.feature_importances_})) | |
feature_importances = (feature_importances | |
.sort_values(by="importance", | |
ascending=False)) | |
figsize(20, 10) | |
plt.rcParams['font.size'] = 14 | |
sns.set(font_scale=1.5, style="whitegrid") | |
# set color | |
labels = np.array(feature_importances.feature) | |
values = np.array(feature_importances.importance) | |
colors = ["#808080" if (y < max(values)) | |
else "#971539" for y in values] | |
# set the plot | |
ax = sns.barplot(x="importance", | |
y="feature", | |
data=feature_importances, | |
palette = colors) | |
# set title and save plot | |
plt.title("Feature Importances", size =16) | |
# plt.savefig("FeatureImportances.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment