gpt4 book ai didi

python - make_scorer 给出错误 : 'str' object is not callable make_scorer

转载 作者:行者123 更新时间:2023-12-01 02:53:36 24 4
gpt4 key购买 nike

我正在使用以下代码来优化随机森林算法,但这会引发类型错误:“str”对象不可调用。您能帮我找出可能的原因吗?拟合线在 Scorer.py 文件中抛出错误“score = Scorer(estimator, X_test, y_test)”

from sklearn.grid_search import GridSearchCV
from sklearn.metrics import roc_auc_score, make_scorer
clf_scorer = make_scorer('roc_auc')
rfc = RandomForestClassifier(n_estimators=100,oob_score=True)
param_grid = {
'max_depth':[4,8,12],
}

cv_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv=5,
scoring=clf_scorer)
cv_rfc.fit(train_data,target)

以下是 cross_validation.py 中给出错误的代码:

def _score(estimator, X_test, y_test, scorer):
"""Compute the score of an estimator on a given test set."""
if y_test is None:
score = scorer(estimator, X_test)
else:
**score = scorer(estimator, X_test, y_test)**
if hasattr(score, 'item'):
try:
# e.g. unwrap memmapped scalars
score = score.item()
except ValueError:
# non-scalar?
pass
if not isinstance(score, numbers.Number):
raise ValueError("scoring must return a number, got %s (%s) instead."
% (str(score), type(score)))
return score

最佳答案

将第三行更改为:

clf_scorer = make_scorer(roc_auc_score)

第一个参数 score_func 需要是一个可调用的评分函数。

关于python - make_scorer 给出错误 : 'str' object is not callable make_scorer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44476187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com