LabelRankerMixin#
- class sklr.base.LabelRankerMixin#
Mixin class for all label rankers in scikit-lr.
A label ranker predicts a ranking of the labels for each sample, without ties, and learns from (possibly incomplete) rankings without ties. This mixin sets the
estimator_typetag to"label_ranker"and states through the target tags thatfitrequires a targetywith several outputs, one per label, as described in Ranking representation. Itsscore()method computes Kendall’s \(\tau\) between the predicted and the true rankings.See also
PartialLabelRankerMixinMixin class for all partial label rankers.
is_label_rankerReturn whether an estimator is a label ranker.
Examples
>>> import numpy as np >>> from sklearn.base import BaseEstimator >>> from sklr.base import LabelRankerMixin, is_label_ranker >>> # Mixin classes should always be on the left-hand side for a correct MRO >>> class MyEstimator(LabelRankerMixin, BaseEstimator): ... def fit(self, X, y): ... self.ranking_ = np.asarray(y)[0] ... return self ... def predict(self, X): ... return np.tile(self.ranking_, (len(X), 1)) >>> estimator = MyEstimator() >>> X = np.array([[1, 2], [2, 3], [3, 4]]) >>> y = np.array([[1, 2, 3], [2, 1, 3], [1, 3, 2]]) >>> estimator.fit(X, y).predict(X) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) >>> is_label_ranker(estimator) True
- 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 toy.