kendall_tau_score#

sklr.metrics.kendall_tau_score(y_true: ArrayLike, y_pred: ArrayLike, *, sample_weight: ArrayLike | None = None) → float#

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

Kendall’s \(\tau\) [kendall_new_1938] compares two rankings of the same labels by scoring each pair of labels with +1 if both rankings order it in the same way (a concordant pair) and with -1 otherwise (a discordant pair), and dividing the total score by the number of pairs. It ranges from -1, when one ranking is the reverse of the other, to 1, when both rankings are equal. The score of the rankings is the average of the coefficients 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.

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:
scorefloat

The average Kendall’s \(\tau\) of the samples, between -1 and 1.

See also

tau_x_score

Compute the \(\tau_x\) coefficient between partial label rankings.

kendall_distance

Compute the Kendall distance between label rankings.

scipy.stats.kendalltau

Kendall’s \(\tau\) between two variables.

Notes

Given a ranking \(y\) of \(m\) labels, where \(y_i\) is the position of label \(i\), let \(a_{ij}\) be 1 if \(y_i < y_j\) and -1 if \(y_i > y_j\). Kendall’s \(\tau\) between the rankings \(y\) and \(\hat{y}\) is

\[\tau(y, \hat{y}) = \frac{2}{m(m - 1)} \sum_{i < j} a_{ij} \hat{a}_{ij}.\]

Without ties, it is equal to Kendall’s \(\tau_a\) and \(\tau_b\), to the coefficient computed by scipy.stats.kendalltau() and to the coefficient computed by tau_x_score(). Rankings with ties are rejected, because the variants of Kendall’s \(\tau\) handle ties in different ways; use tau_x_score() for them.

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_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...
>>> kendall_tau_score(y_true, y_pred, sample_weight=[3, 1])
0.666...