2929 column_or_1d ,
3030)
3131from sklearn .utils ._array_api import (
32+ _average ,
3233 _max_precision_float_dtype ,
3334 get_namespace_and_device ,
3435 size ,
@@ -1412,6 +1413,7 @@ def coverage_error(y_true, y_score, *, sample_weight=None):
14121413 >>> coverage_error(y_true, y_score)
14131414 1.5
14141415 """
1416+ xp , _ , device_ = get_namespace_and_device (y_true , y_score , sample_weight )
14151417 y_true = check_array (y_true , ensure_2d = True )
14161418 y_score = check_array (y_score , ensure_2d = True )
14171419 check_consistent_length (y_true , y_score , sample_weight )
@@ -1423,12 +1425,12 @@ def coverage_error(y_true, y_score, *, sample_weight=None):
14231425 if y_true .shape != y_score .shape :
14241426 raise ValueError ("y_true and y_score have different shape" )
14251427
1426- y_score_mask = np . ma . masked_array ( y_score , mask = np . logical_not ( y_true ) )
1427- y_min_relevant = y_score_mask . min ( axis = 1 ). reshape (( - 1 , 1 ) )
1428- coverage = ( y_score >= y_min_relevant ). sum ( axis = 1 )
1429- coverage = coverage . filled ( 0 )
1430-
1431- return float ( np . average ( coverage , weights = sample_weight ) )
1428+ y_true_logical_not = xp . astype ( y_true , xp . bool , device = device_ , copy = False )
1429+ inf_val = xp . asarray ( xp . inf , dtype = y_score . dtype , device = device_ )
1430+ y_score_relevant_only = xp . where ( y_true_logical_not , y_score , inf_val )
1431+ y_min_relevant = xp . reshape ( xp . min ( y_score_relevant_only , axis = 1 ), ( - 1 , 1 ) )
1432+ coverage = xp . count_nonzero ( y_score >= y_min_relevant , axis = 1 )
1433+ return _average ( coverage , weights = sample_weight )
14321434
14331435
14341436@validate_params (
0 commit comments