
    Mpj                     r    S r SSKJr  SSKrSSKJs  Jr  SSK	J
r
Jr  SSKJrJrJr  SSKJr  S	S jrS
S jrg)z
Common code for all metrics.

    )combinationsN)check_arraycheck_consistent_length)_average_ravelget_namespace_and_device)type_of_targetc                    [        X$5      u  pVnSnX8;  a  [        SR                  U5      5      e[        U5      n	U	S;  a  [        SR                  U	5      5      eU	S:X  a  U " XUS9$ [	        XU5        [        U5      n[        U5      nSn
UnSnUS	:X  a8  Ub  UR                  XR                  S   5      n[        U5      n[        U5      nOUS
:X  a  UbI  UR                  XR                  S9nUR                  UR                  XR                  US5      5      SS9nOUR                  USS9n[        R                  " UR                  U5      UR                  SUR                  US95      (       a  gOUS:X  a  UnSnSn
UR                   S:X  a  UR                  US5      nUR                   S:X  a  UR                  US5      nUR                  U
   nUR#                  U4US9n[%        U5       H]  n[        UR'                  XR                  U/US9U
S95      n[        UR'                  X%R                  U/US9U
S95      nU " UUUS9X'   M_     Ub  Ub  SXS:H  '   [)        [+        XUS95      $ U$ )a  Average a binary metric for multilabel classification.

Parameters
----------
binary_metric : callable, returns shape [n_classes]
    The binary metric function to use.

y_true : array, shape = [n_samples] or [n_samples, n_classes]
    True binary labels in binary label indicators.

y_score : array, shape = [n_samples] or [n_samples, n_classes]
    Target scores, can either be probability estimates of the positive
    class or non-thresholded decision values (as returned by
    :term:`decision_function` on some classifiers).

average : {None, 'micro', 'macro', 'samples', 'weighted'}, default='macro'
    If ``None``, the scores for each class are returned. Otherwise,
    this determines the type of averaging performed on the data:

    ``'micro'``:
        Calculate metrics globally by considering each element of the label
        indicator matrix as a label.
    ``'macro'``:
        Calculate metrics for each label, and find their unweighted
        mean.  This does not take label imbalance into account.
    ``'weighted'``:
        Calculate metrics for each label, and find their average, weighted
        by support (the number of true instances for each label).
    ``'samples'``:
        Calculate metrics for each instance, and find their average.

    Will be ignored when ``y_true`` is binary.

sample_weight : array-like of shape (n_samples,), default=None
    Sample weights.

Returns
-------
score : float or array of shape [n_classes]
    If not ``None``, average the score, else return the score for each
    classes.

)Nmicromacroweightedsampleszaverage has to be one of {0})binaryzmultilabel-indicatorz{0} format is not supportedr   )sample_weight   Nr   r   )dtype)r   r   )axis)r   devicer   )r   )weightsxp)r   
ValueErrorformatr	   r   r   repeatshaper   asarrayr   summultiplyreshapexpxisclosendimzerosrangetakefloatr   )binary_metricy_truey_scoreaverager   r   __deviceaverage_optionsy_typenot_average_axisscore_weightaverage_weight	n_classesscorecy_true_c	y_score_cs                     Q/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/sklearn/metrics/_base.py_average_binary_scorer8      s   X .gENB7EO%7>>OPPF#F776==fEFFVMJJF]; F'"G LN'#99\<<?CL/	J	#ZZ.@.@ZAFVVFJJ|W$EFQ $ N  VVFV3N;;FF>"JJq 4 4WJE
 
 	
 
I	%{{aFG,||q**Wg../IHHi\'H2E9GGFJJs7J;BRGS
 GGGZZGZ<CSGT
	 !9LQ  % *+EA%&XeCDD    c                 &   [        X5        [        R                  " U5      nUR                  S   nXUS-
  -  S-  n[        R                  " U5      nUS:H  nU(       a  [        R                  " U5      OSn	[        [        US5      5       Hq  u  n
u  pX:H  nX:H  n[        R                  " X5      nU(       a  [        R                  " U5      X'   X   nX   nU " UX/U4   5      nU " UX/U4   5      nUU-   S-  Xz'   Ms     [        R                  " XyS9$ )a  Average one-versus-one scores for multiclass classification.

Uses the binary metric for one-vs-one multiclass classification,
where the score is computed according to the Hand & Till (2001) algorithm.

Parameters
----------
binary_metric : callable
    The binary metric function to use that accepts the following as input:
        y_true_target : array, shape = [n_samples_target]
            Some sub-array of y_true for a pair of classes designated
            positive and negative in the one-vs-one scheme.
        y_score_target : array, shape = [n_samples_target]
            Scores corresponding to the probability estimates
            of a sample belonging to the designated positive class label

y_true : array-like of shape (n_samples,)
    True multiclass labels.

y_score : array-like of shape (n_samples, n_classes)
    Target scores corresponding to probability estimates of a sample
    belonging to a particular class.

average : {'macro', 'weighted'}, default='macro'
    Determines the type of averaging performed on the pairwise binary
    metric scores:
    ``'macro'``:
        Calculate metrics for each label, and find their unweighted
        mean. This does not take label imbalance into account. Classes
        are assumed to be uniformly distributed.
    ``'weighted'``:
        Calculate metrics for each label, taking into account the
        prevalence of the classes.

Returns
-------
score : float
    Average of the pairwise binary metric scores.
r   r      r   N)r   )	r   npuniquer   empty	enumerater   
logical_orr*   )r'   r(   r)   r*   y_true_uniquer2   n_pairspair_scoresis_weighted
prevalenceixaba_maskb_maskab_maska_trueb_truea_true_scoreb_true_scores                       r7   _average_multiclass_ovo_scorerP      s
   P F,IIf%M##A&Iq=)Q.G((7#KZ'K&1'"tJ  ]A >?
FQ--/ZZ0JN$VWaZ-@A$VWaZ-@A',6!; @ ::k66r9   )N)r   )__doc__	itertoolsr   numpyr<   !sklearn.externals.array_api_extra	externalsarray_api_extrar    sklearn.utilsr   r   sklearn.utils._array_apir   r   r   sklearn.utils.multiclassr	   r8   rP    r9   r7   <module>r[      s7    #  / / > 
 4tnC7r9   