對你的 XGBoost 模型做交叉驗證
在這個練習中,你會更進一步,使用你建立的 pipeline 來做前處理,並且對模型進行交叉驗證。
本練習屬於課程
使用 XGBoost 的極端梯度提升
練習說明
- 使用
steps建立名為xgb_pipeline的 pipeline。 - 使用 10 折交叉驗證,呼叫
cross_val_score()。你需要傳入 pipeline、X(以字典格式,使用.to_dict("records"))、y、要使用的折數,以及scoring("neg_mean_squared_error")。 - 列印 10 折 RMSE。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import necessary modules
from sklearn.feature_extraction import DictVectorizer
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
# Fill LotFrontage missing values with 0
X.LotFrontage = ____
# Setup the pipeline steps: steps
steps = [("ohe_onestep", DictVectorizer(sparse=False)),
("xgb_model", xgb.XGBRegressor(max_depth=2, objective="reg:squarederror"))]
# Create the pipeline: xgb_pipeline
xgb_pipeline = ____
# Cross-validate the model
cross_val_scores = ____
# Print the 10-fold RMSE
print("10-fold RMSE: ", np.mean(np.sqrt(np.abs(____))))