4. Reduction to standard learning problems#

The rankers of sklr.reduction decompose label ranking into several standard learning problems, such as classification or regression problems, solve each one with an estimator, and combine the predictions of the estimators into a ranking. The rankings follow the representation described in Ranking representation, with the notation of Notation.

4.1. Labelwise decomposition#

LabelwiseLabelRanker implements the labelwise decomposition of [cheng_labelwise_2013]. The position of each label \(y_j\) in the training rankings is a class, from 1 to \(n\), so learning it is a classification problem whose classes are ordered, an ordinal classification problem. The ranker fits a probabilistic classifier to the positions of each label, which predicts the probability \(P_j(r \mid \boldsymbol{x})\) that \(y_j\) is at position \(r\) for an instance \(\boldsymbol{x}\), and predicts the ranking that minimizes the expected loss under these probabilities, as if the positions of the labels were independent:

>>> import numpy as np
>>> from sklr.reduction import LabelwiseLabelRanker
>>> X = [[0], [1], [2], [3], [4], [5]]
>>> y = [[1, 2, 3], [2, 1, 3], [1, 3, 2], [np.nan, 1, 2], [2, np.nan, 1], [3, 2, 1]]
>>> ranker = LabelwiseLabelRanker().fit(X, y)
>>> ranker.predict([[0.4], [2.6], [4.2]])
array([[1, 2, 3],
       [3, 1, 2],
       [3, 2, 1]])

The training rankings may be incomplete, as described in Incomplete rankings, with the default estimator or any other of Other estimators that learns from them, but they must not have ties, and the predicted rankings are complete and have no ties. The n_jobs parameter fits the classifiers of the labels in parallel.

4.1.1. Expected loss#

