Plotting Feature Importance

Python

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.

 1|  def plot_feature_importance(importance,names,model_type):
 2|  
 3|  	#Create arrays from feature importance and feature names
 4|  	feature_importance = np.array(importance)
 5|  	feature_names = np.array(names)
 6|  
 7|  	#Create a DataFrame using a Dictionary
 8|  	data={'feature_names':feature_names,'feature_importance':feature_importance}
 9|  	fi_df = pd.DataFrame(data)
10|  
11|  	#Sort the DataFrame in order decreasing feature importance
12|  	fi_df.sort_values(by=['feature_importance'], ascending=False,inplace=True))
13|  
14|  	#Define size of bar plot
15|  	plt.figure(figsize=(10,8))
16|  	#Plot Searborn bar chart
17|  	sns.barplot(x=fi_df['feature_importance'], y=fi_df['feature_names'])
18|  	#Add chart labels
19|  	plt.title(model_type + 'FEATURE IMPORTANCE')
20|  	plt.xlabel('FEATURE IMPORTANCE')
21|  	plt.ylabel('FEATURE NAMES')
22|  
23|  plot_feature_importance(rf_model.feature_importances_,train.columns,'RANDOM FOREST')
24|  plot_feature_importance(xgb_model.feature_importances_,train.columns,'XG BOOST')
25|  plot_feature_importance(cb_model.get_feature_importance(),train.columns,'CATBOOST')
Did you find this snippet useful?

Sign up for free to to add this to your code library