本文介绍了XGBoost最佳迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XGBoost算法进行回归,

I am running a regression using the XGBoost Algorithm as,

clf = XGBRegressor(eval_set = [(X_train, y_train), (X_val, y_val)],
                       early_stopping_rounds = 10, 
                       n_estimators = 10,                    
                       verbose = 50)

clf.fit(X_train, y_train, verbose=False)
print("Best Iteration: {}".format(clf.booster().best_iteration))

它可以正确训练自己,但是打印功能会引发以下错误,

It correctly trains itself, but the print function raises the following error,

TypeError: 'str' object is not callable

如何获取模型的最佳 迭代次数?

此外,如何打印每个 一轮中的培训 错误?

Furthermore, how can I print the training error of each round?

推荐答案

对于您的TypeError:使用 get_booster()而不是booster()

For your TypeError: use get_booster() instead of booster()

print("Best Iteration: {}".format(clf.get_booster().best_iteration))

要在预测时使用最佳迭代次数,请使用一个名为ntree_limit的参数,该参数指定要使用的增强器数量.从训练过程中生成的值是best_ntree_limit,可以在以下事项中训练模型后调用该值:clg.get_booster().best_ntree_limit.更具体地说,当您进行预测时,请使用:

To use the number of the best iteration when you predict, you have a parameter called ntree_limit which specify the number of boosters to use. And the value generated from the training process is best_ntree_limit which can be called after training your model in the following matter: clg.get_booster().best_ntree_limit. More specifically when you predict, use:

best_iteration = clg.get_booster().best_ntree_limit
predict(data, ntree_limit=best_iteration)

如果您在.fit()命令

clf.fit(X_train, y_train,
        eval_set = [(X_train, y_train), (X_val, y_val)],
        eval_metric = 'rmse',
        early_stopping_rounds = 10, verbose=True)

注意::Early_stopping_rounds参数应该在.fit()命令中,而不是在XGBRegressor()实例中.

NOTE: early_stopping_rounds parameter should be in the .fit() command not in the XGBRegressor() instantiation.

另一个注意事项: XGBRegressor()中的verbose = 50是多余的. verbose变量应该在您的.fit()函数中,并且为True或False.对于verbose = True所做的操作,在此处阅读在详细部分下.这直接影响到您的第三个问题.

Another NOTE: verbose = 50 in XGBRegressor() is redundant. The verbose variable should be in your .fit() function and is True or False. For what the verbose=True do, read here under the verbose section. It is directly affects your 3rd question.

这篇关于XGBoost最佳迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:42