我想使用XGBoost的early_stopping_rounds
参数来进行不过拟合的训练。为此,我使用了以下代码:
parameters = {'nthread': 4,'objective': 'binary:logistic','learning_rate': 0.06,'max_depth': 6,'min_child_weight': 3, 'silent': 0,'gamma': 0,'subsample': 0.7,'colsample_bytree': 0.5,'n_estimators': 5, 'missing': -999,'scale_pos_weight': scale_pos_weight,'seed': 4789,'eval_metric':'auc','early_stopping_rounds': 100}X_train, X_test, y_train, y_test =train_test_split(train_feature,train_label, test_size=0.3, random_state=4789)dtrain = xgb.DMatrix(X_train, label=y_train)dtest = xgb.DMatrix(X_test, label=y_test)evallist = [(dtest, 'eval'), (dtrain, 'train')]bst = xgb.train(parameters, dtrain,num_boost_round=1500, evals=evallist)
当打印中间结果时,我得到了如下日志:
[1469] eval-auc:0.912417 train-auc:0.986104[16:04:23] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 110 extra nodes, 0 pruned nodes, max_depth=6[1470] eval-auc:0.912412 train-auc:0.986118[16:04:27] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 102 extra nodes, 0 pruned nodes, max_depth=6[1471] eval-auc:0.912405 train-auc:0.986129[16:04:30] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 116 extra nodes, 0 pruned nodes, max_depth=6[1472] eval-auc:0.912383 train-auc:0.986143[16:04:34] src/tree/updater_prune.cc:74: tree pruning end, 1 roots, 116 extra nodes, 0 pruned nodes, max_depth=6[1473] eval-auc:0.912375 train-auc:0.986159
现在我想知道这种训练结果是否正确?如何检测我的模型是否过拟合,以及应该选择多少轮次?
回答:
正如@XXX所说,你看到的结果是正确的——你的模型刚开始过拟合。
关于你的第二个问题,early_stopping_rounds
参数的工作方式是在N轮之后如果评估AUC没有改善就停止训练(N是early_stopping_rounds
的值)。请注意,中间评估AUC值可能会下降,但只要在最后N轮中有任何绝对改善,训练就会继续进行。
在你的例子中,第1469轮的评估AUC值达到了最大值,因此训练不会在第1569轮之前停止(按照设定,100轮之后)。
最后,达到的最佳轮次应该存储在你示例中的bst
变量中。