視覺化特徵重要度
在這個練習中,你要找出在先前練習中訓練的隨機森林迴歸器 rf 判定為最具有預測力的特徵。
為此,你會繪製由 rf 評估之特徵重要度的水平長條圖。幸好,因為有 pandas 的繪圖功能,這件事相當容易。
我們已建立名為 importances 的 pandas.Series 物件,其中以特徵名稱作為 index,以其重要度作為值。另外,matplotlib.pyplot 已以 plt 匯入,pandas 以 pd 匯入可用。
本練習屬於課程
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()