check_ranking#
- sklr.utils.check_ranking(y: ArrayLike, *, allow_ties: bool = False, allow_incomplete: bool = False, input_name: str = 'y') ndarray#
Input validation on the rankings of a target.
Each row of
yis the ranking of a sample and each column a label, so thaty[i, j]is the position of labeljin rankingi, from1for the most preferred one. Tied labels share their position and the positions must be dense, without gaps between them.np.nanmarks a label that is not ranked, and the positions of the ranked labels of each row must be dense from1, whatever their number. See Ranking representation for the details.- Parameters:
- yarray-like of shape (n_samples, n_labels)
The rankings to check, with at least one sample and two labels.
- allow_tiesbool, default=False
Whether to accept rankings with tied labels, that is, partial label rankings.
- allow_incompletebool, default=False
Whether to accept incomplete rankings, with labels that are not ranked.
- input_namestr, default=”y”
The name of the rankings in the error messages.
- Returns:
- y_convertedndarray of shape (n_samples, n_labels)
The validated rankings, as a
float64array. It may beyitself ifyis already afloat64array.
- Raises:
- TypeError
If
yis a sparse matrix.- ValueError
If
ydoes not encode rankings, because it is not a two-dimensional array of numbers with at least one sample and two labels or because the positions of some ranking are not dense from1, or ifyholds rankings with ties or incomplete rankings that are not allowed.
See also
type_of_rankingDetermine the type of rankings indicated by the target.
sklearn.utils.check_arrayInput validation on an array, list, sparse matrix or similar.
Examples
>>> import numpy as np >>> from sklr.utils import check_ranking >>> check_ranking([[1, 2, 3], [3, 1, 2]]) array([[1., 2., 3.], [3., 1., 2.]]) >>> check_ranking([[1, 1, 2], [3, 1, 2]], allow_ties=True) array([[1., 1., 2.], [3., 1., 2.]]) >>> check_ranking([[2, np.nan, 1], [3, 1, 2]], allow_incomplete=True) array([[ 2., nan, 1.], [ 3., 1., 2.]]) >>> check_ranking([[1, 1, 2], [3, 1, 2]]) Traceback (most recent call last): ... ValueError: Expected rankings without ties in y, got y[0] = [1 1 2].