
    e!hN                         d Z ddlZddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ ddlmZ d	d
lmZmZ d	dlmZmZ ddZddZeeedZd Zd Zd Zd ZddZddZddZd Zy)zAUtilities to handle multiclass/multioutput target in classifiers.    N)Sequence)chain)issparse   )get_namespace)VisibleDeprecationWarning   )attach_uniquecached_unique)_assert_all_finitecheck_arrayc                     t        | |      \  }}t        | d      s|rt        |j                  |       |      S t	        |       S )Nxp	__array__)r   hasattrr   asarraysetyr   is_array_api_compliants      `/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/sklearn/utils/multiclass.py_unique_multiclassr      s?    !.qR!8Bq+"8RZZ]r221v    c                 z    t        | |      \  }}|j                  t        | dg d      j                  d         S )Nr   r   csrcsccoo)
input_nameaccept_sparser	   )r   aranger   shape)r   r   _s      r   _unique_indicatorr%      s<    !#EB99A#5JKQQRST r   )binary
multiclassmultilabel-indicatorc            	         t        | ddi} t        |  \  }t        |       dk(  rt        d      t	        d | D              }|ddhk(  rdh}t        |      dkD  rt        d	|z        |j                         }|d
k(  r)t        t	        d | D                    dkD  rt        d      t        j                  |d      st        dt        |       z        |r8j                  | D cg c]  } |       c}      }j                  |      S t	        t        j                  fd| D                    }t        t	        d |D                    dkD  rt        d      j                  t        |            S c c}w )a  Extract an ordered array of unique labels.

    We don't allow:
        - mix of multilabel and multiclass (single label) targets
        - mix of label indicator matrix and anything else,
          because there are no explicit labels)
        - mix of label indicator matrices of different sizes
        - mix of string and integer labels

    At the moment, we also don't allow "multiclass-multioutput" input type.

    Parameters
    ----------
    *ys : array-likes
        Label values.

    Returns
    -------
    out : ndarray of shape (n_unique_labels,)
        An ordered array of unique labels.

    Examples
    --------
    >>> from sklearn.utils.multiclass import unique_labels
    >>> unique_labels([3, 5, 5, 5, 7, 7])
    array([3, 5, 7])
    >>> unique_labels([1, 2, 3, 4], [2, 2, 3, 4])
    array([1, 2, 3, 4])
    >>> unique_labels([1, 2, 10], [5, 11])
    array([ 1,  2,  5, 10, 11])
    return_tupleTr   zNo argument has been passed.c              3   2   K   | ]  }t        |        y wN)type_of_target).0xs     r   	<genexpr>z unique_labels.<locals>.<genexpr>O   s     1>!$1s   r&   r'   r	   z'Mix type of y not allowed, got types %sr(   c              3   T   K   | ]   }t        |g d       j                  d    " yw)r   )r!   r	   N)r   r#   )r.   r   s     r   r0   z unique_labels.<locals>.<genexpr>\   s(      QRA-BCII!Ls   &(zCMulti-label binary indicator input with different numbers of labelsNzUnknown label type: %sr   c              3   @   K   | ]  }d   |      D          yw)c              3       K   | ]  }|  y wr,    )r.   is     r   r0   z*unique_labels.<locals>.<genexpr>.<genexpr>q   s     A1QAs   r   Nr4   )r.   r   _unique_labelsr   s     r   r0   z unique_labels.<locals>.<genexpr>q   s!     NaAqR(@AANs   c              3   <   K   | ]  }t        |t                y wr,   )
isinstancestr)r.   labels     r   r0   z unique_labels.<locals>.<genexpr>t   s     =%z%%=s   z,Mix of label input types (string and number))r
   r   len
ValueErrorr   pop_FN_UNIQUE_LABELSgetreprconcatunique_valuesr   from_iterabler   sorted)	ysr   ys_types
label_typer   	unique_ys	ys_labelsr6   r   s	          @@r   unique_labelsrJ   )   s   @ 
	.	.B!.!3B
2w!|788 1b11HHl++ >
8}qBXMNNJ 	,, VX 

  Q
 	

 '**:t<N1DH<==IIDA~aB7DE		**N2NNI 3=9==>BGHH::fY'(( Es   (E<c           
          t        |       \  }}|j                  | j                  d      xrQ t        |j	                  |j                  |j                  | |j                        | j                        | k(              S )Nreal floating)r   isdtypedtypeboolallastypeint64r   s      r   _is_integral_floatrS   z   sd    !.q!1B::agg/ D
ryy"))Arxx0177;q@A5 r   c                    t        |       \  }}t        | d      st        | t              s|rWt	        dddddd      }t        j                         5  t        j                  dt               	 t        | fddi|} ddd       t        | d
      r!| j                  dk(  r| j                  d   dkD  syt!        |       r| j"                  dv r| j%                         } |j'                  | j(                        }t+        | j(                        dk(  xsM |j,                  dk(  xs |j,                  dk(  xr d|v xr% | j.                  j0                  dv xs t3        |      S t5        | |      }|j                  d   dk  xr) |j7                  | j.                  d      xs t3        |      S # t        t        f$ r8}t        |      j                  d	      r t        | fdt        i|} Y d}~md}~ww xY w# 1 sw Y   wxY w)a~  Check if ``y`` is in a multilabel format.

    Parameters
    ----------
    y : ndarray of shape (n_samples,)
        Target values.

    Returns
    -------
    out : bool
        Return ``True``, if ``y`` is in a multilabel format, else ```False``.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.utils.multiclass import is_multilabel
    >>> is_multilabel([0, 1, 0, 1])
    False
    >>> is_multilabel([[1], [0, 2], []])
    False
    >>> is_multilabel(np.array([[1, 0], [0, 0]]))
    True
    >>> is_multilabel(np.array([[1], [0], [0]]))
    False
    >>> is_multilabel(np.array([[1, 0, 0]]))
    True
    r   TFr   r!   allow_ndensure_all_finite	ensure_2densure_min_samplesensure_min_featureserrorrN   NComplex data not supportedr#   r   r	   )doklilbiur      )rO   zsigned integerzunsigned integer)r   r   r8   r   dictwarningscatch_warningssimplefilterr   r   r<   r9   
