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.
Deze oefening maakt deel uit van de cursus
Machine Learning with Tree-Based Models in Python
Oefeninstructies
Call the
.sort_values()method onimportancesand assign the result toimportances_sorted.Call the
.plot()method onimportances_sortedand set the arguments:kindto'barh'colorto'lightgreen'
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# 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()