開始使用免費開始

在 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
____
編輯並執行程式碼