kendall_distance#

sklr.metrics.kendall_distance(y_true: ArrayLike, y_pred: ArrayLike, *, normalize: bool = True, sample_weight: ArrayLike | None = None) → float#

Compute the Kendall distance between label rankings.

The Kendall distance between two rankings of the same labels is the number of pairs of labels that they order in different ways (the discordant pairs) [kendall_new_1938], which is also the minimum number of swaps of adjacent labels that turn one ranking into the other. The distance of the rankings is the average of the distances of their samples.

Read more in the User Guide.

Parameters:
y_truearray-like of shape (n_samples, n_labels)

The true rankings, complete and without ties, as described in Ranking representation.

y_predarray-like of shape (n_samples, n_labels)

The predicted rankings, complete and without ties.

normalizebool, default=True

If True, divide the distance of each sample by the number of pairs of labels, n_labels * (n_labels - 1) / 2, so that it ranges from 0 to 1. Otherwise, it is the number of discordant pairs.

sample_weightarray-like of shape (n_samples,), default=None

Non-negative weights of the samples in the average. If None, the samples are equally weighted.

Returns:
distancefloat

The average Kendall distance of the samples. It is 0 when the rankings are equal.

See also

kendall_tau_score

Compute Kendall’s \(\tau\) between label rankings.

Notes

The normalized distance \(d\) is related to Kendall’s \(\tau\) by \(d = (1 - \tau) / 2\), sample by sample. Rankings with ties are rejected, because a pair of labels that is tied in one ranking is neither concordant nor discordant.

References

[kendall_new_1938]

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

Examples

>>> from sklr.metrics import kendall_distance
>>> y_true = [[1, 2, 3], [3, 2, 1]]
>>> y_pred = [[1, 2, 3], [1, 3, 2]]
>>> kendall_distance(y_true, y_pred)
0.333...
>>> kendall_distance(y_true, y_pred, normalize=False)
1.0