startswithobjectndimr#   r   formattocsrrB   datar;   sizerN   kindrS   r   rM   )r   r   r   check_y_kwargselabelss         r   is_multilabelrp      s   8 "/q!1Bq+*Q"9=S #  !
 $$& 
	C!!'+DEC@@@
	C AwAFFaKAGGAJN{88~%	A!!!&&)K1 Fq HV[[A%5$HAK F&D*<V*D	
 qR(||A" 
JJqww NO *!&)	
/ .z: Cq6$$%AB  BB>BC	
	C 
	Cs0   G1.F''G.6-G)#G1)G..G11G;c                 D    t        | d      }|dvrt        d| d      y)aA  Ensure that target y is of a non-regression type.

    Only the following target types (as defined in type_of_target) are allowed:
        'binary', 'multiclass', 'multiclass-multioutput',
        'multilabel-indicator', 'multilabel-sequences'

    Parameters
    ----------
    y : array-like
        Target values.
    r   r    )r&   r'   zmulticlass-multioutputr(   zmultilabel-sequenceszUnknown label type: zy. Maybe you are trying to fit a classifier, which expects discrete classes on a regression target with continuous values.N)r-   r<   )r   y_types     r   check_classification_targetsrt      sB     A#.F   "6( +8 8
 	
r   c                     t               \  }} fd}t         t              xs t               xs t	         d      xr t         t
               xs |}|st        d z         j                  j                  dv }|rt        d      t               ryt        ddddd	d	
      }t        j                         5  t        j                  dt               t               s	 t         fddi| ddd       	 t               r
 d	gddf   n d	   }
t        |
t$              rt        j&                  dt(               t	        |
d      s+t        |
t              rt        |
t
              st        d       j,                  dvr |       S t/         j0                        s j,                  dk(  ry |       S t               s7 j2                  t"        k(  r$t         j4                  d	   t
              s |       S  j,                  dk(  r j0                  d   dkD  rd}nd}|j7                   j2                  d      rSt               r j8                  n }|j;                  ||j=                  |t>              k7        rtA        |       d|z   S t        
      r|
