Ale*_*lli 6 regression machine-learning python-3.x xgboost
我正在使用 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))
Run Code Online (Sandbox Code Playgroud)
它正确地训练自己,但打印功能引发以下错误,
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)
如何获得模型的最佳 迭代次数?
此外,我怎么能打印训练 误差的每 一轮?
Era*_*she 10
对于您的 TypeError:使用get_booster()而不是 booster()
print("Best Iteration: {}".format(clf.get_booster().best_iteration))
Run Code Online (Sandbox Code Playgroud)
要在predict时使用最佳迭代次数,您有一个名为的参数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)
Run Code Online (Sandbox Code Playgroud)
如果您在.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)
Run Code Online (Sandbox Code Playgroud)
注意: early_stopping_rounds 参数应该在.fit()命令中而不是在XGBRegressor()实例化中。
另一个注意: verbose = 50 inXGBRegressor()是多余的。该verbose变量应该在您的.fit()函数中并且是 True 或 False。对于 verbose=True 的作用,请阅读此处的详细部分。它直接影响你的第三个问题。