我正在使用XGBoost,并试图让它在其内置的交叉验证例程(XGB.cv)中报告自定义指标,我获得了“TypeError:无法解压缩不可迭代的int对象””。我已经做了我相信在互联网上的教程合并告诉我在这种情况下要做的,我想知道如果我发现了一个错误或我正在做一个简单的错误的地方
history = xgb.cv(param, dMatrix, num_boost_round=10,as_pandas = True , nfold=5, custom_metric = myMetric)
字符串
正在调用我自定义的度量函数(创建该函数是为了对精确度和召回率进行不同的加权)
def myMetric(y_pred , dMatrix):
y_true = (dMatrix.get_label())
custom_Score = sklearn.metrics.recall_score(y_true,y_pred) * ((2*sklearn.metrics.precision_score(y_true,y_pred) - 1)))
return custom_Score
型
我获得错误堆栈:
Traceback (most recent call last):
history = xgb.cv(param, dMatrix, num_boost_round=10,as_pandas = True ,nfold=5,custom_metric = myMetric)
File "C:\Program Files\Python38\lib\site-packages\xgboost\training.py", line 540, in cv
should_break = callbacks_container.after_iteration(booster, i, dtrain, None)
File "C:\Program Files\Python38\lib\site-packages\xgboost\callback.py", line 232, in after_iteration
scores = model.eval(epoch, self.metric, self._output_margin)
File "C:\Program Files\Python38\lib\site-packages\xgboost\training.py", line 233, in eval
result = [f.eval(iteration, feval, output_margin) for f in self.cvfolds]
File "C:\Program Files\Python38\lib\site-packages\xgboost\training.py", line 233, in <listcomp>
result = [f.eval(iteration, feval, output_margin) for f in self.cvfolds]
File "C:\Program Files\Python38\lib\site-packages\xgboost\training.py", line 219, in eval
return self.bst.eval_set(self.watchlist, iteration, feval, output_margin)
File "C:\Program Files\Python38\lib\site-packages\xgboost\core.py", line 2010, in eval_set
name, val = feval_ret
TypeError: cannot unpack non-iterable numpy.float64 object
型
引用的源代码是(函数中最后一个if语句抛出的错误):
def eval_set(
self,
evals: Sequence[Tuple[DMatrix, str]],
iteration: int = 0,
feval: Optional[Metric] = None,
output_margin: bool = True
) -> str:
# pylint: disable=invalid-name
"""Evaluate a set of data.
Parameters
----------
evals :
List of items to be evaluated.
iteration :
Current iteration.
feval :
Custom evaluation function.
Returns
-------
result: str
Evaluation result string.
"""
for d in evals:
if not isinstance(d[0], DMatrix):
raise TypeError(f"expected DMatrix, got {type(d[0]).__name__}")
if not isinstance(d[1], str):
raise TypeError(f"expected string, got {type(d[1]).__name__}")
self._validate_dmatrix_features(d[0])
dmats = c_array(ctypes.c_void_p, [d[0].handle for d in evals])
evnames = c_array(ctypes.c_char_p, [c_str(d[1]) for d in evals])
msg = ctypes.c_char_p()
_check_call(
_LIB.XGBoosterEvalOneIter(
self.handle,
ctypes.c_int(iteration),
dmats,
evnames,
c_bst_ulong(len(evals)),
ctypes.byref(msg),
)
)
assert msg.value is not None
res = msg.value.decode() # pylint: disable=no-member
if feval is not None:
for dmat, evname in evals:
feval_ret = feval(
self.predict(dmat, training=False, output_margin=output_margin), dmat
)
if isinstance(feval_ret, list):
for name, val in feval_ret:
# pylint: disable=consider-using-f-string
res += "\t%s-%s:%f" % (evname, name, val)
else:
name, val = feval_ret
# pylint: disable=consider-using-f-string
res += "\t%s-%s:%f" % (evname, name, val)
return res
型
你能看出问题所在吗?是在我这边吗
我还尝试在myMetric中将所有内容转换为int类型(只是为了查看),结果收到“cannot unpack non-iterable type int”
有关CV函数的信息,请访问https://xgboost.readthedocs.io/en/stable/python/python_api.html。以下是使用自定义指标https://xgboost.readthedocs.io/en/latest/tutorials/custom_metric_obj.html的后续内容
1条答案
按热度按时间luaexgnf1#
我对你们这些善良的人撒谎...答案是'simple',它在文档中,应该返回一个字符串+一个输出值!在定义中做这一行是安全的让它看起来像是医生
看起来像:
字符串