我正在尝试在数据集上使用 xgboost。我在各种博客中看到了相同的语法,但在调用 clf.evals_result() 时出现错误
这是我的代码
from xgboost import XGBRegressor as xgb
from sklearn.metrics import mean_absolute_error as mae
evals_result ={}
eval_s = [(x, y),(xval,yval)]
clf = xgb(n_estimators=100,learning_rate=0.03,tree_method='gpu_hist',lamda=0.1,eval_metric='mae',eval_set=eval_s,early_stopping_rounds=0,evals_result=evals_result)
clf.fit(x,y)
r = clf.evals_result()
这是我收到的错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-138-2d6867968043> in <module>
1
----> 2 r = clf.evals_result()
3
4 p = clf.predict(xval)
/opt/conda/lib/python3.6/site-packages/xgboost/sklearn.py in evals_result(self)
399 'validation_1': {'logloss': ['0.41965', '0.17686']}}
400 """
--> 401 if self.evals_result_:
402 evals_result = self.evals_result_
403 else:
AttributeError: 'XGBRegressor' object has no attribute 'evals_result_'
最佳答案
我得到了完全相同的错误,解决方案是将 eval_set 传递给 fit 函数,而不是在创建分类器时
clf.fit(x,y,eval_set=eval_s)
然后你可以运行 clf.evals_result()
关于python-3.x - XGBModel' 对象没有属性 'evals_result_',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52815702/