 ANALYSEUP

Plotting Feature Importance

Python
Model Evaluation

This is an example of using a function for generating a feature importance plot when using Random Forest, XGBoost or Catboost. This allows more intuitive evaluation of models built using these algorithms.

def plot_feature_importance(importance,names,model_type):

#Create arrays from feature importance and feature names
feature_importance = np.array(importance)
feature_names = np.array(names)

#Create a DataFrame using a Dictionary
data={'feature_names':feature_names,'feature_importance':feature_importance}
fi_df = pd.DataFrame(data)

#Sort the DataFrame in order decreasing feature importance
fi_df.sort_values(by=['feature_importance'], ascending=False,inplace=True))

#Define size of bar plot
plt.figure(figsize=(10,8))
#Plot Searborn bar chart
sns.barplot(x=fi_df['feature_importance'], y=fi_df['feature_names'])
plt.title(model_type + 'FEATURE IMPORTANCE')
plt.xlabel('FEATURE IMPORTANCE')
plt.ylabel('FEATURE NAMES')

plot_feature_importance(rf_model.feature_importances_,train.columns,'RANDOM FOREST')
plot_feature_importance(xgb_model.feature_importances_,train.columns,'XG BOOST')
plot_feature_importance(cb_model.get_feature_importance(),train.columns,'CATBOOST')

By analyseup - Last Updated Jan. 6, 2022, 4:02 p.m. Did you find this snippet useful?

RELATED SNIPPETS
Evaluating Poor Predictions Using SHAP
Python
Model Evaluation
2
Shap Summary Plots
Python
Model Evaluation
1
Prediction Error Distribution
Python
Model Evaluation
1
Top Contributors 82 75 36
Find Snippets by Language
Find Snippets by Use