@@ -66,7 +66,7 @@ def _check_zero_division(zero_division):
6666 return np .nan
6767
6868
69- def _check_targets (y_true , y_pred ):
69+ def _check_targets (y_true , y_pred , sample_weight = None ):
7070 """Check that y_true and y_pred belong to the same classification task.
7171
7272 This converts multiclass or binary types to a common shape, and raises a
@@ -83,6 +83,8 @@ def _check_targets(y_true, y_pred):
8383
8484 y_pred : array-like
8585
86+ sample_weight : array-like, default=None
87+
8688 Returns
8789 -------
8890 type_true : one of {'multilabel-indicator', 'multiclass', 'binary'}
@@ -92,11 +94,17 @@ def _check_targets(y_true, y_pred):
9294 y_true : array or indicator matrix
9395
9496 y_pred : array or indicator matrix
97+
98+ sample_weight : array or None
9599 """
96- xp , _ = get_namespace (y_true , y_pred )
97- check_consistent_length (y_true , y_pred )
100+ xp , _ = get_namespace (y_true , y_pred , sample_weight )
101+ check_consistent_length (y_true , y_pred , sample_weight )
98102 type_true = type_of_target (y_true , input_name = "y_true" )
99103 type_pred = type_of_target (y_pred , input_name = "y_pred" )
104+ if sample_weight is not None :
105+ sample_weight = _check_sample_weight (
106+ sample_weight , y_true , force_float_dtype = False
107+ )
100108
101109 y_type = {type_true , type_pred }
102110 if y_type == {"binary" , "multiclass" }:
@@ -148,7 +156,7 @@ def _check_targets(y_true, y_pred):
148156 y_pred = csr_matrix (y_pred )
149157 y_type = "multilabel-indicator"
150158
151- return y_type , y_true , y_pred
159+ return y_type , y_true , y_pred , sample_weight
152160
153161
154162def _validate_multiclass_probabilistic_prediction (
@@ -200,6 +208,9 @@ def _validate_multiclass_probabilistic_prediction(
200208 raise ValueError (f"y_prob contains values lower than 0: { y_prob .min ()} " )
201209
202210 check_consistent_length (y_prob , y_true , sample_weight )
211+ if sample_weight is not None :
212+ _check_sample_weight (sample_weight , y_true , force_float_dtype = False )
213+
203214 lb = LabelBinarizer ()
204215
205216 if labels is not None :
@@ -356,8 +367,9 @@ def accuracy_score(y_true, y_pred, *, normalize=True, sample_weight=None):
356367 xp , _ , device = get_namespace_and_device (y_true , y_pred , sample_weight )
357368 # Compute accuracy for each possible representation
358369 y_true , y_pred = attach_unique (y_true , y_pred )
359- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
360- check_consistent_length (y_true , y_pred , sample_weight )
370+ y_type , y_true , y_pred , sample_weight = _check_targets (
371+ y_true , y_pred , sample_weight
372+ )
361373
362374 if y_type .startswith ("multilabel" ):
363375 differing_labels = _count_nonzero (y_true - y_pred , xp = xp , device = device , axis = 1 )
@@ -464,7 +476,9 @@ def confusion_matrix(
464476 (0, 2, 1, 1)
465477 """
466478 y_true , y_pred = attach_unique (y_true , y_pred )
467- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
479+ y_type , y_true , y_pred , sample_weight = _check_targets (
480+ y_true , y_pred , sample_weight
481+ )
468482 if y_type not in ("binary" , "multiclass" ):
469483 raise ValueError ("%s is not supported" % y_type )
470484
@@ -482,10 +496,6 @@ def confusion_matrix(
482496
483497 if sample_weight is None :
484498 sample_weight = np .ones (y_true .shape [0 ], dtype = np .int64 )
485- else :
486- sample_weight = np .asarray (sample_weight )
487-
488- check_consistent_length (y_true , y_pred , sample_weight )
489499
490500 n_labels = labels .size
491501 # If labels are not consecutive integers starting from zero, then
@@ -654,11 +664,10 @@ def multilabel_confusion_matrix(
654664 [1, 2]]])
655665 """
656666 y_true , y_pred = attach_unique (y_true , y_pred )
657- xp , _ , device_ = get_namespace_and_device (y_true , y_pred )
658- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
659- if sample_weight is not None :
660- sample_weight = column_or_1d (sample_weight , device = device_ )
661- check_consistent_length (y_true , y_pred , sample_weight )
667+ xp , _ , device_ = get_namespace_and_device (y_true , y_pred , sample_weight )
668+ y_type , y_true , y_pred , sample_weight = _check_targets (
669+ y_true , y_pred , sample_weight
670+ )
662671
663672 if y_type not in ("binary" , "multiclass" , "multilabel-indicator" ):
664673 raise ValueError ("%s is not supported" % y_type )
@@ -1171,8 +1180,9 @@ def matthews_corrcoef(y_true, y_pred, *, sample_weight=None):
11711180 -0.33
11721181 """
11731182 y_true , y_pred = attach_unique (y_true , y_pred )
1174- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
1175- check_consistent_length (y_true , y_pred , sample_weight )
1183+ y_type , y_true , y_pred , sample_weight = _check_targets (
1184+ y_true , y_pred , sample_weight
1185+ )
11761186 if y_type not in {"binary" , "multiclass" }:
11771187 raise ValueError ("%s is not supported" % y_type )
11781188
@@ -1759,7 +1769,7 @@ def _check_set_wise_labels(y_true, y_pred, average, labels, pos_label):
17591769 raise ValueError ("average has to be one of " + str (average_options ))
17601770
17611771 y_true , y_pred = attach_unique (y_true , y_pred )
1762- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
1772+ y_type , y_true , y_pred , _ = _check_targets (y_true , y_pred )
17631773 # Convert to Python primitive type to avoid NumPy type / Python str
17641774 # comparison. See https://github.com/numpy/numpy/issues/6784
17651775 present_labels = _tolist (unique_labels (y_true , y_pred ))
@@ -2227,7 +2237,9 @@ class are present in `y_true`): both likelihood ratios are undefined.
22272237 # remove `FutureWarning`, and the Warns section in the docstring should not mention
22282238 # `raise_warning` anymore.
22292239 y_true , y_pred = attach_unique (y_true , y_pred )
2230- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
2240+ y_type , y_true , y_pred , sample_weight = _check_targets (
2241+ y_true , y_pred , sample_weight
2242+ )
22312243 if y_type != "binary" :
22322244 raise ValueError (
22332245 "class_likelihood_ratios only supports binary classification "
@@ -2945,7 +2957,9 @@ class 2 1.00 0.67 0.80 3
29452957 """
29462958
29472959 y_true , y_pred = attach_unique (y_true , y_pred )
2948- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
2960+ y_type , y_true , y_pred , sample_weight = _check_targets (
2961+ y_true , y_pred , sample_weight
2962+ )
29492963
29502964 if labels is None :
29512965 labels = unique_labels (y_true , y_pred )
@@ -3134,15 +3148,15 @@ def hamming_loss(y_true, y_pred, *, sample_weight=None):
31343148 0.75
31353149 """
31363150 y_true , y_pred = attach_unique (y_true , y_pred )
3137- y_type , y_true , y_pred = _check_targets (y_true , y_pred )
3138- check_consistent_length (y_true , y_pred , sample_weight )
3151+ y_type , y_true , y_pred , sample_weight = _check_targets (
3152+ y_true , y_pred , sample_weight
3153+ )
31393154
31403155 xp , _ , device = get_namespace_and_device (y_true , y_pred , sample_weight )
31413156
31423157 if sample_weight is None :
31433158 weight_average = 1.0
31443159 else :
3145- sample_weight = xp .asarray (sample_weight , device = device )
31463160 weight_average = _average (sample_weight , xp = xp )
31473161
31483162 if y_type .startswith ("multilabel" ):
@@ -3440,6 +3454,8 @@ def _validate_binary_probabilistic_prediction(y_true, y_prob, sample_weight, pos
34403454 assert_all_finite (y_prob )
34413455
34423456 check_consistent_length (y_prob , y_true , sample_weight )
3457+ if sample_weight is not None :
3458+ _check_sample_weight (sample_weight , y_true , force_float_dtype = False )
34433459
34443460 y_type = type_of_target (y_true , input_name = "y_true" )
34453461 if y_type != "binary" :
0 commit comments