make_scorer giving an error: 'str' object is not callable make_scorer
make_scorer giving an error: 'str' object is not callable make_scorer
我正在使用以下代码来优化随机森林算法,但这会引发类型错误:'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
需要是可调用的评分函数。
我正在使用以下代码来优化随机森林算法,但这会引发类型错误:'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
需要是可调用的评分函数。