
    h$,fq                        d dl mZ d dlmZ d dlmZmZmZ d dlZ	d dl
Z
d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d dl9m:Z: d d	l;m<Z< d d
l=m>Z> d dl?m@Z@mAZA d dlBmCZCmDZDmEZEmFZFmGZGmHZH d dlImJZJ d dlKmLZL d dlMmNZNmOZO e&e'e,e*e.e(e ee3d       ee-d       e+e) ee-d       eed      eedZPi deded eed      d eed      dedd d e/d! ee/d"      d#ed$e8d% ee8d      d&e!d'e2d(e4d)ed* eed+,      d- eed.,      i d/e%d0 eed1d.2      d3 eed14      d5 eed1d+2      d6 ee2d14      d7 ee4d14      d8 ee!d14      d9 eed:d.2      d; eed:4      d< eed:d+2      d= ee2d:4      d> ee4d:4      d? ee!d:4      d@ eedAd.2      dB eedA4      dC eedAd+2      dD ee2dA4       ee4dA4       ee!dA4       eedEd.2       eedE4       eedEd+2       ee2dE4       ee4dE4       ee!dE4      edF	ZQdG ZRe6eRedHZSi dIedJe#dKe$dL ee$d      dMe dNedOe5dP ee5d14      dQ ee5dE4      dR ee5d:4      dS ee5dAdTU      dV ee5d1dTU      dW ee5dAdXU      dY ee5d1dXU      dZ ee5d.[      d\ed] eed14       eedE4       eed:4      e"e0ee7d^ZT eU       ZVeVj                  eT       eVj                  eQ       eVj                  eP       eVj                  eS       h d_ZXh d`ZYeXj                  eY      Z[h daZ\h dbZ]h dcZ^h ddZ_h deZ`h dfZah dgZbh dhZch diZdh djZeh dkZfh dlZgdm Zhdn Zie
j                  j                  do eled            dp        Zme
j                  j                  do elee            dq        Zne
j                  j                  do el eoeV      e[z
              dr        ZpeHds        Zqe
j                  j                  do el eoeV      e[z
              dt        Zre
j                  j                  do el eoeQ      e[z
              du        Zse
j                  j                  doeT      dv        Ztd dwge	j                  e	j                  gfd dwge	j                  e	j                  gfd dwge	j                  e	j                  gfd dwge	j                  dwgfd dwge	j                  dwgfgZwe
j                  j                  dx eeTj                         ePj                                     e
j                  j                  dyew      dz               Zye
j                  j                  dxeQj                               e
j                  j                  dyewe	j                  dwd+gg d{fe	j                  dwd+gg d{fgz         d|               Zze
j                  j                  dxeQj                               d}        Z{eHd~        Z|eHd        Z}e
j                  j                  do el eoeV      e[z
   eoeT      z
              d        Z~e
j                  j                  do elecebz              d        Ze
j                  j                  do elec            d        Ze
j                  j                  do elec            d        ZeHe
j                  j                  deJ      d               Ze
j                  j                  do eleb            d        Ze
j                  j                  do ele`            d        Ze
j                  j                  do ele`            d        Ze
j                  j                  do ele`j                  eb                  d        ZeHd        Zd Ze
j                  j                  do ele\            d        Ze
j                  j                  do ele\e]z              d        Ze
j                  j                  do ele\            d        Zd Ze
j                  j                  do ele\            d        ZeHd        Ze
j                  j                  do el eoeV      j                   eoeP            efz
              d        Ze
j                  j                  do el eoeV       eoeP      z
  efz
  eXz
              d        Ze
j                  j                  do el eoeV       eoeP      z
  efz
  e[z
              d        Ze
j                  j                  do elebeaz  ecz  efz
              d        ZeHd        Ze
j                  j                  do elebd hz
              d        Ze
j                  j                  do eleaecz              d        Ze
j                  j                  do el eoeT      e[z
              d        Ze
j                  j                  deQ      d        Ze
j                  j                  dedfedfedf eedw,      dfe!dfe1dfe2dfe4dfe6dfg	      e
j                  j                  deeg      d               Zd Zd Zd Zeeege8eegiZefdZe
j                  j                  d eA             e
j                  j                  d e             d               Zy)    )partial)	signature)chainpermutationsproductN)config_context)make_multilabel_classification))accuracy_scoreaverage_precision_scorebalanced_accuracy_scorebrier_score_losscohen_kappa_scoreconfusion_matrixcoverage_errord2_absolute_error_scored2_pinball_scored2_tweedie_score	dcg_score	det_curveexplained_variance_scoref1_scorefbeta_scorehamming_loss
hinge_lossjaccard_score%label_ranking_average_precision_scorelabel_ranking_losslog_lossmatthews_corrcoef	max_errormean_absolute_errormean_absolute_percentage_errormean_gamma_deviancemean_pinball_lossmean_poisson_deviancemean_squared_errormean_tweedie_deviancemedian_absolute_errormultilabel_confusion_matrix
ndcg_scoreprecision_recall_curveprecision_scorer2_scorerecall_scoreroc_auc_score	roc_curvetop_k_accuracy_scorezero_one_loss)_average_binary_score)LabelBinarizer)shuffle)_atol_for_type)yield_namespace_device_dtype_combinations)_array_api_for_testsassert_allcloseassert_almost_equalassert_array_equalassert_array_lessignore_warnings)COO_CONTAINERS)type_of_target)_num_samplescheck_random_statevariance_weighted)multioutput)powergffffff?)r    r!   r&   r$   r(   r"   r   r-   mean_normal_deviancer%   r#   mean_compound_poisson_deviancer   r   r   r
   r    adjusted_balanced_accuracy_scoreT)adjustedunnormalized_accuracy_scoreF	normalizeunnormalized_confusion_matrixnormalized_confusion_matrixc                      t        | i |j                  d      t        | i |j                  d      d d t        j                  f   z  S )Nfloat   axis)r   astypesumnpnewaxis)argskwargss     Alib/python3.12/site-packages/sklearn/metrics/tests/test_common.py<lambda>rZ      sM    $)&)009
D
+F
+
/
/Q
/
72::
F	G     (unnormalized_multilabel_confusion_matrix/unnormalized_multilabel_confusion_matrix_sample)
samplewiser   r2   unnormalized_zero_one_lossr   r,   r.   r   f2_score   )beta
f0.5_scoreg      ?matthews_corrcoef_scoreweighted_f0.5_scoreweighted)averagerb   weighted_f1_scorerg   weighted_f2_scoreweighted_precision_scoreweighted_recall_scoreweighted_jaccard_scoremicro_f0.5_scoremicromicro_f1_scoremicro_f2_scoremicro_precision_scoremicro_recall_scoremicro_jaccard_scoremacro_f0.5_scoremacromacro_f1_scoremacro_f2_scoremacro_precision_scoresamples)	macro_recall_scoremacro_jaccard_scoresamples_f0.5_scoresamples_f1_scoresamples_f2_scoresamples_precision_scoresamples_recall_scoresamples_jaccard_scorer   c            
         t        | i |\  }}}t        |      t        |      z
  }t        j                  ||t        j                  |j                  t        j                        d|fdt        j                  g      g      S )a'  
    The dimensions of precision-recall pairs and the threshold array as
    returned by the precision_recall_curve do not match. See
    func:`sklearn.metrics.precision_recall_curve`

    This prevents implicit conversion of return value triple to an higher
    dimensional np.array of dtype('float64') (it will be of dtype('object)
    instead). This again is needed for assert_array_equal to work correctly.

    As a workaround we pad the threshold array with NaN values to match
    the dimension of precision and recall arrays respectively.
    r   constant)	pad_widthmodeconstant_values)r+   lenrU   arraypadrS   float64nan)rW   rX   	precisionrecall
thresholdspad_threshholdss         rY   (precision_recall_curve_padded_thresholdsr      sz     %;D$KF$K!Ivz)ns:6O88FF!!"**-o.!#			
 r[   )r0   r+   r   r   r   r   unnormalized_log_lossr   r   r/   weighted_roc_aucsamples_roc_aucmicro_roc_aucovr_roc_aucovr)rg   multi_classweighted_ovr_roc_aucovo_roc_aucovoweighted_ovo_roc_aucpartial_roc_auc)max_fprr    weighted_average_precision_score)samples_average_precision_scoremicro_average_precision_scorer   r*   r   r1   >   r   r*   r   r~   r   r   r   r   r   r   r]   r}   >   r   r`   r   r0   r.   r   r   r/   r   r,   r   r   r   r+   rc   >   r   r`   r.   r   r,   rc   >   r/   r   r   >   r   r`   r   r0   r.   r   r,   r   r+   r   r   r   r   rc   >    r   r`   r   r0   r.   r   rw   rx   rp   rq   r,   r   rh   rj   r{   rs   r|   rt   ry   rr   rl   r+   rm   rk   rM   rL   r\   r]   rc   ru   rn   re   >   r2   r
   r1   >   r   r   r*   r   r/   r   r   r   r   r   r   r   r   r   r   r   >   r   r2   r
   rw   rx   rp   rq   r~   r   rh   rj   r{   rs   r|   rt   r   ry   rr   r   rl   rm   r   rk   r_   rI   r\   ru   rn   r}   re   >	   r-   r   r$   r&   r!   r(   r   r   r"   >   r   r    r   r   r2   r
   rw   rp   rq   r   r$   r&   rs   r|   r!   rt   rE   r(   rr   r   rl   rd   r_   rI   rn   >   r`   r-   r   r0   r   r.   rx   r,   r   r   rh   rj   r{   r#   ry   r%   r+   rm   r   r   r   rk   rM   rL   r"   rF   rG   r\   rc   ru   re   >   r    r   r   r(   >   r   r#   r%   rF   c                     t        t        | j                         |j                                     dz   }| |z  } ||z  }| |fS )zMake targets strictly positiverP   )absmin)y1y2offsets      rY   _require_positive_targetsr     s?    RVVXrvvx()A-F&LB&LBr6Mr[   c                      t         t        z  t        t              z  t        z  t        t
              k(  sJ t         t        z  t               k(  sJ y N)SYMMETRIC_METRICSNOT_SYMMETRIC_METRICSsetTHRESHOLDED_METRICS"METRIC_UNDEFINED_BINARY_MULTICLASSALL_METRICS r[   rY   test_symmetry_consistencyr   %  sX     	
	 
!
"	# -	- 
[	    55#%???r[   namec                    t        d      }|j                  ddd      }|j                  ddd      }| t        v rt        ||      \  }}|j                  ddd      }|j                  ddd      }t        |    }| t
        v r/| t        v r t         |||       |||      d| z         y J d       t         |||       |||      d| z         y )	Nr   ra      sizer      z%s is not symmetricerr_msgz This case is currently unhandled)rA   randintMETRICS_REQUIRE_POSITIVE_Yr   r   METRIC_UNDEFINED_BINARYMULTILABELS_METRICSr9   )r   random_statey_truey_pred
y_true_bin
y_pred_binmetrics          rY   test_symmetric_metricr   1  s     &a(L!!!QU!3F!!!QU!3F))266B%%a%:J%%a%:JF&&&&z:.z:.-4 =<<566"66")D0	
r[   c                 D   t        d      }|j                  ddd      }|j                  ddd      }| t        v rt        ||      \  }}t        |    }t        j                  t              5  t         |||       |||             t        d| z        # 1 sw Y   y xY w)Nr   ra   r   r   z%s seems to be symmetric)
rA   r   r   r   r   pytestraisesAssertionErrorr;   
ValueError)r   r   r   r   r   s        rY   test_not_symmetric_metricr   P  s     &a(L!!!QU!3F!!!QU!3F))266BF 
~	& <6&&16&&3IJ3d:;;< <s   -)BBc                 H   t        d      }|j                  ddd      }|j                  ddd      }| t        v rt        ||      \  }}t	        ||d      \  }}t               5  t        |    }t         |||       |||      d| z         d d d        y # 1 sw Y   y xY w)Nr   ra   r   r   r    %s is not sample order invariantr   )rA   r   r   r   r5   r=   r   r9   )r   r   r   r   y_true_shuffley_pred_shuffler   s          rY   test_sample_order_invariancer   b  s     &a(L!!!QU!3F!!!QU!3F))266B%,VV!%L"NN		 
T"66">>26=	

 
 
