get_scorer#

sklr.metrics.get_scorer(scoring: str | Callable | None) → Any#

Get a scorer of label ranking or partial label ranking from its name.

The scorers can be passed to the scoring parameter of the model selection tools of scikit-learn, such as sklearn.model_selection.cross_val_score() and sklearn.model_selection.GridSearchCV, which do not accept their names.

Read more in the User Guide.

Parameters:
scoringstr, callable or None

The name of the scorer, among those returned by get_scorer_names(). If it is a callable or None, it is returned as is.

Returns:
scorercallable or None

The scorer, which is called as scorer(estimator, X, y).

Raises:
ValueError

If scoring is not the name of a scorer.

See also

get_scorer_names

Get the names of all the scorers.

sklearn.metrics.make_scorer

Make a scorer from a metric.

Notes

When passed a string, this function always returns a copy of the scorer, so calling it twice with the same name gives two separate scorers.

Examples

>>> import numpy as np
>>> from sklearn.base import BaseEstimator
>>> from sklr.base import LabelRankerMixin
>>> from sklr.metrics import get_scorer
>>> class FirstRanker(LabelRankerMixin, BaseEstimator):
...     def fit(self, X, y):
...         self.ranking_ = np.asarray(y)[0]
...         return self
...     def predict(self, X):
...         return np.tile(self.ranking_, (len(X), 1))
>>> X = [[0], [1]]
>>> y = [[1, 2, 3], [3, 2, 1]]
>>> ranker = FirstRanker().fit(X, y)
>>> scorer = get_scorer("kendall_tau")
>>> scorer(ranker, X, y)
0.0