PairwiseLabelRanker#

class sklr.reduction.PairwiseLabelRanker(estimator: object | None = None, *, n_jobs: int | None = None)#

Label ranker that learns the preference between each pair of labels.

The ranking by pairwise comparison of [hullermeier_label_2008] fits, for each pair of labels, a probabilistic classifier that predicts the probability that the first label is ahead of the second one, and ranks the labels by the sum of the probabilities that each one is ahead of the others (weighted voting). The training rankings may be incomplete, and each one is only used for the pairs of labels that it ranks.

Read more in the User Guide.

Parameters:
estimatorestimator object, default=None

Probabilistic binary classifier fitted to each pair of labels, which implements fit and predict_proba. Its class 1 means that the first label of the pair is ahead of the second one, and its class 0 that it is behind. None means a LogisticRegression with its default parameters. If sample_weight is given, its fit has to support it.

n_jobsint, default=None

Number of jobs to run in parallel to fit the estimators of the pairs of labels. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.

Attributes:
estimators_list of estimator objects or None

Estimator fitted to each pair of labels \(y_i\) and \(y_j\), with \(i < j\), in the order of numpy.triu_indices(), or None for a pair that no training ranking of positive weight ranks, whose labels are predicted ahead of each other with probability 0.5. A pair that all those rankings order in the same way has a DummyClassifier that predicts that order.

n_features_in_int

Number of features seen during fit.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when X has feature names that are all strings.

See also

LabelwiseLabelRanker

Label ranker that learns the position of each label separately.

sklr.neighbors.KNeighborsLabelRanker

Label ranker based on the k-nearest neighbors.

sklr.tree.DecisionTreeLabelRanker

A decision tree label ranker.

sklearn.multiclass.OneVsOneClassifier

One-vs-one multiclass strategy.

Notes

The estimator of each pair of labels \(y_i\) and \(y_j\), with \(i < j\), is fitted to the training rankings that rank both labels, and predicts the probability \(R_{ij}(\boldsymbol{x})\) that \(y_i\) is ahead of \(y_j\) given an instance \(\boldsymbol{x}\), with \(R_{ji}(\boldsymbol{x}) = 1 - R_{ij}(\boldsymbol{x})\). The votes of each label are

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

and the predicted ranking sorts the labels by decreasing votes, equations (3.2) to (3.4) of [hullermeier_label_2008], with the label of smallest index first on ties. If each \(R_{ij}(\boldsymbol{x})\) is the true probability that \(y_i\) is ahead of \(y_j\), the expected position of \(y_i\) is \(n - S_i(\boldsymbol{x})\), where \(n\) is the number of labels, and the predicted ranking minimizes the expected sum of the squared differences between its positions and the true ones, an affine transformation of Spearman’s \(\rho\) (Theorem 3 of [hullermeier_label_2008]).

A training ranking that does not rank one of the labels of a pair says nothing about their preference, so it is left out of the training data of the pair, as are the rankings of zero weight. If no ranking is left for a pair, fit() warns with a UserWarning, and its labels are predicted ahead of each other with probability 0.5. If all of them order the pair in the same way, that order is predicted with probability 1.

References

[hullermeier_label_2008] (1,2,3)

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.

Examples

>>> from sklr.reduction import PairwiseLabelRanker
>>> X = [[0], [1], [2], [3]]
>>> y = [[1, 2, 3], [2, 1, 3], [1, 2, 3], [3, 1, 2]]
>>> ranker = PairwiseLabelRanker().fit(X, y)
>>> ranker.predict([[0.1], [2.9]])
array([[1, 2, 3],
       [2, 1, 3]])

The training rankings may be incomplete, with np.nan for the unranked labels:

>>> import numpy as np
>>> y = [[1, 2, np.nan], [2, 1, 3], [1, np.nan, 2], [3, 1, 2]]
>>> ranker = PairwiseLabelRanker().fit(X, y)
>>> ranker.predict([[0.1], [2.9]])
array([[2, 1, 3],
       [2, 1, 3]])
fit(X: ArrayLike, y: ArrayLike, sample_weight: ArrayLike | None = None) → Self#

Fit an estimator to the preference between each pair of labels.

Parameters:
Xarray-like of shape (n_samples, n_features)

Training data.

yarray-like of shape (n_samples, n_labels)

Training rankings, as described in Ranking representation, without ties and possibly incomplete.

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

Sample weights, non-negative. None means that all the samples weigh the same.

Returns:
selfobject

Fitted estimator.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

predict(X: ArrayLike) → ndarray#

Predict the rankings of the samples.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

Returns:
yndarray of shape (n_samples, n_labels)

Predicted rankings, complete and without ties, as the positions of the labels.

score(X: ArrayLike, y: ArrayLike, sample_weight: ArrayLike | None = None) → float#

Return the mean Kendall’s \(\tau\) on the given data.

It is computed with kendall_tau_score().

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples, n_labels)

True rankings for X, complete and without ties.

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

Sample weights.

Returns:
scorefloat

Mean Kendall’s \(\tau\) of self.predict(X) with respect to y.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → PairwiseLabelRanker#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns:
selfobject

The updated object.

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → PairwiseLabelRanker#

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.