
    UgK                         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dS )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(|                    |                    |                     S t	          |           S )N	__array__)r   hasattrunique_valuesasarraysetyxpis_array_api_compliants      W/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/utils/multiclass.py_unique_multiclassr      sX    !.q!1!1Bq+ "8 

1...1vv    c                     t          |           \  }}|                    t          | dg d          j        d                   S )Nr   csrcsccoo)
input_nameaccept_sparser	   )r   aranger   shape)r   r   _s      r   _unique_indicatorr#      sK    !EB99A#5J5J5JKKKQRST  r   )binary
multiclassmultilabel-indicatorc                  n   t          |  \  }}| st          d          t          d | D                       }|ddhk    rdh}t          |          dk    rt          d|z            |                                }|dk    r9t          t          d | D                                 dk    rt          d	          t
                              |d
          st          dt          |           z            |r6|                    fd| D                       }|	                    |          S t          t          j        fd| D                                 }t          t          d |D                                 dk    rt          d          |                    t          |                    S )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   4   K   | ]}t          |          V  d S N)type_of_target).0xs     r   	<genexpr>z unique_labels.<locals>.<genexpr>N   s*      11>!$$111111r   r$   r%   r	   z'Mix type of y not allowed, got types %sr&   c              3   R   K   | ]"}t          |g d           j        d         V  #dS )r   )r   r	   N)r   r!   )r+   r   s     r   r-   z unique_labels.<locals>.<genexpr>[   sO        QRA-B-B-BCCCI!L     r   zCMulti-label binary indicator input with different numbers of labelsNzUnknown label type: %sc                 &    g | ]} |          S  r0   r+   r   _unique_labelss     r   
<listcomp>z!unique_labels.<locals>.<listcomp>l   s#    ===Q~~a00===r   c              3   B   K   | ]}d   |          D             V  dS )c              3      K   | ]}|V  d S r)   r0   )r+   is     r   r-   z*unique_labels.<locals>.<genexpr>.<genexpr>o   s"      (F(Fq(F(F(F(F(F(Fr   Nr0   r1   s     r   r-   z unique_labels.<locals>.<genexpr>o   s<      'S'S1(F(FNN14E4E(F(F(F'S'S'S'S'S'Sr   c              3   @   K   | ]}t          |t                    V  d S r)   )
isinstancestr)r+   labels     r   r-   z unique_labels.<locals>.<genexpr>q   s,      ==%z%%%======r   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_type	unique_ys	ys_labelsr2   s          @r   unique_labelsrI   )   s   @ "/!3B 97888 11b11111HHl+++ >
8}}qBXMNNNJ 	,,,  VX    
 

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

 '**:t<<N >1DHH<=== +II===="===>>		***E''S'S'S'SPR'S'S'SSSTTI
3==9=====>>BBGHHH::fY''(((r   c           
         t          |           \  }}|                    | j        d          oWt          |                    |                    |                    | |j                  | j                  | k                        S )Nreal floating)r   isdtypedtypeboolallastypeint64r   s      r   _is_integral_floatrR   w   sr    !.q!1!1B::ag// D
ryy"))Arx0017;;q@AA5 5 r   c                    t          |           \  }}t          | d          st          | t                    s|rt	          dddddd          }t          j                    5  t          j        dt                     	 t          | fddi|} nU# t          t          f$ rA}t          |                              d	          r t          | fdt          i|} Y d}~nd}~ww xY wddd           n# 1 swxY w Y   t          | d
          r| j        dk    r| j        d         dk    sdS t!          |           r| j        dv r|                                 } |                    | j                  }t+          | j                  dk    p6|j        dk    s|j        dk    o d|v o| j        j        dv pt3          |          S |                    |           }|j        d         dk     o)|                    | j        d          pt3          |          S )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errorrM   NComplex data not supportedr!   r   r	   )doklilbiu   )rN   zsigned integerzunsigned integer)r   r   r8   r   dictwarningscatch_warningssimplefilterr   r   r;   r9   
startswithobjectndimr!   r   formattocsrr   datar<   sizerM   kindrR   rL   )r   r   r   check_y_kwargselabelss         r   is_multilabelro   ~   s   8 "/q!1!1Bq+ C*Q"9"9 C=S C "  !
 
 
 $&& 
	C 
	C!'+DEEEC@@@@@-z: C C Cq66$$%ABB   BBB>BBC	
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C 
	C Aw AFaKKAGAJNNu{{ 
8~%%		A!!!&))KK1 Fq HV[A%5$HAK F&D*<V*D*D	
 !!!$$|A" 
JJqw NOO *!&))	
s<   "C/>BC/C 7CC/C  C//C36C3c                 V    t          | d          }|dvrt          d| d          dS )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_targetsrs      s\     A#...F    86 8 8 8
 
 	
 r    c                    t          |           \  }}t          | t                    st          |           st	          | d          rt          | t
                     p|}|st          d| z            | j        j        dv }|rt          d          t          |           rdS t          dddddd	          }t          j                    5  t          j        d
