对 XGBoost 模型进行交叉验证
在本练习中,您将在此前构建的管道基础上更进一步,同时完成预处理并对模型进行交叉验证。
本练习是课程的一部分
使用 XGBoost 的极端梯度提升
练习说明
- 使用
steps创建名为xgb_pipeline的管道。 - 使用
cross_val_score()进行 10 折交叉验证。您需要传入管道、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(____))))