在 pipeline 中進行前處理
既然你已經知道要如何逐步處理 Ames 房價資料,現在改用更乾淨、精簡的 DictVectorizer 做法,並把它和 XGBoostRegressor 一起放進 scikit-learn 的 pipeline 裡。
本練習屬於課程
使用 XGBoost 的極端梯度提升
練習說明
- 從
sklearn.feature_extraction匯入DictVectorizer,並從sklearn.pipeline匯入Pipeline。 - 將
X中LotFrontage欄位的遺漏值填成0。 - 完成 pipeline 的步驟:
"ohe_onestep"使用DictVectorizer(sparse=False),"xgb_model"使用xgb.XGBRegressor()。 - 使用
Pipeline()與steps建立 pipeline。 - 訓練
Pipeline。別忘了先呼叫X的to_dict("records")方法,把X轉成DictVectorizer可理解的格式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import necessary modules
____
____
# Fill LotFrontage missing values with 0
X.LotFrontage = ____
# Setup the pipeline steps: steps
steps = [("ohe_onestep", ____),
("xgb_model", ____)]
# Create the pipeline: xgb_pipeline
xgb_pipeline = ____
# Fit the pipeline
____