o
    ôj§O  ã                   @   sÚ   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d„Zd#dd„ZeeedœZdd„ Zdd„ Zdd„ Zdd„ Zd$dd„Zd#dd„Zd#dd „Zd!d"„ ZdS )%zAUtilities to handle multiclass/multioutput target in classifiers.é    N)ÚSequence)Úchain)Úissparseé   )Úget_namespace)ÚVisibleDeprecationWarningé   )Úattach_uniqueÚcached_unique)Ú_assert_all_finiteÚcheck_arrayc                 C   s8   t | |d�\}}t| dƒs|rt| | ¡|d�S t| ƒS )N©ÚxpÚ	__array__)r   Úhasattrr
   ÚasarrayÚset©Úyr   Úis_array_api_compliant© r   úk/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sklearn/utils/multiclass.pyÚ_unique_multiclass   s   r   c                 C   s.   t | |d�\}}| t| dg d¢d�jd ¡S )Nr   r   ©ÚcsrÚcscÚcoo)Ú
input_nameÚaccept_sparser   )r   Úaranger   Úshape)r   r   Ú_r   r   r   Ú_unique_indicator   s   ÿr"   )ÚbinaryÚ
multiclassúmultilabel-indicatorc                     s2  t | ddiŽ} t| Ž \‰}t| ƒdkrtdƒ‚tdd„ | D ƒƒ}|ddhkr)dh}t|ƒd	kr5td
| ƒ‚| ¡ }|dkrNttdd„ | D ƒƒƒd	krNtdƒ‚t |d¡‰ ˆ s^tdt| ƒ ƒ‚|rrˆ 	‡ ‡fdd„| D ƒ¡}ˆ 
|¡S tt ‡ ‡fdd„| D ƒ¡ƒ}ttd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])
    Úreturn_tupleTr   zNo argument has been passed.c                 s   s   � | ]}t |ƒV  qd S ©N)Útype_of_target)Ú.0Úxr   r   r   Ú	<genexpr>O   s   € z unique_labels.<locals>.<genexpr>r#   r$   r   z'Mix type of y not allowed, got types %sr%   c                 s   s&   � | ]}t |g d ¢d�jd V  qdS )r   )r   r   N)r   r    ©r)   r   r   r   r   r+   \   s   € 
ÿzCMulti-label binary indicator input with different numbers of labelsNzUnknown label type: %sc                    s   g | ]}ˆ |ˆd �‘qS )r   r   r,   ©Z_unique_labelsr   r   r   Ú
<listcomp>m   s    z!unique_labels.<locals>.<listcomp>c                 3   s&   � | ]}d d„ ˆ |ˆd�D ƒV  qdS )c                 s   s   � | ]}|V  qd S r'   r   )r)   Úir   r   r   r+   q   s   € z*unique_labels.<locals>.<genexpr>.<genexpr>r   Nr   r,   r-   r   r   r+   q   s   €$ c                 s   s   � | ]}t |tƒV  qd S r'   )Ú
isinstanceÚstr)r)   Úlabelr   r   r   r+   t   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   Zys_typesÚ
label_typeZ	unique_ysZ	ys_labelsr   r-   r   Úunique_labels)   sB    ÿÿûÿ
ÿr?   c              	   C   s@   t | ƒ\}}| | jd¡ot| | | | |j¡| j¡| k¡ƒS )Núreal floating)r   ÚisdtypeÚdtypeÚboolÚallÚastypeÚint64r   r   r   r   Ú_is_integral_floatz   s    ÿrG   c                 C   sŒ  t | ƒ\}}t| dƒst| tƒs|ritddddddd�}t ¡ �A t dt¡ zt	| fddi|¤Ž} W n& tt
fyY } zt|ƒ d	¡rE‚ t	| fdti|¤Ž} W Y d}~nd}~ww W d  ƒ n1 sdw   Y  t| d
ƒrz| jdkrz| jd dks|dS t| ƒr®| jdv r‰|  ¡ } | | j¡}t| jƒdkp­|jdks¤|jdko­d|v o­| jjdv p­t|ƒS t| |d�}|jd dk oÅ| | jd¡pÅt|ƒ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Úensure_all_finiteÚ	ensure_2dÚensure_min_samplesÚensure_min_featuresÚerrorrB   NúComplex data not supportedr    r   r   )ÚdokÚlilÚbiur   é   )rC   zsigned integerzunsigned integer)r   r   r0   r   ÚdictÚwarningsÚcatch_warningsÚsimplefilterr   r   r4   r1   Ú
startswithÚobjectÚndimr    r   ÚformatÚtocsrr:   Údatar3   ÚsizerB   ÚkindrG   r
   rA   )r   r   r   Úcheck_y_kwargsÚeÚlabelsr   r   r   Úis_multilabel�   sJ   ú
 €ú€ü"
þþrc   c                 C   s(   t | d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(   r4   )r   Zy_typer   r   r   Úcheck_classification_targetsÉ   s   
ÿùre   Ú Fc                    s  t ˆƒ\}}‡ ‡‡fdd„}tˆtƒstˆƒstˆdƒo!tˆtƒ p#|}|s,tdˆ ƒ‚ˆjjdv }|r8tdƒ‚t	ˆƒr>dS t
ddd	d	d
d
d�}t ¡ �E t dt¡ tˆƒsŠztˆfddi|¤Ž‰W n& ttfy‰ }	 zt|	ƒ d¡ru‚ tˆfdti|¤Ž‰W Y d}	~	nd}	~	ww W d  ƒ n1 s”w   Y  z/tˆƒr§ˆd
gdd…f nˆd
 }
