
    {KgK                         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 Z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   )_assert_all_finitecheck_arrayc                     t        |       \  }}t        | d      s|r |j                  |j                  |             S t	        |       S )N	__array__)r   hasattrunique_valuesasarraysetyxpis_array_api_compliants      \/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/sklearn/utils/multiclass.py_unique_multiclassr      sA    !.q!1Bq+"8

1..1v    c                 v    t        |       \  }}|j                  t        | dg d      j                  d         S )Nr   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        |  \  }}| s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        |r6|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])
    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>N   s     1b>!$bs   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   r-   z unique_labels.<locals>.<genexpr>[   s)      VXQRA-BCII!LVXs   &(zCMulti-label binary indicator input with different numbers of labelsNzUnknown label type: %sc              3   <   K   | ]  }d   |      D          yw)c              3       K   | ]  }|  y wr)    )r+   is     r   r-   z*unique_labels.<locals>.<genexpr>.<genexpr>o   s     (F4Eq4Es   Nr1   )r+   r   _unique_labelss     r   r-   z unique_labels.<locals>.<genexpr>o   s     'SPR1(FN14E(FPRs   c              3   <   K   | ]  }t        |t                y wr)   )
isinstancestr)r+   labels     r   r-   z unique_labels.<locals>.<genexpr>q   s     =9%z%%9s   z,Mix of label input types (string and number))r   
ValueErrorr   lenpop_FN_UNIQUE_LABELSgetreprconcatr   r   from_iterabler   sorted)	ysr   r   ys_types
label_typer   	unique_ys	ys_labelsr3   s	           @r   unique_labelsrF   )   sr   @ "/!3B788 1b11HHl++ >
8}qBXMNNJ 	,, VX 

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

 '**:t<N1DH<==II"="Q~a0"=>		**E'''SPR'SSTI
3=9==>BGHH::fY'(( >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_floatrO   w   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 |j'                  |       }|j                  d   dk  xr) |j5                  | j.                  d      xs t3        |      S # t        t        f$ r8}t        |      j                  d	      r t        | fdt        i|} Y d}~qd}~ww xY w# 1 sw Y   {x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force_all_finite	ensure_2densure_min_samplesensure_min_featureserrorrJ   NComplex data not supportedr!   r   r	   )doklilbiu   )rK   zsigned integerzunsigned integer)r   r   r5   r   dictwarningscatch_warningssimplefilterr   r   r8   r6   
startswithobjectndimr!   r   formattocsrr   datar9   sizerJ   kindrO   rI   )r   r   r   check_y_kwargselabelss         r   is_multilabelrl   ~   s   8 "/q!1Bq+*Q"9=S "  !
 $$&!!'+DEC@@@ ' AwAFFaKAGGAJN{88~%	A!!!&&)K1 Fq HV[[A%5$HAK F&D*<V*D	
 !!!$||A" 
JJqww NO *!&)	
/ .z: Cq6$$%AB  BB>BC	 '&s0   G5.F++G2:-G-'G5-G22G55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*   r8   )r   y_types     r   check_classification_targetsrp      sB     A#.F   "6( +8 8
 	
r   c                 p   t        |       \  }}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yt/        | j0                        s| j,                  dk(  ryyt        |       s1| j2                  t"        k(  rt        | j4                  d   t
              sy| 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                  }|jC                  |       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}~'d}~ww xY w# 1 sw Y   1x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

    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'
    r   z:Expected array-like (array or non-string sequence), got %r)SparseSeriesSparseArrayz1y cannot be class 'SparseSeries' or 'SparseArray'r&   TFr   rQ   rW   rJ   NrX   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   unknownr	   r$   r   z-multioutput rH   rn   
continuousr%   )#r   r5   r   r   r   r6   r8   	__class____name__rl   r]   r^   r_   r`   r   r   ra   rb   byteswarnFutureWarning
IndexErrorrc   minr!   rJ   flatrI   rf   anyrM   intr
   r   r9   )r   r   r   r   validsparse_pandasri   rj   first_row_or_valsuffixrf   s              r   r*   r*      s   P "/q!1B	Ax	 	JHQK	J71k3J 	#1c""	"! 
 H1L
 	
 KK((,KKMLMMQ% N 
	 	 	"g'@A{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	  #a'AFFaKC@P<QTU<Uf$$S .z: Cq6$$%AB  BB>BC 
#	"N  sC   7&LK5A;L( L -LLLLL%(	L54L5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)getattrr8   nparray_equalr   rF   )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uniquerf   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     sG   , GIK77Iy 

=1{GGI!y!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 "< y!AYYqAwtDNIsNN9%Y__Q/0KK]CM}}/@/@/BBC " 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   r2   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   )ru   r)   )__doc__r^   collections.abcr   	itertoolsr   numpyr   scipy.sparser   utils._array_apir   utils.fixesr   
validationr
   r   r   r#   r;   rF   rO   rl   rp   r*   r   r   r   r1   r   r   <module>r      sp    G  $   ! , 3 7 !$- K)\F
R
6vr FG-T*+r   