부스팅 라운드 수 튜닝하기
먼저, 파라미터 튜닝의 시작으로 부스팅 라운드 수(생성하는 트리 개수)가 XGBoost 모델의 홀드아웃 성능에 어떤 영향을 주는지 살펴보겠습니다. for 루프 안에서 xgb.cv()를 사용해 num_boost_round 값마다 하나의 모델을 학습할 거예요.
여기서는 Ames 주택 데이터셋을 계속 사용합니다. 특성은 배열 X에, 타깃 벡터는 y에 들어 있습니다.
이 연습은 강의의 일부입니다
XGBoost로 익히는 Extreme Gradient Boosting
연습 안내
X와y로부터housing_dmatrix라는DMatrix를 생성하세요.params라는 파라미터 딕셔너리를 만들고, 적절한"objective"("reg:squarederror")와"max_depth"(값은3)을 설정하세요.for루프에서num_rounds를 순회하며 3-겹 교차 검증을 수행하세요. 루프의 각 반복에서 현재 부스팅 라운드 수(curr_num_rounds)를xgb.cv()의num_boost_round인수로 전달하세요.- 교차 검증된 각 XGBoost 모델의 마지막 부스팅 라운드 RMSE를
final_rmse_per_round리스트에 추가하세요. num_rounds와final_rmse_per_round는 이미 zip으로 묶여 DataFrame으로 변환되어 있어, 라운드별 모델 성능을 쉽게 확인할 수 있어요. 결과를 보려면 'Submit Answer'를 눌러 주세요!
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Create the DMatrix: housing_dmatrix
housing_dmatrix = ____
# Create the parameter dictionary for each tree: params
params = {"____":"____", "____":____}
# Create list of number of boosting rounds
num_rounds = [5, 10, 15]
# Empty list to store final round rmse per XGBoost model
final_rmse_per_round = []
# Iterate over num_rounds and build one model per num_boost_round parameter
for curr_num_rounds in num_rounds:
# Perform cross-validation: cv_results
cv_results = ____(dtrain=____, params=____, nfold=3, num_boost_round=____, metrics="rmse", as_pandas=True, seed=123)
# Append final round RMSE
____.____(cv_results["test-rmse-mean"].tail().values[-1])
# Print the resultant DataFrame
num_rounds_rmses = list(zip(num_rounds, final_rmse_per_round))
print(pd.DataFrame(num_rounds_rmses,columns=["num_boosting_rounds","rmse"]))