Cross-validate mô hình XGBoost của bạn
Trong bài này, bạn sẽ tiến thêm một bước bằng cách dùng pipeline đã tạo để vừa tiền xử lý dữ liệu và cross-validate mô hình của mình.
Bài tập này là một phần của khóa học
Gradient Boosting Cực Mạnh với XGBoost
Hướng dẫn bài tập
- Tạo một pipeline tên
xgb_pipelinebằngsteps. - Thực hiện cross-validation 10-fold bằng
cross_val_score(). Bạn cần truyền vào pipeline,X(dưới dạng dictionary, dùng.to_dict("records")),y, số lượng fold muốn dùng, vàscoring("neg_mean_squared_error"). - In ra RMSE của 10-fold.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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(____))))