0

我正在做超参数调整,我无法将最大 AUC 分数与最佳参数一起打印出来。看我下面的代码。它只打印 'Best params: None, AUC: inf' 而不是 'Best params: 5, AUC: 87.1'

#HyperParameter Tuning
gridsearch_params = [
    max_depth
    for max_depth in range(2,10,1)
]

#initial best params and AUC
max_auc = float("Inf")
best_params = None
for max_depth in gridsearch_params:
    print("CV with max_depth={}".format(
                             max_depth
                             ))
    # Update our parameters
    parameters['max_depth'] = max_depth
    # Run CV
    cv_results = xgb.cv(
        parameters,
        dtrain,
        num_boost_round=num_rounds,
        seed=42,
        nfold=5,
        metrics={'auc'},
        early_stopping_rounds=10,
        verbose_eval=100,
        stratified=False
    )
    # Update best AUC
    mean_auc = max(cv_results['test-auc-mean'])
    boost_rounds = np.argmax((cv_results['test-auc-mean']))
    print("\tAUC {} for {} rounds".format(mean_auc, boost_rounds))
    if mean_auc > max_auc:
        max_auc = mean_auc
        best_params = max_depth


print("Best params: {}, AUC: {}".format(best_params, max_auc))
4

1 回答 1

1

max_auc 应初始化为 float(0) 否则 mean_auc > max_auc 测试将始终返回 False。

于 2020-09-03T04:23:18.463 回答