開始使用免費開始

視覺化特徵重要度

在這個練習中,你要找出在先前練習中訓練的隨機森林迴歸器 rf 判定為最具有預測力的特徵。

為此,你會繪製由 rf 評估之特徵重要度的水平長條圖。幸好,因為有 pandas 的繪圖功能,這件事相當容易。

我們已建立名為 importancespandas.Series 物件,其中以特徵名稱作為 index,以其重要度作為值。另外,matplotlib.pyplot 已以 plt 匯入,pandaspd 匯入可用。

本練習屬於課程

Machine Learning with Tree-Based Models in Python

檢視課程

練習說明

  • importances 呼叫 .sort_values() 方法,並將結果指定給 importances_sorted

  • importances_sorted 呼叫 .plot() 方法,並設定參數:

    • kind 設為 'barh'
    • color 設為 'lightgreen'

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