t                     t          |           sf	 t          | fddi|} nU# t          t          f$ rA}t          |                              d          r t          | fdt"          i|} Y d}~nd}~ww xY wddd           n# 1 swxY w Y   	 t          |           r| dgddf         n| d         }t          |t$                    rt          j        dt(                     t	          |d          s9t          |t                    r$t          |t
                    st          d          n# t*          $ r Y nw xY w| j        dvrdS t/          | j                  s| j        dk    rdS dS t          |           s2| j        t"          k    r"t          | j        d         t
                    sdS | j        dk    r| j        d         dk    rd}	nd}	|                    | j        d          r`t          |           r| j        n| }
|                    |
|                    |
t>                    k              rtA          |
|           d|	z   S t          |          r|j        }|!                    |           j        d         dk    s| j        dk    rtE          |          dk    rd|	z   S dS )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   rT   rZ   rM   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   unknownr	   r$   r   z-multioutputrt   rK   rq   
continuousr%   )#r   r8   r   r   r   r9   r;   	__class____name__ro   r`   ra   rb   rc   r   r   rd   re   byteswarnFutureWarning
IndexErrorrf   minr!   rM   flatrL   ri   anyrP   intr
   r   r<   )r   r   r   r   validsparse_pandasrl   rm   first_row_or_valsuffixri   s              r   r*   r*      sc   P "/q!1!1B	Ax	 	 	JHQKK	J71k3J3J 	#1c"""	"! 
  
H1L
 
 	
 K(,KKM NLMMMQ &%%   N 
	 	"	" C Cg'@AAA{{ 		CC@@@@@-z: C C Cq66$$%ABB   BBB>BBCC C C C C C C C C C C C C C C )1=1aS!!!V991&.. 	M    (+66	+X66	 /55	
 ;       	vVyqw<< 6Q;;8yA;; 17f,,Zq	35O5O,y 	v{{qwqzA~~ 
zz!'?++ )!!+qvv!66$"))D#...// 	)t
;;;;&((  !! 1+0	 #a''AFaKKC@P<Q<QTU<U<Uf$$xsO   *E'6DE'E7EE'EE''E+.E+3BH 
HHc                     t          | dd          |t          d          |it          | dd          Bt          j        | j        t          |                    st          d|d| j                  nt          |          | _        dS dS )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   rI   )clfclassess     r   _check_partial_fit_first_callr     s     sJ%%-'/STTT		3
D))5>#,g0F0FGG  j18#,,H   )11CL4 5r   c                 ^   g }g }g }| j         \  }}|t          j        |          }t          |           r|                                 } t          j        | j                  }t          |          D ]}| j        | j        |         | j        |dz                     }	|2||	         }
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         d                    |                    ||	                                z             nt          |          D ]}t          j
        | dd|f         d          \  }}|                    |           |                    |j         d                    t          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uniqueri   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     s   , GIK7Iy 
=11{{ 'DGGII!!y!! 	D 	DA)AHQK!(1q5/$ABK(!.{!;(*}(=(=~@V@V(V%%!%()
U1X(=%Yqx{QXa!e_45d  NIs K^DDDM I~~i1n---1FF--- 	!!eAh&;&;IiA66	 "	-<Q R RNN9%%%Y_Q/000}}/@/@/B/BBCCCC9	D< y!! 	D 	DAYqAwtDDDNIsNN9%%%Y_Q/000K]CCCM}}/@/@/B/BBCCCCY,,r   c                    | j         d         }t          j        ||f          }t          j        ||f          }d}t          |          D ]}t          |dz   |          D ]}|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  }|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   r6   jtransformed_confidencess
             r   _ovr_decision_functionr     s~   & !!$IHi+,,E9i"899	A9  q1ui(( 	 	Aqqq!t$$$AAAqD(99$$$qqq!t$$$AAAqD(99$$$+aaad#q(!+,,,1,,,+aaad#q(!+,,,1,,,FAA	 1	RV&''!+, ***r   )rt   r)   )__doc__ra   collections.abcr   	itertoolsr   numpyr   scipy.sparser   utils._array_apir   utils.fixesr   
validationr
   r   r   r#   r>   rI   rR   ro   rs   r*   r   r   r   r0   r   r   <module>r      sn   G G  $ $ $ $ $ $           ! ! ! ! ! ! , , , , , , 3 3 3 3 3 3 7 7 7 7 7 7 7 7     !$-  K) K) K)\  F
 F
 F
R
 
 
6v v v vr       FG- G- G- G-T*+ *+ *+ *+ *+r   