4. 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.

4.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

"kendall_tau"

kendall_tau_score()

"tau_x"

tau_x_score()

"neg_kendall_distance"

kendall_distance()

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', '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.

4.2. Label ranking metrics#

The metrics of label ranking compare rankings without ties, and they reject the rankings with tied labels.

4.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

\[\tau(\hat{\pi}, \sigma) = \frac{2}{n(n - 1)} \sum_{i < j} a_{ij}(\hat{\pi}) \, a_{ij}(\sigma),\]

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.

4.2.2. Kendall distance#

kendall_distance() computes the Kendall distance, the number of discordant pairs of labels,

\[D(\hat{\pi}, \sigma) = |\{(i, j) : i < j, \, (\hat{\pi}(i) - \hat{\pi}(j)) (\sigma(i) - \sigma(j)) < 0\}|,\]

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

4.3. Partial label ranking metrics#

The metrics of partial label ranking compare rankings that may have ties.

4.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

\[\tau_x(\hat{\pi}, \sigma) = \frac{1}{n(n - 1)} \sum_{i \neq j} a'_{ij}(\hat{\pi}) \, a'_{ij}(\sigma).\]

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

\[\tau_x(\hat{\pi}, \sigma) = 1 - \frac{2 D_{\mathrm{KS}}(\hat{\pi}, \sigma)}{n(n - 1)}.\]

4.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 with sample_weight and be incomplete. In DummyLabelRanker, 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 its model. In DummyPartialLabelRanker, 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 with constant.

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

[kendall_new_1938]

M. G. Kendall, “A new measure of rank correlation”, Biometrika, vol. 30, no. 1/2, pp. 81-93, 1938.

[emond_new_2002]

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.