使用xgboost分类器进行二进制分类时,我想创建自己的f_score评估指标。
我的指标是
def f_score(preds,y):
df = pd.DataFrame({'predict':pred, 'true':y.get_labels()})
correct_1 = len(df.loc[(df['predict'] == 1) & (df['true'] == 1])
wrong_1 = len(df.loc[(df['predict'] == 0]) & (df['true'] == 0])
wrong_0 = len(df.loc[(df['predict'] == 0]) & (df['true'] == 1])
precission = correct_1 / (correct_1+wrong_1)
recall = correct_1/(correct_1+wrong_0)
score = 2*precission*recall/(precission+recall)
return score
然后我有
cv_results = xgboost.cv(
params,
dtrain,
num_boost_round = 999,
seed = 42,
nfold = 5,
feval = f_score,
early_stopping_rounds = 10
)
当我运行代码时,它将对构造的指标执行一次迭代,然后在返回“分数”后抛出“ TypeError:无法解压缩不可迭代的float对象”
我不知道为什么会这样。知道如何解决吗?
File "c:\Users\esilkas\.vscode\extensions\ms-python.python-
2019.2.5433\pythonFiles\lib\python\ptvsd\__main__.py", line 357, in main
run()
File "c:\Users\esilkas\.vscode\extensions\ms-python.python-
2019.2.5433\pythonFiles\lib\python\ptvsd\__main__.py", line 257, in run_file
runpy.run_path(target, run_name='__main__')
File
"C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\runpy.py", line
263, in run_path
pkg_name=pkg_name, script_name=fname)
File
"C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\runpy.py", line
96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File
"C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\runpy.py", line
85, in _run_code
exec(code, run_globals)
File
"c:\Users\esilkas\Documents\apps\masterthesis\Model\xgb_tuning_outlier.py",
line 293, in <module>
feval = f_score,
File "C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\site-
packages\xgboost\training.py", line 446, in cv
res = aggcv([f.eval(i, feval) for f in cvfolds])
File "C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\site-
packages\xgboost\training.py", line 446, in <listcomp>
res = aggcv([f.eval(i, feval) for f in cvfolds])
File "C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\site-
packages\xgboost\training.py", line 234, in eval
return self.bst.eval_set(self.watchlist, iteration, feval)
File "C:\Users\esilkas\AppData\Local\Programs\Python\Python37\lib\site-
packages\xgboost\core.py", line 1115, in eval_set
name, val = feval_ret
TypeError: cannot unpack non-iterable float object