t|
tƒr´tdƒ‚t|
dƒsÇt|
tƒrÇt|
tƒsÇtdƒ‚W n	 tyÑ   Y nw ˆjdvrÚ|ƒ S tˆjƒséˆjdkrædS |ƒ S tˆƒsýˆjtkrýtˆjd
 tƒsý|ƒ S ˆjdk�rˆjd dk�rd}nd}| ˆjd¡�r8tˆƒ�r ˆjnˆ}| || |t¡k¡�r8t|ˆ d� d| S t|
ƒ�r@|
j}
t ˆƒ}ˆjd
 dk�roˆjd
 |jd
   k�ret!dˆjd
  ƒk�ron ntj"dt#dd� |jd
 dk�s„ˆjdk�rˆt$|
ƒdk�rˆd| 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

    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                     s(   ˆrˆ rˆ nd} t d| › dˆ›�ƒ‚dS )zdDepending on the value of raise_unknown, either raise an error or return
        'unknown'.
        r]   zUnknown label type for z: Úunknown)r4   )Úinput©r   Úraise_unknownr   r   r   Ú_raise_or_return3  s   z(type_of_target.<locals>._raise_or_returnr   z:Expected array-like (array or non-string sequence), got %r)ÚSparseSeriesÚSparseArrayz1y cannot be class 'SparseSeries' or 'SparseArray'r%   TFr   rH   rN   rB   NrO   zkSupport for labels represented as bytes is not supported. Convert the labels to a string or integer format.zÝYou 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-multioutputrf   r@   rd   Ú
continuousé   g      à?z’The number of unique classes is greater than 50% of the number of samples. `y` could represent a regression problem, not a classification problem.)Ú
stacklevelr$   )%r   r0   r   r   r   r1   r4   Ú	__class__Ú__name__rc   rT   rU   rV   rW   r   r   rX   rY   ÚbytesÚ	TypeErrorÚ
IndexErrorrZ   Úminr    rB   ÚflatrA   r]   ÚanyrE   Úintr   r
   ÚroundÚwarnÚUserWarningr3   )r   r   rj   r   r   rk   ÚvalidZsparse_pandasr`   ra   Zfirst_row_or_valÚsuffixr]   Úclassesr   ri   r   r(   ä   s¨   M
þýÿú
	 €ú€û"
ÿÿþýÿ€ÿ


"
Fû*r(   c                 C   sr   t | ddƒdu r|du rtdƒ‚|dur7t | ddƒdur0t | jt|ƒ¡s.td|| jf ƒ‚dS 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.zD`classes=%r` is not the same as on last call to partial_fit, was: %rTF)Úgetattrr4   ÚnpÚarray_equalr€   r?   )Zclfr   r   r   r   Ú_check_partial_fit_first_call±  s   ÿÿ
ûr„   c                 C   sÖ  g }g }g }| j \}}|durt |¡}t| ƒr³|  ¡ } t | j¡}t|ƒD ]‹}| j| j| | j|d  … }	|durJ||	 }
t 	|¡t 	|
¡ }nd}
| j d ||  }tj
| j| j| | j|d  … dd�\}}tj||
d�}d|v r€||dk  |7  < d|vr›|| | j d k r›t |dd¡}t |d|¡}| |¡ | |j d ¡ | || 	¡  ¡ q&n3t|ƒD ].}tj
| dd…|f dd�\}}| |¡ | |j d ¡ tj||d�}| || 	¡  ¡ q·|||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Úuniquer]   ÚbincountÚinsertÚappend)r   Úsample_weightr   Ú	n_classesZclass_priorÚ	n_samplesÚ	n_outputsZy_nnzÚkZcol_nonzeroZnz_samp_weightZzeros_samp_weight_sumZ	classes_kÚy_kZclass_prior_kr   r   r   Úclass_distributionÔ  sH   


ÿ
ä

r—   c           
      C   s  | j d }t ||f¡}t ||f¡}d}t|ƒD ]X}t|d |ƒD ]N}|dd…|f  |dd…|f 8  < |dd…|f  |dd…|f 7  < || dd…|f dk|f  d7  < || dd…|f dk|f  d7  < |d7 }q"q|dt |¡d   }	||	 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   NrS   )r    r‚   ÚzerosrŠ   Úabs)
ZpredictionsZconfidencesr’   r“   ZvotesZsum_of_confidencesr•   r/   ÚjZtransformed_confidencesr   r   r   Ú_ovr_decision_function  s    
$$$$
ûÿr›   r'   )rf   F)Ú__doc__rU   Úcollections.abcr   Ú	itertoolsr   Únumpyr‚   Úscipy.sparser   Úutils._array_apir   Zutils.fixesr   Ú_uniquer	   r
   Ú
validationr   r   r   r"   r6   r?   rG   rc   re   r(   r„   r—   r›   r   r   r   r   Ú<module>   s2    

ýQH
 
N
#J