Search code examples
pythonstringtypeerrorgrid-search

make_scorer giving an error: 'str' object is not callable make_scorer


I am using the following code to optimize a random forest algorithm but this is throwing a typeerror: 'str' object is not callable. Could you please help me in identifying what could be the cause? The fit line is throwing the error in the scorer.py file for this one "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)

Following is the code from cross_validation.py that's giving the error:

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

Solution

  • Change your third line to:

    clf_scorer = make_scorer(roc_auc_score)
    

    The first argument, score_func, needs to be a callable scoring function.