XGBoost 最佳迭代
XGBoost Best Iteration
我是 运行 使用 XGBoost 算法的回归,
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))
它正确地训练了自己,但是打印函数引发了以下错误,
TypeError: 'str' object is not callable
如何获取模型的最佳迭代的次数?
此外,如何打印 each training error 轮?
你的错误是 XGBRegressor
的 booster
属性是一个字符串,它指定了要使用的助推器的种类,而不是实际的助推器实例。来自文档:
booster: string
Specify which booster to use: gbtree, gblinear or dart.
为了获得实际的助推器,您可以改为调用 get_booster()
:
>>> clf.booster
'gbtree'
>>> clf.get_booster()
<xgboost.core.Booster object at 0x118c40cf8>
>>> clf.get_booster().best_iteration
9
>>> print("Best Iteration: {}".format(clf.get_booster().best_iteration))
Best Iteration: 9
我不确定你问题的后半部分,即:
Furthermore, how can I print the training error of ** each round**?
但希望您已畅通无阻!
对于您的 TypeError:使用 get_booster() 而不是 booster()
print("Best Iteration: {}".format(clf.get_booster().best_iteration))
要在 预测 时使用最佳迭代次数,您需要一个名为 ntree_limit
的参数,该参数指定要使用的助推器的数量。训练过程中生成的值是best_ntree_limit
,可以在训练模型后调用以下内容:clg.get_booster().best_ntree_limit
。更具体地说,当你预测时,使用:
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()
实例化中。
另一个注意: verbose = 50
in XGBRegressor()
是多余的。 verbose
变量应该在您的 .fit()
函数中并且是 True 或 False。对于 verbose=True 的作用,read here 在 verbose 部分下。它直接影响你的第三个问题。
我是 运行 使用 XGBoost 算法的回归,
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))
它正确地训练了自己,但是打印函数引发了以下错误,
TypeError: 'str' object is not callable
如何获取模型的最佳迭代的次数?
此外,如何打印 each training error 轮?
你的错误是 XGBRegressor
的 booster
属性是一个字符串,它指定了要使用的助推器的种类,而不是实际的助推器实例。来自文档:
booster: string
Specify which booster to use: gbtree, gblinear or dart.
为了获得实际的助推器,您可以改为调用 get_booster()
:
>>> clf.booster
'gbtree'
>>> clf.get_booster()
<xgboost.core.Booster object at 0x118c40cf8>
>>> clf.get_booster().best_iteration
9
>>> print("Best Iteration: {}".format(clf.get_booster().best_iteration))
Best Iteration: 9
我不确定你问题的后半部分,即:
Furthermore, how can I print the training error of ** each round**?
但希望您已畅通无阻!
对于您的 TypeError:使用 get_booster() 而不是 booster()
print("Best Iteration: {}".format(clf.get_booster().best_iteration))
要在 预测 时使用最佳迭代次数,您需要一个名为 ntree_limit
的参数,该参数指定要使用的助推器的数量。训练过程中生成的值是best_ntree_limit
,可以在训练模型后调用以下内容:clg.get_booster().best_ntree_limit
。更具体地说,当你预测时,使用:
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()
实例化中。
另一个注意: verbose = 50
in XGBRegressor()
是多余的。 verbose
变量应该在您的 .fit()
函数中并且是 True 或 False。对于 verbose=True 的作用,read here 在 verbose 部分下。它直接影响你的第三个问题。