j8                  }
tC               j0                  d	   dkD  s j,                  dk(  rtE        |
      dkD  rd|z   S y# t        t        f$ r8}	t        |	      j!                  d      r t         fdt"        i| Y d}	~	3d}	~	ww xY w# 1 sw Y   =xY w# t*        $ r Y w xY w)a
  Determine the type of data indicated by the target.

    Note that this type is the most specific type that can be inferred.
    For example:

    * ``binary`` is more specific but compatible with ``multiclass``.
    * ``multiclass`` of integers is more specific but compatible with ``continuous``.
    * ``multilabel-indicator`` is more specific but compatible with
      ``multiclass-multioutput``.

    Parameters
    ----------
    y : {array-like, sparse matrix}
        Target values. If a sparse matrix, `y` is expected to be a
        CSR/CSC matrix.

    input_name : str, default=""
        The data name used to construct the error message.

        .. versionadded:: 1.1.0

    raise_unknown : bool, default=False
        If `True`, raise an error when the type of target returned by
        :func:`~sklearn.utils.multiclass.type_of_target` is `"unknown"`.

        .. versionadded:: 1.6

    Returns
    -------
    target_type : str
        One of:

        * 'continuous': `y` is an array-like of floats that are not all
          integers, and is 1d or a column vector.
        * 'continuous-multioutput': `y` is a 2d array of floats that are
          not all integers, and both dimensions are of size > 1.
        * 'binary': `y` contains <= 2 discrete values and is 1d or a column
          vector.
        * 'multiclass': `y` contains more than two discrete values, is not a
          sequence of sequences, and is 1d or a column vector.
        * 'multiclass-multioutput': `y` is a 2d array that contains more
          than two discrete values, is not a sequence of sequences, and both
          dimensions are of size > 1.
        * 'multilabel-indicator': `y` is a label indicator matrix, an array
          of two dimensions with at least two columns, and at most 2 unique
          values.
        * 'unknown': `y` is array-like but none of the above, such as a 3d
          array, sequence of sequences, or an array of non-sequence objects.

    Examples
    --------
    >>> from sklearn.utils.multiclass import type_of_target
    >>> import numpy as np
    >>> type_of_target([0.1, 0.6])
    'continuous'
    >>> type_of_target([1, -1, -1, 1])
    'binary'
    >>> type_of_target(['a', 'b', 'a'])
    'binary'
    >>> type_of_target([1.0, 2.0])
    'binary'
    >>> type_of_target([1, 0, 2])
    'multiclass'
    >>> type_of_target([1.0, 0.0, 3.0])
    'multiclass'
    >>> type_of_target(['a', 'b', 'c'])
    'multiclass'
    >>> type_of_target(np.array([[1, 2], [3, 1]]))
    'multiclass-multioutput'
    >>> type_of_target([[1, 2]])
    'multilabel-indicator'
    >>> type_of_target(np.array([[1.5, 2.0], [3.0, 1.6]]))
    'continuous-multioutput'
    >>> type_of_target(np.array([[0, 1], [1, 1]]))
    'multilabel-indicator'
    c                  8    rrnd} t        d|  d      y)zdDepending on the value of raise_unknown, either raise an error or return
        'unknown'.
        rj   zUnknown label type for z: unknown)r<   )inputr    raise_unknownr   s    r   _raise_or_returnz(type_of_target.<locals>._raise_or_return4  s-     ",J&E6ugRuEFFr   r   z:Expected array-like (array or non-string sequence), got %r)SparseSeriesSparseArrayz1y cannot be class 'SparseSeries' or 'SparseArray'r(   TFr   rU   r[   rN   Nr\   zSupport for labels represented as bytes is deprecated in v1.5 and will error in v1.7. Convert the labels to a string or integer format.zYou appear to be using a legacy multi-label data representation. Sequence of sequences are no longer supported; use a binary array or sparse matrix instead - the MultiLabelBinarizer transformer can convert to this format.)r	   r   r	   r&   r   z-multioutput rL   rr   
continuousr'   )#r   r8   r   r   r   r9   r<   	__class____name__rp   ra   rb   rc   rd   r   r   re   rf   byteswarnFutureWarning
IndexErrorrg   minr#   rN   flatrM   rj   anyrQ   intr   r   r;   )r   r    ry   r   r   rz   validsparse_pandasrm   rn   first_row_or_valsuffixrj   s   ```          r   r-   r-      s%   Z "/q!1B 
Ax	 	JHQK	J71k3J 	#1c""	"! 
 H1L
 	
 KK((,KKMLMMQ% N 
	 	 	" Cg'@A{C@@@	C )11aS!V91&.MM  (+6+X6/5;  	vvV!!qww<66Q;!!A;177f,Zq	35O!! 	vv{qwwqzA~ 
zz!''?+!!qvv!66$"))D#../t
;&((  !+00Qa 1$1=M9NQR9Rf$$S .z: Cq6$$%AB  BB>BCC CN  sC   &L1(K'?A;L> 'L.6-L)#L1)L..L11L;>	M
Mc                    t        | dd      |t        d      |ct        | dd      Et        j                  | j                  t        |            st        d|d| j                        yt        |      | _        yy)a"  Private helper function for factorizing common classes param logic.

    Estimators that implement the ``partial_fit`` API need to be provided with
    the list of possible classes at the first call to partial_fit.

    Subsequent calls to partial_fit should check that ``classes`` is still
    consistent with a previous value of ``clf.classes_`` when provided.

    This function returns True if it detects that this was the first call to
    ``partial_fit`` on ``clf``. In that case the ``classes_`` attribute is also
    set on ``clf``.

    classes_Nz8classes must be passed on the first call to partial_fit.z	`classes=z7` is not the same as on last call to partial_fit, was: TF)getattrr<   nparray_equalr   rJ   )clfclassess     r   _check_partial_fit_first_callr     s     sJ%-'/STT		3
D)5>>#,,g0FG 18#,,H   )1CL r   c                 ,   g }g }g }| j                   \  }}|t        j                  |      }t        |       r| j	                         } t        j
                  | j                        }t        |      D ]  }| j                  | j                  |   | j                  |dz       }	|1||	   }
