解讀結果
幾乎在所有情況下,了解哪些特徵對你的預測影響最大都很重要。也許結果違反直覺,而這正是一項洞見?也許只要少數幾個特徵就能解釋模型的大部分準確度,你就不需要再花時間收集或調整其他特徵。
在這個例子中,我們會查看一個未使用任何 LISTPRICE 資訊訓練出的模型。在移除它之後,什麼因素最影響價格?
- 注意:特徵重要度的陣列
importances已經替你建立好,來源是model.featureImportances.toArray()。
本練習屬於課程
使用 PySpark 進行特徵工程
練習說明
- 使用
importances的值建立一個pandasdataframe,並透過參數columns將欄位命名為importance。 - 使用已匯入的特徵名稱清單
feature_cols,用pd.Series()建立一個新的pandas.Series,並指定到欄位fi_df['feature']。 - 使用
sort_values()來排序 dataframe,將參數by設為我們的importance欄位,並將ascending設為False以遞減排序。檢查結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Convert feature importances to a pandas column
fi_df = pd.DataFrame(____, columns=[____])
# Convert list of feature names to pandas column
fi_df['feature'] = pd.____(____)
# Sort the data based on feature importance
fi_df.____(by=[____], ascending=____, inplace=True)
# Inspect Results
fi_df.head(10)