s   &)BB!c                  8   t        d      } | j                  ddd      }| j                  ddd      }| j                  |j                        }t	        |||d      \  }}}t
        D ]*  }t        |   }t         |||       |||      d|z         , t        D ]*  }t        |   }t         |||       |||      d|z         , t        D ]I  }t        |   }t         |||       |||      d|z         t         |||       |||      d|z         K y )Nr   ra   r   r   r   r   r   )
rA   r   normalshaper5   r   r   r9   THRESHOLDED_MULTILABEL_METRICSMULTIOUTPUT_METRICS)	r   r   r   y_scorer   r   y_score_shuffler   r   s	            rY   7test_sample_order_invariance_multilabel_and_multioutputr   x  sP   %a(L !!!QX!6F!!!QX!6F!!v||!4G6=a73NNO $ 
T"66">>26=	

 / 
T"67#>?36=	

 $ 
T"67#>?36=	

 	66">>26=	

r[   c                 d   t        d      }|j                  ddd      }|j                  ddd      }| t        v rt        ||      \  }}t	        |      }t	        |      }t        j                  |      t        j                  |      }}t        |j                  d       t        |j                  d       t        j                  |d      }t        j                  |d      }	t        j                  |d      }
t        j                  |d      }t               5  t        |    } |||      }t         |||      |d| z  	       t         |||      |d
| z  	       t         |||	      |d| z  	       t         |||      |d| z  	       t         |||      |d| z  	       t         |||	      |d| z  	       t         |||      |d| z  	       t         |||	      |d| z  	       t         |||      |d| z  	       t        j                  t              5   |||       d d d        t        j                  t              5   ||
|       d d d        t        j                  t              5   |||       d d d        t        j                  t              5   ||
|       d d d        t        j                  t              5   |||       d d d        t        j                  t              5   ||
|	       d d d        | t         t"        z  t$        z  vr+t        j                  t              5   ||
|       d d d        d d d        y # 1 sw Y   +xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   \xY w# 1 sw Y   y xY w)Nr   ra   r   r   rP   )rP   )rP   r   z,%s is not representation invariant with listr   z3%s is not representation invariant with np-array-1dz7%s is not representation invariant with np-array-columnz@%s is not representation invariant with mix np-array-1d and listzK%s is not representation invariant with mix np-array-1d and np-array-columnzD%s is not representation invariant with mix list and np-array-column)rA   r   r   r   listrU   r   r;   ndimreshaper=   r   r9   r   r   r   r   r   r   )r   r   r   r   y1_listy2_listy1_1dy2_1d	y1_column	y2_columny1_rowy2_rowr   measures                 rY   &test_format_invariance_with_1d_vectorsr     s    &a(L			a		/B			a		/B))*2r2B2hG2hG88B<"5Euzz1%uzz1%