t        j                  |      t        j                  |
      z
  }nd}
| j                   d   ||   z
  }t        j                  | j                  | j                  |   | j                  |dz       d      \  }}t        j                  ||
      }d|v r||dk(  xx   |z  cc<   d|vrC||   | j                   d   k  r.t        j                  |dd      }t        j                  |d|      }|j                  |       |j                  |j                   d          |j                  ||j                         z          nt        |      D ]  }t        j                  | dd|f   d      \  }}|j                  |       |j                  |j                   d          t        j                  ||      }|j                  ||j                         z          |||fS )az  Compute class priors from multioutput-multiclass target data.

    Parameters
    ----------
    y : {array-like, sparse matrix} of size (n_samples, n_outputs)
        The labels for each example.

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

    Returns
    -------
    classes : list of size n_outputs of ndarray of size (n_classes,)
        List of classes for each column.

    n_classes : list of int of size n_outputs
        Number of classes in each column.

    class_prior : list of size n_outputs of ndarray of size (n_classes,)
        Class distribution of each column.
    Nr	   r   T)return_inverse)weights)r#   r   r   r   tocscdiffindptrrangeindicessumuniquerj   bincountinsertappend)r   sample_weightr   	n_classesclass_prior	n_samples	n_outputsy_nnzkcol_nonzeronz_samp_weightzeros_samp_weight_sum	classes_ky_kclass_prior_ks                  r   class_distributionr     sU   , GIK77Iy 

=1{GGI!y! 	DA))AHHQK!((1q5/BK(!.{!;(*}(=~@V(V%!%()
U1X(=%YYqxx{QXXa!e_5dNIs KK^DM I~i1n-1FF- 	!eAh&;IIiA6	 "		-<Q RNN9%Y__Q/0}}/@/@/BBC9	D< y! 	DAYYqAwtDNIsNN9%Y__Q/0KK]CM}}/@/@/BBC	D Y,,r   c                    | j                   d   }t        j                  ||f      }t        j                  ||f      }d}t        |      D ]}  }t        |dz   |      D ]i  }|dd|fxx   |dd|f   z  cc<   |dd|fxx   |dd|f   z  cc<   || dd|f   dk(  |fxx   dz  cc<   || dd|f   dk(  |fxx   dz  cc<   |dz  }k  |dt        j                  |      dz   z  z  }	||	z   S )ay  Compute a continuous, tie-breaking OvR decision function from OvO.

    It is important to include a continuous value, not only votes,
    to make computing AUC or calibration meaningful.

    Parameters
    ----------
    predictions : array-like of shape (n_samples, n_classifiers)
        Predicted classes for each binary classifier.

    confidences : array-like of shape (n_samples, n_classifiers)
        Decision functions or predicted probabilities for positive class
        for each binary classifier.

    n_classes : int
        Number of classes. n_classifiers must be
        ``n_classes * (n_classes - 1 ) / 2``.
    r   r	   Nr`   )r#   r   zerosr   abs)
predictionsconfidencesr   r   votessum_of_confidencesr   r5   jtransformed_confidencess
             r   _ovr_decision_functionr     s)   & !!!$IHHi+,E9i"89	A9 q1ui( 	Aq!t$AqD(99$q!t$AqD(99$+ad#q(!+,1,+ad#q(!+,1,FA	 1	RVV&'!+, ***r   r,   )r}   F)__doc__rb   collections.abcr   	itertoolsr   numpyr   scipy.sparser   utils._array_apir   utils.fixesr   _uniquer
   r   
validationr   r   r   r%   r>   rJ   rS   rp   rt   r-   r   r   r   r4   r   r   <module>r      ss    G
  $   ! , 3 1 7 !$- N)bF
R
6FR FG-T*+r   