0

我想知道在为分类模型调整超参数时是否也可以使用以下代码,因为代码最初是为回归设置创建的。我了解目标需要更改为“objective='binary:logistic'”

    params = {
    'max_depth':6,
    'min_child_weight': 1,
    'eta':.1,
    'subsample': 1,
    'colsample_bytree': 1,
    'objective':'reg:squarederror',
}

num_boost_round = 999

model = xgb.train(
    params,
    X_train_std_dm,
    num_boost_round=num_boost_round,
    evals=[(X_test_std_dm, "Test")],
    early_stopping_rounds=5
)

best_numboostround = model.best_iteration
best_rmsescore = model.best_score

print("\n\tBest RMSE Score {:.2f}".format(best_rmsescore))
print("\tBest Number of Boosting Rounds {}".format(best_numboostround))

这部分代码使用了 best_score 和 best_iteration。据我所知,RMSE 不足以用于分类模型。代码的第二部分在下面,还查看了 RMSE。我认为应该使用分类模型 ROC_AUC,但超参数调整也是如此吗?是否有可能获得更好的理解,如果可能的话,如何修改此代码以与分类 XGBoost 相关?

gridsearch_params = [
    (max_depth, min_child_weight)
    for max_depth in range(8,10)
    for min_child_weight in range(6,8)
]

min_rmse = float("Inf")
best_params = None
for max_depth, min_child_weight in gridsearch_params:
    print("CV with max_depth={}, min_child_weight={}".format(
                             max_depth,
                             min_child_weight))
    # Update parameters
    params['max_depth'] = max_depth
    params['min_child_weight'] = min_child_weight
    # Run CV
    cv_results = xgb.cv(
        params,
        X_train_std_dm,
        num_boost_round=num_boost_round,
        seed=42,
        nfold=5,
        metrics={'rmse'},
        early_stopping_rounds=5
    )
    # Update best RMSE
    mean_rmse = cv_results['test-rmse-mean'].min()
    boost_rounds = cv_results['test-rmse-mean'].argmin()
    print("\tRMSE {:.2f} for {} rounds".format(mean_rmse, boost_rounds))
    if mean_rmse < min_rmse:
        min_rmse = mean_rmse
        best_params = (max_depth,min_child_weight)
print("Best params: {}, {}, RMSE: {:.2f}".format(best_params[0], best_params[1], min_rmse))
4

0 回答 0