小编Ayu*_*wal的帖子

XGBModel' 对象没有属性 'evals_result_'

我正在尝试在数据集上使用 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()
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误

---------------------------------------------------------------------------
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_'
Run Code Online (Sandbox Code Playgroud)

machine-learning python-3.x scikit-learn xgboost

5
推荐指数
1
解决办法
4598
查看次数

如果要直接使用self而不是class对象,python函数是否需要self

我有下面的类和其中定义的功能。

class utils:
    def pass_hash(unhashed):
        hashed = hashlib.sha256(unhashed)
        hashed = hashed.hexdigest()
        return hashed
Run Code Online (Sandbox Code Playgroud)

当我打电话

print(utils.pass_hash('abc'.encode()))
Run Code Online (Sandbox Code Playgroud)

它工作正常,但如果我打电话

obj = utils()
print(obj.pass_hash('abc'.encode()))
Run Code Online (Sandbox Code Playgroud)

它给出以下错误:

  print(obj.pass_hash('abc'.encode()))
TypeError: pass_hash() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)

而如果我在函数中传递自变量,则这种行为会逆转,即它可以很好地与对象配合使用,但是在访问时像utils.pass_hash()一样会给出错误。

有人可以解释一下这种行为吗?

python python-3.x

-2
推荐指数
1
解决办法
47
查看次数