我正在使用以下代码来优化随机森林算法,但这会抛出一个类型错误:'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
答案 0 :(得分:2)
将第三行更改为:
clf_scorer = make_scorer(roc_auc_score)
第一个参数score_func
需要是一个可调用的评分函数。