模型解讀、儲存與載入

使用 PySpark 進行特徵工程

John Hogue

Lead Data Scientist, General Mills

模型解讀

import pandas as pd
# 將特徵重要度轉成 pandas 欄位
fi_df = pd.DataFrame(model.featureImportances.toArray(), 
                     columns=['importance'])
# 將特徵名稱清單轉成 pandas 欄位
fi_df['feature'] = pd.Series(feature_cols)
# 依特徵重要度排序資料
fi_df.sort_values(by=['importance'], ascending=False, inplace=True)
使用 PySpark 進行特徵工程

模型解讀

# 解讀結果
model_df.head(9)
|          feature        |importance|
|-------------------------|----------|
| LISTPRICE               | 0.312101 |
| ORIGINALLISTPRICE       | 0.202142 |
| LIVINGAREA              | 0.124239 |
| SQFT_TOTAL              | 0.081260 |
| LISTING_TO_MEDIAN_RATIO | 0.075086 |
| TAXES                   | 0.048452 |
| SQFTABOVEGROUND         | 0.045859 |
| BATHSTOTAL              | 0.034397 |
| LISTING_PRICE_PER_SQFT  | 0.018253 |
使用 PySpark 進行特徵工程

模型儲存與載入

# 儲存模型
model.save('rfr_real_estate_model')
from pyspark.ml.regression import RandomForestRegressionModel

# 載入模型
model2 = RandomForestRegressionModel.load('rfr_real_estate_model')
使用 PySpark 進行特徵工程

進入最後一組練習!

使用 PySpark 進行特徵工程

Preparing Video For Download...