Implementation:Scikit learn Scikit learn Make Scorer
Metadata
- Domains: Statistics, Model_Evaluation
- Source File:
sklearn/metrics/_scorer.py - Last Updated: 2026-02-08 15:00 GMT
Overview
Concrete tool for converting metric functions into scorer callables provided by scikit-learn. This implementation covers two complementary APIs: make_scorer, which constructs custom scorer objects from arbitrary metric or loss functions, and get_scorer_names, which lists all pre-defined scorer names available in the registry.
API Signatures
make_scorer
from sklearn.metrics import make_scorer
make_scorer(
score_func,
*,
response_method="predict",
greater_is_better=True,
**kwargs
)
Parameters:
- score_func (callable) -- Score function (or loss function) with signature
score_func(y, y_pred, **kwargs). - response_method (str or list/tuple of str, default="predict") -- Specifies which estimator method to use to obtain predictions. Valid options:
"predict","predict_proba","decision_function". If a list or tuple, the first available method on the estimator is used. (Added in version 1.4.) - greater_is_better (bool, default=True) -- Whether
score_funcis a score function (high is good) or a loss function (low is good). WhenFalse, the scorer object will negate the outcome ofscore_func. - **kwargs -- Additional parameters to be passed to
score_funcat every invocation.
Returns:
- scorer (callable) -- A callable object with signature
scorer(estimator, X, y_true, **kwargs)that returns a scalar score where greater is better.
Example -- Custom F-beta scorer:
from sklearn.metrics import fbeta_score, make_scorer
# Create an F2 scorer (beta=2 emphasizes recall)
ftwo_scorer = make_scorer(fbeta_score, beta=2)
# Use with GridSearchCV
from sklearn.model_selection import GridSearchCV
from sklearn.svm import LinearSVC
grid = GridSearchCV(
LinearSVC(), param_grid={'C': [1, 10]},
scoring=ftwo_scorer
)
Example -- Loss function with sign flipping:
from sklearn.metrics import mean_squared_error, make_scorer
# mean_squared_error is a loss (lower is better), so negate it
neg_mse_scorer = make_scorer(mean_squared_error, greater_is_better=False)
Example -- Probability-based scorer:
from sklearn.metrics import log_loss, make_scorer
# log_loss needs predict_proba and is a loss function
neg_log_loss_scorer = make_scorer(
log_loss,
response_method="predict_proba",
greater_is_better=False,
)
get_scorer_names
from sklearn.metrics import get_scorer_names
get_scorer_names()
Parameters: None.
Returns:
- list of str -- Sorted list of all available pre-defined scorer names. These names can be passed as strings to any
scoringparameter in scikit-learn or toget_scorer()to retrieve the corresponding scorer object.
Example:
from sklearn.metrics import get_scorer_names
all_scorers = get_scorer_names()
print(type(all_scorers)) # <class 'list'>
print(all_scorers[:3]) # ['accuracy', 'adjusted_mutual_info_score', 'adjusted_rand_score']
print("roc_auc" in all_scorers) # True
Sign Convention Summary
| Metric Type | greater_is_better |
Pre-defined Name Example | Effect |
|---|---|---|---|
| Score (higher is better) | True |
'accuracy', 'r2' |
Score passed through unchanged |
| Loss (lower is better) | False |
'neg_mean_squared_error' |
Score is negated (multiplied by -1) |
When using pre-defined scorer names with the neg_ prefix, the sign has already been flipped. When constructing a custom scorer with make_scorer, you must set greater_is_better=False for loss functions to achieve the same effect.