XGBoost 모델 교차 검증하기
이번 연습에서는, 앞에서 만든 파이프라인을 사용해 전처리와 모델의 교차 검증을 함께 수행해 보겠습니다.
이 연습은 강의의 일부입니다
XGBoost로 익히는 Extreme Gradient Boosting
연습 안내
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(____))))