IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Machine Learning with Tree-Based Models in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Call the .sort_values() method on importances and assign the result to importances_sorted.

  • Call the .plot() method on importances_sorted and set the arguments:

    • kind to 'barh'
    • color to 'lightgreen'

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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()
Modifica ed esegui il codice