5. Metrics and scoring: evaluating the rankers#
sklr.metrics implements functions that compare the true rankings of some
samples with the rankings that an estimator predicts for them, and the scorers that
bring these functions to the model selection tools of scikit-learn. Each function
computes a correlation or a distance between the true and the predicted ranking of
each sample, and returns their average, which may be weighted with
sample_weight.
Each metric is written as a function of two rankings, such as \(\tau(\hat{\pi}, \sigma)\) for the predicted ranking \(\hat{\pi}\) and the true ranking \(\sigma\) of a sample, with the notation of Notation. The rankings follow the representation described in Ranking representation, and they must be complete: the metrics reject rankings with unranked labels, in the true rankings as well as in the predicted ones.
5.1. Scoring#
The score method of the label rankers computes Kendall’s \(\tau\), and
that of the partial label rankers computes the \(\tau_x\) coefficient, both
described below. The model selection tools of scikit-learn use it unless they are
given a scorer with the scoring parameter.
scikit-learn only accepts the names of its own scorers as values of scoring,
so the scorers of scikit-lr are passed as objects, which get_scorer() gets
from their names:
Name |
Function |
|---|---|
|
|
|
|
|
|
|
As in scikit-learn, greater values of a scorer are better, so the scorer of a
distance returns it negated. get_scorer_names() lists the names:
>>> from sklr.metrics import get_scorer, get_scorer_names
>>> get_scorer_names()
['kendall_tau', 'neg_kendall_distance', 'spearman_rho', 'tau_x']
>>> get_scorer("neg_kendall_distance")
make_scorer(kendall_distance, greater_is_better=False, response_method='predict')
The scorer is then passed as scoring, for instance with
cross_val_score(ranker, X, y, scoring=get_scorer("kendall_tau")). When metadata
routing is enabled, the scorers receive sample_weight if it is requested with
their set_score_request method.
5.2. Label ranking metrics#
The metrics of label ranking compare rankings without ties, and they reject the rankings with tied labels.
5.2.1. Kendall’s \(\tau\)#
kendall_tau_score() computes Kendall’s \(\tau\) [kendall_new_1938],
which scores each pair of labels with +1 if the true and the predicted ranking
order it in the same way (a concordant pair) and with -1 otherwise (a
discordant pair). Let \(a_{ij}(\pi)\) be 1 if \(\pi(i) < \pi(j)\) and
-1 if \(\pi(i) > \pi(j)\). Then
which ranges from -1, when one ranking is the reverse of the other, to 1,
when both rankings are equal:
>>> from sklr.metrics import kendall_tau_score
>>> y_true = [[1, 2, 3], [3, 2, 1]]
>>> y_pred = [[1, 2, 3], [1, 3, 2]]
>>> kendall_tau_score(y_true, y_pred)
0.333...
Without ties, all the variants of Kendall’s \(\tau\), such as \(\tau_a\) and \(\tau_b\), are the same. They differ in how they handle ties, which is why the metrics of partial label ranking use the \(\tau_x\) coefficient instead, described in Partial label ranking metrics.
5.2.2. Kendall distance#
kendall_distance() computes the Kendall distance, the number of discordant
pairs of labels,
which is also the minimum number of swaps of adjacent labels that turn one ranking
into the other. By default, it is divided by the number of pairs,
\(n(n - 1) / 2\), so that it ranges from 0 to 1 and is equal to
\((1 - \tau(\hat{\pi}, \sigma)) / 2\):
>>> from sklr.metrics import kendall_distance
>>> kendall_distance(y_true, y_pred)
0.333...
>>> kendall_distance(y_true, y_pred, normalize=False)
1.0
5.2.3. Spearman’s \(\rho\)#
spearman_rho_score() computes Spearman’s rank correlation coefficient
\(\rho\) [spearman_proof_1904], the product-moment correlation between the
positions that the true and the predicted ranking give to each label. Without
ties, it is
an affine transformation of the sum of the squared differences of the
positions, and it ranges from -1, when one ranking is the reverse of the other,
to 1, when both rankings are equal:
>>> from sklr.metrics import spearman_rho_score
>>> spearman_rho_score(y_true, y_pred)
0.25
scipy.stats.spearmanr() computes the same coefficient. It penalizes long
moves of a label more than Kendall’s \(\tau\): moving a label \(k\)
positions, which shifts the \(k\) labels in between by one position, makes
\(k\) discordant pairs, but adds \(k^2 + k\) to the sum of the squared
differences.
5.3. Partial label ranking metrics#
The metrics of partial label ranking compare rankings that may have ties.
5.3.1. \(\tau_x\)#
tau_x_score() computes the \(\tau_x\) rank correlation coefficient
[emond_new_2002], which extends Kendall’s \(\tau\) to rankings with ties.
Let \(a'_{ij}(\pi)\) be 1 if \(\pi(i) \leq \pi(j)\), that is, if label
\(y_i\) is ahead of or tied with label \(y_j\), and -1 if
\(\pi(i) > \pi(j)\), for \(i \neq j\). Then
Unlike Kendall’s \(\tau_b\), its denominator does not depend on the ties, so
a ranking with all its labels tied has a coefficient of 1 with itself and of
0 with any ranking without ties. For rankings without ties, it is equal to
Kendall’s \(\tau\):
>>> from sklr.metrics import tau_x_score
>>> tau_x_score([[1, 1, 1]], [[1, 1, 1]])
1.0
>>> tau_x_score([[1, 1, 1]], [[1, 2, 3]])
0.0
>>> tau_x_score(y_true, y_pred)
0.333...
The \(\tau_x\) coefficient is also equivalent to the Kemeny-Snell distance \(D_{\mathrm{KS}}\) between rankings with ties, by
5.4. Dummy estimators#
A simple sanity check of a ranker is to compare it with a ranker that does not
look at the features. DummyLabelRanker and DummyPartialLabelRanker
predict the same ranking \(\hat{\pi}\) for every sample
\(\boldsymbol{x} \in \mathcal{X}\), chosen by their strategy:
"consensus"predicts the consensus of the training rankings, which may be weighted withsample_weightand be incomplete. InDummyLabelRanker, it is the center ranking of the Mallows model or the ranking that sorts the labels by decreasing parameter of the Plackett-Luce model, as chosen by itsmodel. InDummyPartialLabelRanker, it is the bucket order found by the Bucket Pivot Algorithm, so that the ranker is the Zero-R baseline of [alfaro_learning_2021]."constant"predicts a ranking given by the user withconstant.
With both strategies, predict ignores the input data, apart from its number of
samples. On synthetic rankings that depend on the features, a ranker that uses
them, such as KNeighborsLabelRanker, scores well above
the consensus:
>>> import numpy as np
>>> from scipy.stats import rankdata
>>> from sklearn.model_selection import train_test_split
>>> from sklr.dummy import DummyLabelRanker
>>> from sklr.neighbors import KNeighborsLabelRanker
>>> rng = np.random.RandomState(0)
>>> X = rng.uniform(-1, 1, size=(200, 2))
>>> utilities = X @ rng.normal(size=(2, 4)) + 0.3 * rng.normal(size=(200, 4))
>>> y = rankdata(-utilities, axis=1)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
>>> dummy = DummyLabelRanker().fit(X_train, y_train)
>>> dummy.ranking_
array([3, 4, 1, 2])
>>> round(dummy.score(X_test, y_test), 3)
-0.093
>>> ranker = KNeighborsLabelRanker().fit(X_train, y_train)
>>> round(ranker.score(X_test, y_test), 3)
0.52
A ranker that does not clearly beat the consensus has probably not learned anything from the features: they may not be informative, or its parameters may not be well tuned.
References
M. G. Kendall, “A new measure of rank correlation”, Biometrika, vol. 30, no. 1/2, pp. 81-93, 1938.
C. Spearman, “The proof and measurement of association between two things”, The American Journal of Psychology, vol. 15, no. 1, pp. 72-101, 1904.
E. J. Emond and D. W. Mason, “A new rank correlation coefficient with application to the consensus ranking problem”, Journal of Multi-Criteria Decision Analysis, vol. 11, pp. 17-28, 2002.