Skip to content

Instantly share code, notes, and snippets.

@cereniyim
Created May 30, 2020 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cereniyim/93907f4cb245c67fc98df616a3c15f23 to your computer and use it in GitHub Desktop.
Save cereniyim/93907f4cb245c67fc98df616a3c15f23 to your computer and use it in GitHub Desktop.
Plot feature importances of a ML model
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