The loss between the true ranking \(\sigma\) and the predicted ranking \(\hat\pi\) is a sum over the labels of a loss \(\ell(\sigma(j), \hat\pi(j))\) between the true and the predicted position of each label, chosen by the loss parameter:

  • "absolute_error", the default, is \(\ell(r', r) = |r' - r|\), whose sum is an affine transformation of Spearman’s footrule.

  • "squared_error" is \(\ell(r', r) = (r' - r)^2\), whose sum is an affine transformation of Spearman’s \(\rho\).

As the loss is a sum over the labels, its expected value is the sum of the expected losses of the positions of the labels [cheng_labelwise_2013], where putting \(y_j\) at position \(r\) costs

\[L_j(r) = \sum_{r'=1}^n \ell(r', r) P_j(r' \mid \boldsymbol{x}).\]

Choosing the best position of each label separately may put two labels at the same position, so the predicted ranking is the permutation that minimizes the total cost,

\[\hat\pi = \arg\min_{\pi \in \Omega} \sum_{j=1}^n L_j(\pi(j)),\]

where \(\Omega\) is the set of the rankings of the \(n\) labels, the solution of an assignment problem of the labels to the positions [cheng_labelwise_2013]. With loss="absolute_error", it is solved by scipy.optimize.linear_sum_assignment(). With loss="squared_error", the sum \(\sum_{j=1}^n \pi(j)^2\) is the same for every permutation, so \(\hat\pi\) sorts the labels by increasing expected position \(\sum_{r=1}^n r P_j(r \mid \boldsymbol{x})\), without solving the assignment problem. The two losses may predict different rankings, as for the second sample of the first example:

>>> ranker = LabelwiseLabelRanker(loss="squared_error").fit(X, y)
>>> ranker.predict([[0.4], [2.6], [4.2]])
array([[1, 2, 3],
       [2, 1, 3],
       [3, 2, 1]])

When several rankings have the minimum expected loss, the predicted one gives the smallest position to the first label, then to the second one, and so on. [cheng_labelwise_2013] note that the ranker cannot minimize the expected Kendall distance, since the probabilities of the positions of each label do not determine how likely each pair of labels is to be inverted. Kendall’s \(\tau\) is still the default score of the ranker, as of every label ranker of scikit-lr, and the one of the experiments of [cheng_labelwise_2013].

4.1.2. Incomplete rankings#

An incomplete ranking \(\sigma\) only ranks a subset \(\mathcal{Y}(\sigma) \subseteq \mathcal{Y}\) of the labels, with \(m = |\mathcal{Y}(\sigma)| \leq n\) labels, so the position of a ranked label \(y_j\) in the complete ranking is only known to be between \(\sigma(j)\) and \(\sigma(j) + n - m\). LabelwiseLabelRanker assumes, as in Section 4.4 of [cheng_labelwise_2013], that the \(n - m\) unranked labels were removed at random from a complete ranking. If \(y_j\) is at position \(r\) of the complete ranking, it is observed at position \(\sigma(j)\) when \(r - \sigma(j)\) of the \(r - 1\) labels ahead of it are removed, and the other \(n - m - r + \sigma(j)\) removed labels are among the \(n - r\) behind it. The binomial coefficients count these choices among the \(\binom{n}{m}\) choices of the ranked labels, so the probability of observing \(y_j\) at position \(\sigma(j)\) is equation (16) of [cheng_labelwise_2013],

\[\sum_{r=\sigma(j)}^{\sigma(j) + n - m} P_j(r \mid \boldsymbol{x}) \frac{\binom{r - 1}{r - \sigma(j)} \binom{n - r}{n - m - r + \sigma(j)}}{\binom{n}{m}}.\]

The classifier of each label maximizes the log-likelihood of the rankings that rank that label under this probability, equation (17) of [cheng_labelwise_2013], each ranking weighted by its sample weight. With a complete ranking, \(m = n\) and the sum only has the term \(r = \sigma(j)\), with a binomial weight of 1, so the likelihood is the usual one of a classifier. The rankings that do not rank the label, or rank it alone, observe it with the same probability whatever the classifier, so they are left out, as are those of zero weight.

A label that no ranking of positive weight ranks together with another label has no classifier: its positions are predicted as equally likely, and fit() warns about it with a UserWarning. Its expected loss is then lowest in the middle positions, so such a label is not necessarily predicted last.

[cheng_labelwise_2013] also consider rankings that keep the top labels of the complete ranking, equations (18) and (19), which the representation of the rankings does not tell apart from those with labels removed at random, and which LabelwiseLabelRanker does not model.

4.1.3. Default estimator#

With estimator=None, the classifier of each label is the cumulative logit model of equation (20) of [cheng_labelwise_2013], an ordinal logistic regression. The probability that the position of \(y_j\) is at most \(r\), for \(r = 1, \dots, n - 1\), is

\[\sum_{s=1}^r P_j(s \mid \boldsymbol{x}) = \frac{1}{1 + \exp(-\beta_r - \boldsymbol{w}^\top \boldsymbol{x})},\]

with coefficients \(\boldsymbol{w}\) and thresholds \(\beta_1 \leq \dots \leq \beta_{n - 1}\) of its own, and the probability of each position is the difference of two consecutive cumulative probabilities. It learns from incomplete rankings by maximizing the likelihood of the previous section directly, as described in Other estimators. [cheng_labelwise_2013] do not say how they fit it: here, the coefficients have the \(L_2\) penalty of LogisticRegression with its default strength, the thresholds are not penalized, and the likelihood is maximized by L-BFGS-B, with a ConvergenceWarning from fit() if it does not converge within 100 iterations. Because of the penalty, the scale of the features matters, and they should be standardized as for LogisticRegression, as described in Using the rankers with scikit-learn.

4.1.4. Other estimators#

The estimator parameter takes any classifier with fit and predict_proba methods, which is cloned for each label. To learn from incomplete rankings, the classifier also has to maximize the likelihood of Incomplete rankings with a method of this signature, as the default estimator does:

def fit_likelihood(self, X, likelihood, classes, sample_weight=None):
    ...
    return self

likelihood[i, k] is the probability of what is observed of sample i if its class is classes[k], and the method maximizes the sum over the samples, weighted by sample_weight, of the logarithm of likelihood[i] @ proba[i], where proba[i] holds the probabilities of the classes of sample i that predict_proba predicts afterwards, with classes_ set to classes. LabelwiseLabelRanker calls it for each label with the positions from 1 to \(n\) as classes, and with the binomial weights of Incomplete rankings as likelihood, which are one for the observed position and zero for the others in a complete ranking. With complete rankings only, the method maximizes the usual likelihood of a classifier.

A classifier without fit_likelihood is fitted with fit and only learns from complete rankings, so fit() raises an error with incomplete ones:

>>> from sklearn.linear_model import LogisticRegression
>>> y = [[1, 2, 3], [2, 1, 3], [1, 3, 2], [3, 1, 2], [2, 3, 1], [3, 2, 1]]
>>> ranker = LabelwiseLabelRanker(LogisticRegression()).fit(X, y)
>>> ranker.predict([[0.4], [2.6], [4.2]])
array([[1, 2, 3],
       [3, 1, 2],
       [3, 2, 1]])

The classifier sees the positions as classes, and most classifiers ignore their order, as LogisticRegression does. A position that a label never takes in the training rankings is not a class of its classifier, so it gets a zero probability, and a label that always takes the same position leaves its classifier with a single class, which some classifiers, such as LogisticRegression, reject. If sample_weight is passed to fit(), the classifier must accept it in the method that fits it.

4.1.5. Complexity#

The ranker fits \(n\) classifiers on at most \(N\) samples each. With the default estimator and \(d\) features, each iteration of L-BFGS-B takes \(O(N(d + n))\) time for each label. For each predicted sample, loss="squared_error" takes \(O(n^2)\) time after the classifiers predict the probabilities, plus a sort of the labels. loss="absolute_error" takes \(O(n^3)\) time to compute the costs and solve the assignment problem, and choosing the ranking of the smallest positions among those of minimum cost solves at most \(O(n^2)\) smaller assignment problems, which takes \(O(n^5)\) time in the worst case.

The example Labelwise decomposition of rankings compares the losses and the estimators of LabelwiseLabelRanker, also with incomplete training rankings.

4.2. Pairwise comparison#

PairwiseLabelRanker implements the ranking by pairwise comparison of [hullermeier_label_2008]. Each pair of labels \(y_i\) and \(y_j\), with \(i < j\), is a binary classification problem: whether \(y_i\) is ahead of \(y_j\) in the ranking of an instance. The ranker fits a probabilistic classifier to each of the \(n(n - 1) / 2\) pairs, which predicts the probability \(R_{ij}(\boldsymbol{x})\) that \(y_i\) is ahead of \(y_j\) for an instance \(\boldsymbol{x}\), with \(R_{ji}(\boldsymbol{x}) = 1 - R_{ij}(\boldsymbol{x})\), and ranks the labels by the sum of these probabilities:

>>> import numpy as np
>>> from sklr.reduction import PairwiseLabelRanker
>>> X = [[0], [1], [2], [3], [4], [5]]
>>> y = [[1, 2, 3], [2, 1, 3], [1, 3, 2], [np.nan, 1, 2], [2, np.nan, 1], [3, 2, 1]]
>>> ranker = PairwiseLabelRanker().fit(X, y)
>>> ranker.predict([[0.4], [2.6], [4.2]])
array([[1, 2, 3],
       [2, 1, 3],
       [3, 2, 1]])

The fitted classifiers are in estimators_, one per pair in the order of numpy.triu_indices(), so the first one is that of \(y_1\) and \(y_2\), whose class 1 means that \(y_1\) is ahead:

>>> ranker.estimators_[0].predict_proba([[0.4], [2.6], [4.2]])[:, 1].round(2)
array([0.68, 0.42, 0.25])

The training rankings may be incomplete, as described in Incomplete rankings, but they must not have ties, and the predicted rankings are complete and have no ties. The n_jobs parameter fits the classifiers of the pairs in parallel.

4.2.1. Weighted voting#

The predicted ranking \(\hat\pi\) sorts the labels by decreasing votes,

\[S_i(\boldsymbol{x}) = \sum_{j \neq i} R_{ij}(\boldsymbol{x}),\]

the weighted voting of equations (3.3) and (3.4) of [hullermeier_label_2008], with the label of smallest index first when two labels have the same votes.

The classifiers are fitted independently, so their probabilities need not be consistent with any distribution of the rankings: they may even predict that \(y_1\) is ahead of \(y_2\), \(y_2\) ahead of \(y_3\) and \(y_3\) ahead of \(y_1\). The voting still gives a ranking, and it is the optimal one for Spearman’s \(\rho\) when the classifiers are right. If each \(R_{ij}(\boldsymbol{x})\) is the true probability \(P(y_i \succ y_j \mid \boldsymbol{x})\) that \(y_i\) is ahead of \(y_j\), the expected position of \(y_i\) is \(n - S_i(\boldsymbol{x})\) (Lemma 3 of [hullermeier_label_2008]), and sorting the labels by increasing expected position minimizes the expected sum of the squared differences between the predicted and the true positions,

\[\hat\pi = \arg\min_{\pi \in \Omega} \sum_{\sigma \in \Omega} P(\sigma \mid \boldsymbol{x}) \sum_{i=1}^n (\pi(i) - \sigma(i))^2,\]

where \(\Omega\) is the set of the rankings of the \(n\) labels (Theorem 3 of [hullermeier_label_2008]). This sum is an affine transformation of Spearman’s \(\rho\), described in Spearman’s \rho, so the voting maximizes its expected value. [hullermeier_label_2008] also show that, if the probabilities are the frequencies with which a set of rankings orders each pair, the voting ranks the labels as the Borda count of these rankings.

The same probabilities also give the expected Kendall distance of any ranking, but finding the ranking that minimizes it is an NP-complete problem, equivalent to the feedback arc set problem of a weighted tournament [hullermeier_label_2008], so PairwiseLabelRanker only implements the weighted voting, as in the experiments of [hullermeier_label_2008]. Kendall’s \(\tau\) is still the default score of the ranker, as of every label ranker of scikit-lr.

4.2.2. Incomplete rankings#

An incomplete ranking \(\sigma\) only ranks a subset \(\mathcal{Y}(\sigma) \subseteq \mathcal{Y}\) of the labels, with \(m = |\mathcal{Y}(\sigma)| \leq n\) labels, and it says nothing about the preference between two labels unless it ranks both. As in [hullermeier_label_2008], the classifier of each pair is fitted to the training rankings that rank its two labels, and the other rankings are left out. Each ranking is weighted by its sample weight, and those of zero weight are left out too. A ranking of \(m\) labels is therefore used by \(m(m - 1) / 2\) classifiers, and a ranking of fewer than two labels by none. A ranking that keeps the top labels of a complete ranking would also tell that its labels are ahead of the unranked ones, but the representation of the rankings does not tell it apart from one with labels removed at random, and the ranker does not use these preferences.

A pair that all the rankings left for it order in the same way gives its classifier a single class, which some classifiers, such as LogisticRegression, reject. The pair then gets a DummyClassifier that predicts that order with probability 1. A pair that no ranking of positive weight ranks has no classifier: its entry of estimators_ is None, its labels are predicted ahead of each other with probability 0.5, and fit() warns about it with a UserWarning. A label that no ranking ranks together with another label thus gets \((n - 1) / 2\) votes, so it is not necessarily predicted last.

4.2.3. Default estimator#

With estimator=None, the classifier of each pair is a LogisticRegression with its default parameters, since the experiments of [hullermeier_label_2008] use a logistic regression without stating its settings. Its coefficients have an \(L_2\) penalty, so the features should be standardized, as described in Using the rankers with scikit-learn.

4.2.4. Other estimators#

The estimator parameter takes any classifier with fit and predict_proba methods, which is cloned for each pair. The weighted voting uses the probabilities of the classifiers, so a classifier without predict_proba, such as LinearSVC, is rejected, and CalibratedClassifierCV turns its decisions into probabilities. The optimality for Spearman’s \(\rho\) holds when the probabilities are right, so well calibrated classifiers suit the ranker best. A classifier that predicts probabilities of 0 and 1, such as a DecisionTreeClassifier with pure leaves, turns the voting into a count of the pairs that each label wins. If sample_weight is passed to fit(), the classifier must accept it in its fit method, which KNeighborsClassifier, for instance, does not.

4.2.5. Complexity#

The ranker fits \(n(n - 1) / 2\) classifiers, the one of \(y_i\) and \(y_j\) on the \(N_{ij} \leq N\) training rankings that rank both labels. In total, they are fitted on \(N \bar{P}\) samples, where \(\bar{P}\) is the average number of pairs of labels of the training rankings, at most \(n(n - 1) / 2\) (Lemma 1 of [hullermeier_label_2008]). With a classifier that takes \(O(N^a)\) time on \(N\) samples, for some \(a \geq 1\), fitting the ranker takes \(O(\bar{P} N^a)\) time (Theorem 1 of [hullermeier_label_2008]). For each predicted sample, the \(n(n - 1) / 2\) classifiers predict a probability, the votes take \(O(n^2)\) time and sorting them \(O(n \log n)\) time. With linear classifiers and \(d\) features, the ranker stores \(O(d n^2)\) coefficients.

The example Ranking by pairwise comparison compares the estimators of PairwiseLabelRanker with LabelwiseLabelRanker, also with incomplete training rankings.

References

[cheng_labelwise_2013] (1,2,3,4,5,6,7,8,9,10,11)

W. Cheng, S. Henzgen and E. Hüllermeier, “Labelwise versus pairwise decomposition in label ranking”, in Proceedings of the Workshop on Lernen, Wissen & Adaptivität, pp. 129-136, 2013.

[hullermeier_label_2008] (1,2,3,4,5,6,7,8,9,10,11)

E. Hüllermeier, J. Fürnkranz, W. Cheng and K. Brinker, “Label ranking by learning pairwise preferences”, Artificial Intelligence, vol. 172, no. 16-17, pp. 1897-1916, 2008.