Visualizing features importances
In this exercise, you'll determine which features were the most predictive according to the random forests regressor rf
that you trained in a previous exercise.
For this purpose, you'll draw a horizontal barplot of the feature importance as assessed by rf
. Fortunately, this can be done easily thanks to plotting capabilities of pandas
.
We have created a pandas.Series
object called importances
containing the feature names as index
and their importances as values. In addition, matplotlib.pyplot
is available as plt
and pandas
as pd
.
This exercise is part of the course
Machine Learning with Tree-Based Models in Python
Exercise instructions
Call the
.sort_values()
method onimportances
and assign the result toimportances_sorted
.Call the
.plot()
method onimportances_sorted
and set the arguments:kind
to'barh'
color
to'lightgreen'
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a pd.Series of features importances
importances = pd.Series(data=rf.feature_importances_,
index= X_train.columns)
# Sort importances
importances_sorted = ____
# Draw a horizontal barplot of importances_sorted
____.____(____='____', ____='____')
plt.title('Features Importances')
plt.show()