開始使用免費開始

視覺化特徵重要性:資料集中哪些特徵最重要

視覺化 XGBoost 模型的另一種方式,是在模型中檢視原始資料集中各特徵欄位的重要性。

一個簡單的方法,是統計在所有提昇輪次(樹)中,每個特徵被用來做切分的次數,然後依出現次數排序,將結果畫成長條圖。XGBoost 提供 plot_importance() 函式,正好能做到這件事,而你會在本練習中實際使用它!

本練習屬於課程

使用 XGBoost 的極端梯度提升

檢視課程

練習說明

  • 和之前一樣,從 Xy 建立 DMatrix
  • 建立參數字典,設定合適的 "objective""reg:squarederror")以及 "max_depth"4
  • 與上一個練習完全相同,使用 10 個提昇輪次來訓練模型。
  • 使用 xgb.plot_importance(),並傳入訓練後的模型,以產生特徵重要性的圖表。

動手互動練習

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

# Create the DMatrix: housing_dmatrix
housing_dmatrix = ____

# Create the parameter dictionary: params
params = ____

# Train the model: xg_reg
xg_reg = ____

# Plot the feature importances
____
plt.show()
編輯並執行程式碼