5'*I

5'*IZZw'FZZw'F		 `'T"R.7G$BTI	
 	5% IDP	
 	9i(MPTT	
 	5'"V	
 	7E"V	
 	5)$2 		
 	9e$2 		
 	7I&V	
 	9g&V	
 ]]:& 	"5&!	"]]:& 	"65!	"]]:& 	$7F#	$]]:& 	$67#	$]]:& 	&9f%	&]]:& 	&69%	&
 "@@CVV
 z* 'vv&'`' `'Z	" 	"	" 	"	$ 	$	$ 	$	& 	&	& 	&' '`' `'s   DN&
M'!N&
M!N&3
M*=!N&
M6(!N&	
N!N&4
N>7N&5
N?N&M	N&M'	"N&*M3	/N&6M?	;N&N	N&N	N&N#	N&&N/c                 @   t        d      }|j                  ddd      }|j                  ddd      }t        j                  ddg      |   }t        j                  ddg      |   }d}ddg}t	               5  t
        |    } |||      }	|}
| t        v rt        |
|      }
 |
||      }t        |	|dj                  |       	        |
|j                  d
      |j                  d
            }t        |	|dj                  |       	       | t        v rwt        |
|      }
 |
||      }t        |	|dj                  |       	        |
|j                  d
      |j                  d
            }t        |	|dj                  |       	       d d d        y # 1 sw Y   y xY w)Nr   ra   r   r   eggsspam	pos_label+{0} failed string vs number invariance testr   O2{0} failed string object vs number invariance test)labelsz,{0} failed string vs number  invariance test)rA   r   rU   r   r=   CLASSIFICATION_METRICSMETRICS_WITH_POS_LABELr   r;   formatrS   METRICS_WITH_LABELS)r   r   r   r   y1_stry2_strpos_label_str
labels_strr   measure_with_number
metric_strmeasure_with_strmeasure_with_strobjs                rY   7test_classification_invariance_string_vs_numbers_labelsr     s   
 &a(L			a		/B			a		/BXXvv&'+FXXvv&'+FM&!J		 &'-$Rn 
)) }EJ%ff5AHHN	
 )s);V]]3=OPHOOPTU	
 && J?J)&&9# FMMdS #-V]]3-?sAS"T##FMMdSE& & &s   8DFFc                    t        d      }|j                  ddd      }|j                  ddd      }t        j                  ddg      |   }d}t	               5  t
        |    }| t        vr||}| t        v rt        ||      } |||      } |||      }	t        ||	dj                  |       	        ||j                  d
      |      }
t        ||
dj                  |       	       net        j                  t              5   |||       d d d        t        j                  t              5   ||j                  d
      |       d d d        d d d        y # 1 sw Y   LxY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   ra   r   r   r   r   r   r   r   r   r   )rA   r   rU   r   r=   r   r   r   r   r;   r   rS   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   s              rY   4test_thresholded_invariance_string_vs_numbers_labelsr  T  ss    &a(L			a		/B			a		/BXXvv&'+FM		 /$T*..J--$Z=I
"(R.)&"5# ELLTR #-V]]3-?"D##LSS z* #vr"#z* /v}}S)2./9/ /4# #/ /9/ /s=   B'E.
E!E.,E"E.E	E."E+	'E..E7rP   r   zy_true, y_scorec                     | t         k(  r|g}|g}t        j                  t        d      5   | ||       d d d        y # 1 sw Y   y xY w)Nzcontains (NaN|infinity)match)r   r   r   r   )r   r   r   s      rY   )test_regression_thresholded_inf_nan_inputr    sH     )	z)C	D  vw     s	   
>A)rP   ra      c                 v   t        j                  |      j                         s+d}t        j                  |      j	                         rd}n-d}n*d}t        j                  |      j	                         rd}nd}d| d| }t        j                  t        |      5   | ||       ddd       y# 1 sw Y   yxY w)	z{check that classification metrics raise a message mentioning the
    occurrence of non-finite values in the target vectors.r   NaNzinfinity or a value too larger   zInput z
 contains r  N)rU   isfiniteallisnananyr   r   r   )r   r   r   
input_nameunexpected_valuer   s         rY   !test_classification_inf_nan_inputr    s     ;;v""$
88F!$>
88G  "$>zl*-=,>?G	z	1  vw     s   
B//B8c                     g dg d}}d}t        j                  t        |      5   | ||       ddd       y# 1 sw Y   yxY w)zocheck that classification metrics raise a message of mixed type data
    with continuous/binary target vectors.)abr  )皙?g?333333?zJClassification metrics can't handle a mix of binary and continuous targetsr  N)r   r   r   )r   r   r   r   s       rY   +test_classification_binary_continuous_inputr    sD     &GFT  
z	1  vw     s	   
9Ac                 x    t         |    }| t        v rddg}nddg}t        |d      D ]  \  }} ||g|g        y )NrP   ra   r   repeat)r   r   r   )r   r   valuesijs        rY   check_single_sampler    sS     F ))QQq) 1sQCr[   c                     t         |    }t        ddgd      D ]<  \  }}}} |t        j                  ||gg      t        j                  ||gg             > y )Nr   rP      r  )r   r   rU   r   )r   r   r  r  kls         rY   check_single_sample_multioutputr"    sV    Fq!fQ/ 7
1arxx!Q!288aVH#567r[   c                     t        |        y r   )r  r   s    rY   test_single_sampler%    s     r[   c                     t        |        y r   )r"  r$  s    rY   test_single_sample_multioutputr'    s
    #D)r[   c                     t        j                  g dg dg dg      }t        j                  ddgddgddgg      }t        |    }t        j                  t
              5   |||       d d d        y # 1 sw Y   y xY w)N)rP   r   r   rP   )r   rP   rP   rP   )rP   rP   r   rP   r   rP   rU   r   r   r   r   r   )r   r   r   r   s       rY   (test_multioutput_number_of_output_differr*    sm    XX|\<@AFXX1v1v1v./FF	z	" vv  s   
A22A;c           	      6   t        d      }|j                  ddd      }|j                  ddd      }t        |    } |||      }t        d      D ]F  }|j	                  |j
                  d         }t         ||d d |f   |d d |f         |d| z         H y )	Nr   ra   )r      r   r  rP   z'%s is not dimension shuffling invariantr   )rA   uniformr   rangepermutationr   r9   )r   r   r   r   r   error_perms           rY   =test_multioutput_regression_invariance_to_dimension_shufflingr3    s     &a(L!!!QW!5F!!!QW!5FF66"E1X 
''Q86!T'?F1d7O4=F	

r[   coo_containerc                    d}d}t        d|d|d      \  }}t        d|d|d      \  }}t        j                  |dg|z  gg      }t        j                  |dg|z  gg      } | |      } | |      }t        |      }t        |      }	|D 
cg c]  }
t        |
       }}
|	D 
cg c]  }
t        |
       }}
t        D ]z  }t
        |   }t        |t              rd|_        ||_	         |||      }t         |||      |d|z  	       t         |||      |d
|z  	       t         |||	      |d|z  	       | y c c}
w c c}
w )Nr  2   rP   r   T
n_features	n_classesr   	n_samplesallow_unlabeledtmpzO%s failed representation invariance between dense and sparse indicator formats.r   z\%s failed representation invariance  between dense array and list of list indicator formats.zW%s failed representation invariance  between dense and list of array indicator formats.)r	   rU   vstackr   r   r   
isinstancer   
__module____name__r9   r:   )r4  r9  r:  r1  r   r   y1_sparse_indicatory2_sparse_indicatory1_list_array_indicatory2_list_array_indicatorr  y1_list_list_indicatory2_list_list_indicatorr   r   r   s                   rY   )test_multilabel_representation_invariancerG    s    II*EAr +EAr 
B!y)*	+B	B!y)*	+B'+'+"2h"2h/FG!d1gGG/FG!d1gGG# '
T" fg& %F"FOR. 	&(;<6 		
 	)+AB% 		
 	*,CD% 		
='
 HGs   D=%Ec           	          dgdgddggg dg gdgt        j                  g ddggd      g}t        |    }|D ]-  }t        j                  t
              5   |||       d d d        / y # 1 sw Y   :xY w)NrP   ra   r   )r   ra   )r   rP   r   objectdtyper)  )r   multilabel_sequencesr   seqs       rY   +test_raise_value_error_multilabel_sequencesrN  X  s     qcAq6		
"q!fX. F# ]]:& 	3	 		 	s   
A((A1	c                 N   d}d}t        d      }|j                  d||f      }|j                  d||f      }|j                  |j                        }t        |    }| t
        v r|n|} |||d      }	 |||d      }
t        d|	z  dd	
       t        |	|
|z  d|  
       y )Nra   r   r   r   TrJ   F      0We failed to test correctly the normalize optionr   Failed with )rA   r   r   r   r   r   r<   r9   r   r9  r:  r   r   r   r   metricspredmeasure_normalizedmeasure_not_normalizeds              rY   +test_normalize_option_binary_classificationrX  i  s     II%a(L!!!Yi\!BF!!!Yi\!BF!!v||!4G$G117vD >$VTUC!!	B *tf%r[   c                 >   d}d}t        d      }|j                  d||f      }|j                  d||f      }|j                  ||f      }t        |    }| t        v r|n|} |||d      }	 |||d      }
t        d|	z  dd	
       t        |	|
|z  d|  
       y )Nr  r   r   r   TrJ   FrP  rQ  r   rR  )rA   r   r-  r   r   r<   r9   rS  s              rY   /test_normalize_option_multiclass_classificationrZ    s     II%a(L!!!Yi\!BF!!!Yi\!BF""I(>"?G$G117vD >$VTUC!!	B *tf%r[   c                 j   d}d}t        d      }t        d|dd|      \  }}t        d|dd|      \  }}|j                  |j                        }|dg|z  z  }|dg|z  z  }t        |    }| t
        v r|n|}	 |||	d      }
 |||	d	      }t        d
|
z  dd       t        |
||z  d|         y )Nr  d   r   rP   T)r8  r9  r   r;  r:  r   rJ   FrP  rQ  r   rR  )rA   r	   r-  r   r   r   r<   r9   )r   r9  r:  r   r1  r   r   r   rT  rU  rV  rW  s               rY   /test_normalize_option_multilabel_classificationr]    s    
 II%a(L /IAv /IAv """5G qcIoF
qcIoF$G117vD >$VTUC!!	B *tf%r[   c                    |j                   \  }} | ||d       }t        |t        |      D 	cg c]  }	 | |d d |	f   |d d |	f          c}	        | ||d      }
t        |
 | |j                         |j                                       | ||d      }t        |t	        j
                  |             t	        j                  |dt              }t	        j                  |      dk7  r- | ||d      }t        |t	        j                  ||             n | ||d      }t        |d       |rN | ||d      }t        |t	        j
                  t        |      D 	cg c]  }	 | ||	   ||	          c}	             t        j                  t              5   | ||d	       d d d        t        j                  t              5   | ||d
       d d d        y c c}	w c c}	w # 1 sw Y   AxY w# 1 sw Y   y xY w)Nri   ro   rv   r   )rR   rK  rf   )weightsrz   unknowngarbage)r   r9   r.  ravelrU   meanrT   intrg   r   r   r   )r   r   r   y_true_binarizey_pred_binarizeis_multilabelr:  r9  label_measurer  micro_measuremacro_measurer_  weighted_measuresample_measures                  rY   _check_averagingrm    s    +00Iy 6648M 9%	
 ?1a4(/!Q$*?@	
 667;Mvo3357L7L7NO
 667;MM277=#9: ff_1C8G	vvg!!&&*E("**]G*TU!&&*E(!, 	BGG #9- ?1-q/AB	
 
z	" 2vvy12	z	" 2vvy12 2S	
@2 22 2s#   G
GG$G0$G-0G9c                     t        |      j                  d      }t        |    }| t        v rt	        ||||||       y | t
        v rt	        ||||||       y t        d      )N
multilabelz2Metric is not recorded as having an average option)r?   
startswithr   METRICS_WITH_AVERAGINGrm  "THRESHOLDED_METRICS_WITH_AVERAGINGr   )r   r   re  r   rf  r   rg  r   s           rY   check_averagingrs    sm    "6*55lCMF%%FFO_m	
 
3	3FG_g}	
 MNNr[   c                 6   d\  }}t        d      }|j                  d||f      }|j                  d||f      }|j                  ||f      }t               j	                  |      }|j                  |      }|j                  |      }	t        | ||||	|       y )N)r6  r  r   r   )rA   r   r-  r4   fit	transformrs  )
r   r:  r9  r   r   r   r   lbre  rf  s
             rY   test_averaging_multiclassrx    s     Iy%a(L!!!Yi\!BF!!!Yi\!BF""I(>"?G				f	%Bll6*Oll6*OD&/6?GTr[   c                     d\  }}t        d|d|d      \  }}|d d }|dd  }t        d      j                  d|f      }|}|}	t        | ||||	|       y )	N)(   r,  rP   r,  Fr7  r   r   r   )r	   rA   r   rs  )
r   r:  r9  r1  yr   r   r   re  rf  s
             rY   test_averaging_multilabelr|  ,  s{     !Iy)DAq sVFrsVF #**Y*@GOOD&/6?GTr[   c                     t        j                  d      }t        j                  d      }t        j                  d      }|}|}t        | |||||       y Nr   r  )rU   zerosrs  r   r   r   r   re  rf  s         rY   $test_averaging_multilabel_all_zeroesr  A  sH    XXgFXXgFhhwGOOD&/6?GTr[   c                      t        j                  d      } t        j                  d      }| }|}dd}t        || |||d       y )Nr  c                 &    t        t        | ||      S r   )r3   r,   )r   r   rg   s      rY   rZ   z=test_averaging_binary_multilabel_all_zeroes.<locals>.<lambda>R  s    =R'> r[   T)rg  )rv   )rU   r  rm  )r   r   re  rf  binary_metrics        rY   +test_averaging_binary_multilabel_all_zeroesr  L  sK    XXgFXXgFOOM r[   c                     t        j                  d      }t        j                  d      }t        j                  d      }|}|}t        | |||||       y r~  )rU   onesrs  r  s         rY   "test_averaging_multilabel_all_onesr  _  sH    WWWFWWWFgggGOOD&/6?GTr[   c                    t         j                  j                  d      }|j                  ddt	        |            }| dk(  rt        |d      n|} |||d       }t        | |||t        j                  t	        |                  d	| z  
        ||||      }t        j                  t              5  t        ||       t        d|d|d|       # 1 sw Y   nxY w ||||j                               }t        ||d|d|d| 
        |t        j                  ||d      t        j                  ||d      d       }	t        ||	d| z  
       |dd d   }
t        j                  |      }d|d d d<   |dd d   }|dd d   } ||||
      } ||||      }t        ||d|d|d| 
       | j                  d      s$dD ]  }t        | |||||z        d| z  
       ! dj!                  t#        |      t#        |      t#        |      dz        }t        j                  t        |      5   |||t        j$                  ||g             d d d        y # 1 sw Y   y xY w)Nr   rP   
   r   r1   )r   sample_weight)r   zAFor %s sample_weight=None is not equivalent to sample_weight=onesr   z>Unweighted and weighted scores are unexpectedly almost equal (z) and (z) for zFWeighted scores for array and list sample_weight input are not equal (z != rQ   z.Weighting %s is not equal to repeating samplesra   zUZeroing weights does not give the same result as removing the corresponding samples (unnormalized)ra   r  z/%s sample_weight is not invariant under scalingzJFound input variables with inconsistent numbers of samples: \[{}, {}, {}\]r  )rU   randomRandomStater   r   r   r9   r  r   r   r   r   tolistr  copyrp  r   r@   hstack)r   r   r   r   rngr  unweighted_scoreweighted_scoreweighted_score_listrepeat_weighted_scoresample_weight_subsetsample_weight_zeroed	y1_subset	y2_subsetweighted_score_subsetweighted_score_zeroedscalingerror_messages                     rY   check_sample_weight_invariancer  j  s   
))


"CKK2CGK4M &*-C%CWVq!F b"D9r2RWW3r7%;<S
 B-@N 
~	& 
(.9 ).$@
 	

 
 !R}7K7K7MN
 .	6 #
		"m!,
		"m!,
 @4G )A.77=1 !114a4I14a4I"9,@ #2r9MN
 !"7	? ??>*   	Gr2]W-DEIDP		##)6l2.]0Ka0O$
  
z	7 Pr2RYY}/M%NOP P Ps   7!CC!?!I))I2c                     d}t        d      }|j                  |f      }|j                  |f      }t        |    }t        | |||       y )Nr6  r   r   )rA   random_sampler   r  )r   r:  r   r   r   r   s         rY   (test_regression_sample_weight_invariancer    sU     I%a(L''i\':F''i\':FF"4@r[   c                     d}t        d      }|j                  dd|f      }|j                  dd|f      }|j                  |f      }t        |    }| t        v rt        | |||       y t        | |||       y )Nr6  r   ra   r   )rA   r   r  r   r   r  )r   r:  r   r   r   r   r   s          rY   $test_binary_sample_weight_invariancer    s     I%a(L!!!Qi\!:F!!!Qi\!:F((yl(;GF""&tVVWE&tVVVDr[   c                 l   d}t        d      }|j                  dd|f      }|j                  dd|f      }|j                  |df      }t        |    }| t        v rJt        j                  |       }||j                  d      j                  dd      z  }t        | |||       y t        | |||       y )Nr6  r   r,  r   r   rQ   rP   )
rA   r   r  r   r   rU   exprT   r   r  )	r   r:  r   r   r   r   r   tempy_score_norms	            rY   (test_multiclass_sample_weight_invariancer    s     I%a(L!!!Qi\!:F!!!Qi\!:F((y!n(=GF""vvwhdhhBh/77A>>&tVV\J&tVVVDr[   c                 Z   t        d      }t        ddddd      \  }}t        ddddd      \  }}t        j                  ||g      }t        j                  ||g      }|j	                  dd|j
                        }t        |    }| t        v rt        | |||       y t        | |||       y )	Nr   rP   r  r6  Fr7  r  r   )	rA   r	   rU   r=  r   r   r   r   r  )	r   r   r1  yaybr   r   r   r   s	            rY   (test_multilabel_sample_weight_invariancer    s     &a(L*bRWEAr +bRWEAr YYBx FYYBx F""1afll";GF""&tVVWE&tVVVDr[   c                     t        j                  g dg dg      } t        j                  g dg dg      }t        j                  g d      }t        j                  g d      }t        j                  g d      }t        j                  |d      \  }}t        D ]Y  }||g| |gfD ]L  \  }}	|t        vr|	j
                  d	kD  rt        |   }
 |
||	|d 
      } |
||	d       }t        |||          N [ y )N)rP   rP   r   r   r   r   rP   rP   )r   rP   rP   r   )r   rP   ra   )r   ra   r  )r  r   rP   ra   T)return_inverserP   )r   rg   ri   )rU   r   uniquerq  r   r   r   r;   )y_true_multilabely_pred_multilabely_true_multiclassy_pred_multiclassr   r1  inverse_labelsr   r   r   r   score_labelsscores                rY   test_no_averaging_labelsr  )  s     ,!=>,!=>++XXl#F		&>A~& D 12 12
 	DNFF ..6;;? &F!&&NL6648E|U>-BC	DDr[   c                 "   t        d      }d\  }}|j                  dd||f      }|j                  dd||f      }t        |    } |||      }t        t	        |      |      D ])  }|d d |f   }	|d d |f   }
 ||
|	      }t        ||       + y )Nr   r   r  ra   r   )rA   r   r   r   r.  r:   r   r   r:  r9  r   r   r   r  r2  y_score_permy_true_permcurrent_scores               rY   -test_multilabel_label_permutations_invariancer  C  s     &a(L Iy!!!Qi-C!DF""1ay).D"EGF67#EU9-y9 2q$w'QWo{L9E=12r[   c                    t        d      }d\  }}|j                  dd||f      }|j                  |j                        }d||j	                  d      dk(  df<   d||j	                  d      dk(  df<   t
        |    } |||      }t        t        |      |      D ]Q  }|d d |f   }	|d d |f   }
 ||
|	      }|t        k(  rt        j                  |      sJ |dkD  rDJ t        ||       S y )Nr   r  ra   r   rP   r  g    .A)rA   r   r   r   rT   r   r   r.  r"   rU   r
  r:   r  s               rY   ?test_thresholded_multilabel_multioutput_permutations_invariancer  X  s    &a(L Iy!!!Qi-C!DF!!v||!4G %&F6::a=Aq !$%F6::a=Aq !F67#EU9-y9 6q$w'QWo{L933;;}--- 3&&&  }56r[   c                    d\  }}t        d      }|j                  ||      }t        j                  |       }||j	                  d      j                  dd      z  }|j                  d||      }t        |    } |||      }t        t        |      |      D ]r  }	t        j                  |t              }
t        j                  |      |
t        |	      <   |d d |
f   }t        j                  |	|      } |||      }t        ||       t y )N)r\  r  r   r   rQ   rP   r   rJ  )rA   randrU   r  rT   r   r   r   r   r.  r  rd  aranger   taker:   )r   r:  r9  r   r   r  r   r   r  r2  inverse_permr  r  r  s                 rY   .test_thresholded_metric_permutation_invariancer  y  s     "Iy%a(L	95G667(DTXX2X&..r155G!!!YY!?FF67#EU9-y9 2xx	5#%99Y#7T$Z q,/ggdF+{L9E=12r[   metric_namec                 L   t         j                  j                  d      }t        j                  dgdz  dgdz  z   t              }|j                  dd|j                        }d	}t        j                  t        |
      5  t        |    ||       d d d        y # 1 sw Y   y xY w)N*   r   r  r   ra   rJ  r   r   z7Labels in y_true and y_pred should be of the same type.r  )rU   r  r  r   rI  r   r   r   r   	TypeErrorr   )r  r  r   r   r   s        rY   "test_metrics_consistent_type_errorr    s     ))


#C	6(Q,&A-V	<B	Q	(BGG	y	0 4{+B34 4 4s   BB#zmetric, y_pred_thresholddtype_y_strc                    t         j                  j                  d      }t        j                  dgdz  dgdz  z   |      }|j	                  dd|j
                        }|st        j                  ddg|      |   }d	}d
}t        |       j                  d   j                  }|dk(  r|n|}	t        j                  t        |	      5   | ||       d d d        y # 1 sw Y   y xY w)Nr  r   r  r   ra   rJ  r   r   zy_true takes value in {'eggs', 'spam'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitzHpos_label=1 is not a valid label. It should be one of \['eggs', 'spam'\]r   rP   r  )rU   r  r  r   r   r   r   
parametersdefaultr   r   r   )
r   y_pred_thresholdr  r  r   r   err_msg_pos_label_Noneerr_msg_pos_label_1pos_label_defaultr   s
             rY    test_metrics_pos_label_error_strr    s    $ ))


#C	6(Q,&A-[	AB	Q	(BXXvv&k:2>	"  	X  "&)44[AII%6!%;!AWG	z	1 r2  s   
CC$c                 .   t        ||      }|j                  ||      }|j                  ||      }	 | |||      }
||j                  ||      }t        d      5   | ||	|      }t        ||
t	        |             d d d        y # 1 sw Y   y xY w)N)devicer  T)array_api_dispatch)atol)r8   asarrayr   r9   r6   )r   array_namespacer  
dtype_name	y_true_np	y_pred_npr  xp	y_true_xp	y_pred_xp	metric_np	metric_xps               rY   check_array_api_metricr    s     
ov	6B

9V
4I

9V
4Iy)=II 

=
@	4	0 
9i}M	
+	

 
 
s   #BBc           	          t        j                  g d      }t        j                  g d      }t        | |||||d        t        j                  g d|      }t        | ||||||       y )Nr  )r   rP   r   rP   r  r  r  g        r  g       @g      ?rJ  rU   r   r  r   r  r  r  r  r  r  s          rY   ,check_array_api_binary_classification_metricr    m     &I&I HH1DM#r[   c           	          t        j                  g d      }t        j                  g d      }t        | |||||d        t        j                  g d|      }t        | ||||||       y )N)r   rP   ra   r  )r   rP   r   ra   r  r  rJ  r  r  s          rY   0check_array_api_multiclass_classification_metricr    r  r[   c              #   V   K   | j                         D ]  \  }}|D ]  }||f 
  y wr   )items)metric_checkersr   checkerscheckers       rY   !yield_metric_checker_combinationsr  $  s<     +113 " 	"G'/!	""s   ')z#array_namespace, device, dtype_namezmetric, check_funcc                      || |||       y r   r   )r   r  r  r  
check_funcs        rY   test_array_api_compliancer  *  s    
 v
;r[   )	functoolsr   inspectr   	itertoolsr   r   r   numpyrU   r   sklearn._configr   sklearn.datasetsr	   sklearn.metricsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   sklearn.metrics._baser3   sklearn.preprocessingr4   sklearn.utilsr5   sklearn.utils._array_apir6   r7   sklearn.utils._testingr8   r9   r:   r;   r<   r=   sklearn.utils.fixesr>   sklearn.utils.multiclassr?   sklearn.utils.validationr@   rA   REGRESSION_METRICSr   r   CURVE_METRICSr   dictr   updater   METRIC_UNDEFINED_MULTICLASSunionr   rq  rr  r   r   METRICS_WITH_NORMALIZE_OPTIONr   r   r   r   r   METRICS_WITHOUT_SAMPLE_WEIGHTr   r   r   markparametrizesortedr   r   r   r   r   r   r   r  infr   invalids_nan_infr  r  r  r  r  r"  r%  r'  r*  r3  rG  rN  rX  rZ  intersectionr]  rm  rs  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  strrI  r  r  r  r  array_api_metric_checkersr  r  r   r[   rY   <module>r     s     2 2   * ;* * * * * * * * * * *V 8 0 !  / 3 EH .,*2&D 8.AB#$9C2.&-.C3&O 0<(6 $6n666 '0GRV(W6 "7>U#K	6 $%56 " $6 /0K6 6w#86$ L%6& ]'6( !'-5"I)6, ]-6. /60 L162 364 !,566 '+C0768 096: 7;
M;6< :>=6> jqI?6@  LA6B W\:FC6D gmZHE6F W3GG6H gh8I6J gk7CK6L W_gFM6N ',@O6P 7='BQ6R W3GS6T gh8U6V gk7CW6X W_gFY6Z ",@"='B!+ysK)<YQG&	J#L)D$]IF*k6 r@ F n ,    WX?	 
 *  (  ]  zB  w}i@  W]G<  7='uM  Gzu  7='uM   Gzu! & w}c:' ( 6) * ')+ 0 (/( &--Dg%V-R0?  D f   & '   ) *   % &   = !   * &=%B%B& "
 & " (! H! " ( D
  B  H!  	@ (9!:;
 <
< (=!>?< @<" 
F3{#&HHI

& '
 '
T 
F3{#&HHIs's'l 
F3-.1SST22j !45'/ 6'/V VbffbffVbffbffVbffbffVbffa[Vbffa[  e'..02D2K2K2MN *,<=  >  #9#@#@#BC
 &&!Q#
&&!Q#	
 
 D , #9#@#@#BC  D     7 7 

K -	- !
"		#		 (;>Q(Q!RS* T* (;!<= > (;!<=
 >
$ .9H
 : H
V (;!<= >  (E!FG H8 (E!FG H8 
F0==>QRS,,^ 32 32lO" (>!?@U AU 
F),NNOUU$ (>!?@U AU& (>!?@U AU ^P ^PB 

K%%c*<&=>
'	(AA 

K
 
!	"
'	( "	"EE 

K
 
!	"
'	( -	-EE" 

	=	=@S	S
'	(EE( D D2 
F&*T)UUV22$ 
F14GGH66< 
F3*+.PPQ22* (>?	4 @	4 	 $'	4 	5	1	%u-		&	% 	u	D
 f6 74
.:< 48 48	  7P " )+T+V -/P/RS< T<r[   