【发布时间】:2017-06-12 23:16:26
【问题描述】:
我正在使用以下代码来优化随机森林算法,但这会引发类型错误:'str' object is not callable。你能帮我确定可能是什么原因吗?拟合线在 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
【问题讨论】:
-
该行不在您发布的代码中。请发布实际代码。
-
另外,请始终发布错误的堆栈跟踪。错误的实际代码是一个加号,但并不总是需要。
标签: python string typeerror grid-search