
    Kd"x                        d dl Z d dl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mZmZ ddlmZ ddlmZmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZmZm Z  g dZ! G d dee          Z" G d dee          Z# edgdg eeddd          g eeddd          gdgdd          d dddd            Z$d Z%d Z& G d dee          Z'dS )     N)defaultdict)Integral   )BaseEstimatorTransformerMixin_fit_context)column_or_1d)_encode_unique)Intervalvalidate_params)type_of_targetunique_labels)min_max_axis)_num_samplescheck_arraycheck_is_fitted)label_binarizeLabelBinarizerLabelEncoderMultiLabelBinarizerc                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )r   a  Encode target labels with value between 0 and n_classes-1.

    This transformer should be used to encode target values, *i.e.* `y`, and
    not the input `X`.

    Read more in the :ref:`User Guide <preprocessing_targets>`.

    .. versionadded:: 0.12

    Attributes
    ----------
    classes_ : ndarray of shape (n_classes,)
        Holds the label for each class.

    See Also
    --------
    OrdinalEncoder : Encode categorical features using an ordinal encoding
        scheme.
    OneHotEncoder : Encode categorical features as a one-hot numeric array.

    Examples
    --------
    `LabelEncoder` can be used to normalize labels.

    >>> from sklearn import preprocessing
    >>> le = preprocessing.LabelEncoder()
    >>> le.fit([1, 2, 2, 6])
    LabelEncoder()
    >>> le.classes_
    array([1, 2, 6])
    >>> le.transform([1, 1, 2, 6])
    array([0, 0, 1, 2]...)
    >>> le.inverse_transform([0, 0, 1, 2])
    array([1, 1, 2, 6])

    It can also be used to transform non-numerical labels (as long as they are
    hashable and comparable) to numerical labels.

    >>> le = preprocessing.LabelEncoder()
    >>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
    LabelEncoder()
    >>> list(le.classes_)
    ['amsterdam', 'paris', 'tokyo']
    >>> le.transform(["tokyo", "tokyo", "paris"])
    array([2, 2, 1]...)
    >>> list(le.inverse_transform([2, 2, 1]))
    ['tokyo', 'tokyo', 'paris']
    c                 P    t          |d          }t          |          | _        | S )zFit label encoder.

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

        Returns
        -------
        self : returns an instance of self.
            Fitted label encoder.
        Twarnr	   r   classes_selfys     <lib/python3.11/site-packages/sklearn/preprocessing/_label.pyfitzLabelEncoder.fitT   s(     &&&

    c                 Z    t          |d          }t          |d          \  | _        }|S )a  Fit label encoder and return encoded labels.

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

        Returns
        -------
        y : array-like of shape (n_samples,)
            Encoded labels.
        Tr   return_inverser   r   s     r!   fit_transformzLabelEncoder.fit_transforme   s4     &&&"1T:::qr#   c                     t          |            t          || j        j        d          }t	          |          dk    rt          j        g           S t          || j                  S )a  Transform labels to normalized encoding.

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

        Returns
        -------
        y : array-like of shape (n_samples,)
            Labels as normalized encodings.
        T)dtyper   r   )uniques)r   r	   r   r)   r   nparrayr
   r   s     r!   	transformzLabelEncoder.transformv   s_     	$-"5DAAA??a8B<<q$-0000r#   c                    t          |            t          |d          }t          |          dk    rt          j        g           S t          j        |t          j        t          | j                                      }t          |          rt          dt          |          z            t          j        |          }| j        |         S )a
  Transform labels back to original encoding.

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

        Returns
        -------
        y : ndarray of shape (n_samples,)
            Original encoding.
        Tr   r   z'y contains previously unseen labels: %s)r   r	   r   r+   r,   	setdiff1darangelenr   
ValueErrorstrasarray)r   r    diffs      r!   inverse_transformzLabelEncoder.inverse_transform   s     	&&&??a8B<<|AryT]););<<==t99 	TFTRSSSJqMM}Qr#   c                     ddgiS NX_types1dlabels r   s    r!   
_more_tagszLabelEncoder._more_tags       J<((r#   N)	__name__
__module____qualname____doc__r"   r'   r-   r6   r=   r;   r#   r!   r   r   "   sj        / /b  "  "1 1 1*     2) ) ) ) )r#   r   c                       e Zd ZU dZegegdgdZeed<   dddddZ e	d	
          d             Z
d Zd ZddZd ZdS )r   a
  Binarize labels in a one-vs-all fashion.

    Several regression and binary classification algorithms are
    available in scikit-learn. A simple way to extend these algorithms
    to the multi-class classification case is to use the so-called
    one-vs-all scheme.

    At learning time, this simply consists in learning one regressor
    or binary classifier per class. In doing so, one needs to convert
    multi-class labels to binary labels (belong or does not belong
    to the class). `LabelBinarizer` makes this process easy with the
    transform method.

    At prediction time, one assigns the class for which the corresponding
    model gave the greatest confidence. `LabelBinarizer` makes this easy
    with the :meth:`inverse_transform` method.

    Read more in the :ref:`User Guide <preprocessing_targets>`.

    Parameters
    ----------
    neg_label : int, default=0
        Value with which negative labels must be encoded.

    pos_label : int, default=1
        Value with which positive labels must be encoded.

    sparse_output : bool, default=False
        True if the returned array from transform is desired to be in sparse
        CSR format.

    Attributes
    ----------
    classes_ : ndarray of shape (n_classes,)
        Holds the label for each class.

    y_type_ : str
        Represents the type of the target data as evaluated by
        :func:`~sklearn.utils.multiclass.type_of_target`. Possible type are
        'continuous', 'continuous-multioutput', 'binary', 'multiclass',
        'multiclass-multioutput', 'multilabel-indicator', and 'unknown'.

    sparse_input_ : bool
        `True` if the input data to transform is given as a sparse matrix,
         `False` otherwise.

    See Also
    --------
    label_binarize : Function to perform the transform operation of
        LabelBinarizer with fixed classes.
    OneHotEncoder : Encode categorical features using a one-hot aka one-of-K
        scheme.

    Examples
    --------
    >>> from sklearn import preprocessing
    >>> lb = preprocessing.LabelBinarizer()
    >>> lb.fit([1, 2, 6, 4, 2])
    LabelBinarizer()
    >>> lb.classes_
    array([1, 2, 4, 6])
    >>> lb.transform([1, 6])
    array([[1, 0, 0, 0],
           [0, 0, 0, 1]])

    Binary targets transform to a column vector

    >>> lb = preprocessing.LabelBinarizer()
    >>> lb.fit_transform(['yes', 'no', 'no', 'yes'])
    array([[1],
           [0],
           [0],
           [1]])

    Passing a 2D matrix for multilabel classification

    >>> import numpy as np
    >>> lb.fit(np.array([[0, 1, 1], [1, 0, 0]]))
    LabelBinarizer()
    >>> lb.classes_
    array([0, 1, 2])
    >>> lb.transform([0, 1, 2, 1])
    array([[1, 0, 0],
           [0, 1, 0],
           [0, 0, 1],
           [0, 1, 0]])
    boolean	neg_label	pos_labelsparse_output_parameter_constraintsr      Fc                0    || _         || _        || _        d S NrE   )r   rF   rG   rH   s       r!   __init__zLabelBinarizer.__init__  s    ""*r#   Tprefer_skip_nested_validationc                    | j         | j        k    r t          d| j          d| j         d          | j        r5| j        dk    s| j         dk    rt          d| j         d| j                    t	          |d          | _        d	| j        v rt          d
          t          |          dk    rt          d|z            t          j        |          | _	        t          |          | _        | S )aa  Fit label binarizer.

        Parameters
        ----------
        y : ndarray of shape (n_samples,) or (n_samples, n_classes)
            Target values. The 2-d matrix should only contain 0 and 1,
            represents multilabel classification.

        Returns
        -------
        self : object
            Returns the instance itself.
        z
neg_label=z& must be strictly less than pos_label=.r   z`Sparse binarization is only supported with non zero pos_label and zero neg_label, got pos_label=z and neg_label=r    )
input_namemultioutput@Multioutput target data is not supported with label binarizationy has 0 samples: %r)rF   rG   r2   rH   r   y_type_r   spissparsesparse_input_r   r   r   s     r!   r"   zLabelBinarizer.fit  s.    >T^++/T^ / /!^/ / /  
  	4>Q#6#6$.A:M:MM!^M M<@NM M   &aC888DL((R   ??a2Q6777[^^%a((r#   c                 R    |                      |                              |          S )a  Fit label binarizer/transform multi-class labels to binary labels.

        The output of transform is sometimes referred to as
        the 1-of-K coding scheme.

        Parameters
        ----------
        y : {ndarray, sparse matrix} of shape (n_samples,) or                 (n_samples, n_classes)
            Target values. The 2-d matrix should only contain 0 and 1,
            represents multilabel classification. Sparse matrix can be
            CSR, CSC, COO, DOK, or LIL.

        Returns
        -------
        Y : {ndarray, sparse matrix} of shape (n_samples, n_classes)
            Shape will be (n_samples, 1) for binary problems. Sparse matrix
            will be of CSR format.
        )r"   r-   r   s     r!   r'   zLabelBinarizer.fit_transform5  s"    ( xx{{$$Q'''r#   c                 
   t          |            t          |                              d          }|r)| j                            d          st	          d          t          || j        | j        | j        | j	                  S )a  Transform multi-class labels to binary labels.

        The output of transform is sometimes referred to by some authors as
        the 1-of-K coding scheme.

        Parameters
        ----------
        y : {array, sparse matrix} of shape (n_samples,) or                 (n_samples, n_classes)
            Target values. The 2-d matrix should only contain 0 and 1,
            represents multilabel classification. Sparse matrix can be
            CSR, CSC, COO, DOK, or LIL.

        Returns
        -------
        Y : {ndarray, sparse matrix} of shape (n_samples, n_classes)
            Shape will be (n_samples, 1) for binary problems. Sparse matrix
            will be of CSR format.
        
multilabelz0The object was not fitted with multilabel input.)classesrG   rF   rH   )
r   r   
startswithrV   r2   r   r   rG   rF   rH   )r   r    y_is_multilabels      r!   r-   zLabelBinarizer.transformK  s    ( 	(++66|DD 	Q4<#:#:<#H#H 	QOPPPMnn,
 
 
 	
r#   Nc                 N   t          |            || j        | j        z   dz  }| j        dk    rt	          || j                  }nt          || j        | j        |          }| j        rt          j	        |          }n(t          j
        |          r|                                }|S )a  Transform binary labels back to multi-class labels.

        Parameters
        ----------
        Y : {ndarray, sparse matrix} of shape (n_samples, n_classes)
            Target values. All sparse matrices are converted to CSR before
            inverse transformation.

        threshold : float, default=None
            Threshold used in the binary and multi-label cases.

            Use 0 when ``Y`` contains the output of :term:`decision_function`
            (classifier).
            Use 0.5 when ``Y`` contains the output of :term:`predict_proba`.

            If None, the threshold is assumed to be half way between
            neg_label and pos_label.

        Returns
        -------
        y : {ndarray, sparse matrix} of shape (n_samples,)
            Target values. Sparse matrix will be of CSR format.

        Notes
        -----
        In the case when the binary labels are fractional
        (probabilistic), :meth:`inverse_transform` chooses the class with the
        greatest value. Typically, this allows to use the output of a
        linear model's :term:`decision_function` method directly as the input
        of :meth:`inverse_transform`.
        Ng       @
multiclass)r   rG   rF   rV   _inverse_binarize_multiclassr   _inverse_binarize_thresholdingrY   rW   
csr_matrixrX   toarray)r   Y	thresholdy_invs       r!   r6   z LabelBinarizer.inverse_transformm  s    @ 	$.8C?I<<''0DMBBEE24<	 E  	$M%((EE[ 	$MMOOEr#   c                     ddgiS r8   r;   r<   s    r!   r=   zLabelBinarizer._more_tags  r>   r#   rL   )r?   r@   rA   rB   r   rI   dict__annotations__rM   r   r"   r'   r-   r6   r=   r;   r#   r!   r   r      s         V Vr ZZ#$ $D    %&% + + + + +
 \555& & 65&P( ( (, 
  
  
D1 1 1 1f) ) ) ) )r#   r   
array-likeneither)closedrD   )r    r]   rF   rG   rH   TrN   rJ   FrE   c                x   t          | t                    st          | dddd          } n%t          |           dk    rt	          d| z            ||k    r#t	          d                    ||                    |r/|dk    s|dk    r#t	          d	                    ||                    |dk    }|r| }t          |           }d
|v rt	          d          |dk    rt	          d          t          j        |           r| j	        d         nt          |           }t          |          }t          j        |          }|dk    rk|dk    rP|rt          j        |dft                    S t          j        t          |           dft                    }	|	|z  }	|	S t          |          dk    rd}t          j        |          }
|dk    rmt#          | d          r| j	        d         nt          | d                   }|j        |k    r0t	          d                    |t'          |                               |dv rt)          |           } t          j        | |          }| |         }t          j        |
|          }t          j        dt          j        |          f          }t          j        |          }|                    |           t          j        |||f||f          }	nh|dk    rPt          j        |           }	|dk    r5t          j        |	j                  }|                    |           ||	_        nt	          d|z            |sK|	                                }	|	                    t          d          }	|dk    r	||	|	dk    <   |r	d|	|	|k    <   n&|	j                            t          d          |	_        t          j        ||
k              r!t          j        |
|          }|	dd|f         }	|dk    r7|r|	                    d          }	n|	dddf                              d          }	|	S )a  Binarize labels in a one-vs-all fashion.

    Several regression and binary classification algorithms are
    available in scikit-learn. A simple way to extend these algorithms
    to the multi-class classification case is to use the so-called
    one-vs-all scheme.

    This function makes it possible to compute this transformation for a
    fixed set of class labels known ahead of time.

    Parameters
    ----------
    y : array-like
        Sequence of integer labels or multilabel data to encode.

    classes : array-like of shape (n_classes,)
        Uniquely holds the label for each class.

    neg_label : int, default=0
        Value with which negative labels must be encoded.

    pos_label : int, default=1
        Value with which positive labels must be encoded.

    sparse_output : bool, default=False,
        Set to true if output binary array is desired in CSR sparse format.

    Returns
    -------
    Y : {ndarray, sparse matrix} of shape (n_samples, n_classes)
        Shape will be (n_samples, 1) for binary problems. Sparse matrix will
        be of CSR format.

    See Also
    --------
    LabelBinarizer : Class used to wrap the functionality of label_binarize and
        allow for fitting to classes independently of the transform operation.

    Examples
    --------
    >>> from sklearn.preprocessing import label_binarize
    >>> label_binarize([1, 6], classes=[1, 2, 4, 6])
    array([[1, 0, 0, 0],
           [0, 0, 0, 1]])

    The class ordering is preserved:

    >>> label_binarize([1, 6], classes=[1, 6, 4, 2])
    array([[1, 0, 0, 0],
           [0, 1, 0, 0]])

    Binary targets transform to a column vector

    >>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes'])
    array([[1],
           [0],
           [0],
           [1]])
    r    csrFN)rR   accept_sparse	ensure_2dr)   r   rU   z7neg_label={0} must be strictly less than pos_label={1}.zuSparse binarization is only supported with non zero pos_label and zero neg_label, got pos_label={0} and neg_label={1}rS   rT   unknownz$The type of target data is not knownbinaryrJ   r)      ra   multilabel-indicatorshapez:classes {0} mismatch with the labels {1} found in the data)rt   ra   rx   z7%s target data is not supported with label binarization)copy)r{   rJ   )!
isinstancelistr   r   r2   formatr   rW   rX   rx   r1   r+   r4   rd   intzerossorthasattrsizer   r	   in1dsearchsortedhstackcumsum
empty_likefilldatare   astypeanygetcolreshape)r    r]   rF   rG   rH   
pos_switchy_type	n_samples	n_classesrf   sorted_classy_n_classesy_in_classesy_seenindicesindptrr   s                    r!   r   r     su   L a 8 #Ue4
 
 
 ??a2Q6777IELL9 
 
 	
  
)q..INN vi++	
 
 	
 aJ J	AFN
 
 	
 ?@@@ k!nn8

#a&&IGIj!!G>> }i^3????Hc!ffa[444Y\\Q!F77##L'''$+Aw$7$7FagajjS1YY<;&&LSS]1--    )))OO wq'**</,77Ary66788}W%%		)M4&1)Y9OPPP	)	)	)M!>>=((DIIi   AFEN
 
 	
  
0IIKKHHSuH%%>>!Aa1fI 	" !Aa9ns// 
vg%&& /,88aaajM 	*AA!!!R%  ))AHr#   c                 0   t          j        |          }t          j        |           r|                                 } | j        \  }}t          j        |          }t          | d          d         }t          j        | j	                  }t          j
        ||          }t          j        || j        k              }|d         dk    r(t          j        |t          | j                  g          }t          j        || j	        dd                   }	t          j        | j        dg          }
|
||	                  }d|t          j        |dk              d         <   t          j        |          |dk    |                                dk    z           }|D ]N}| j        | j	        |         | j	        |dz                     }|t          j        ||                   d         ||<   O||         S |                    |                     d          d          S )z}Inverse label binarization transformation for multiclass.

    Multiclass uses the maximal score instead of a threshold.
    rJ   r{   r   N)axisclip)mode)r+   r4   rW   rX   tocsrrx   r0   r   r5   r   repeatflatnonzeror   appendr1   r   r   whereravelr/   takeargmax)r    r]   r   	n_outputsoutputsrow_maxrow_nnzy_data_repeated_maxy_i_all_argmaxindex_first_argmax	y_ind_ext
y_i_argmaxsamplesiinds                  r!   rb   rb   Y  s   
 j!!G	{1~~ !; GGII w	9)I&&q!$$Q''!(## i99(;qv(EFF 2;!Y~AF}EEN  _^QXcrc]KKIai!--	~.@AB
01
28GqL))!,- )I&&!18L'MN 	C 	CA)AHQK!(1q5/9:C#BL#$>$>?BJqMMz""||AHH!H,,6|:::r#   c                 d   |dk    rC| j         dk    r8| j        d         dk    r't          d                    | j                            |dk    r-| j        d         t	          |          k    rt          d          t          j        |          }t          j        |           r|dk    r[| j        dvr| 	                                } t          j
        | j        |k    t                    | _        |                                  nQt          j
        |                                 |k    t                    } nt          j
        | |k    t                    } |dk    rt          j        |           r|                                 } | j         dk    r#| j        d         dk    r|| d	d	df                  S t	          |          dk    r(t          j        |d         t	          |                     S ||                                          S |d
k    r| S t          d                    |                    )z=Inverse label binarization transformation using thresholding.rt   r   rJ   z'output_type='binary', but y.shape = {0}zAThe number of class is not equal to the number of dimension of y.r   )rp   cscru   Nrw   z{0} format is not supported)ndimrx   r2   r~   r1   r+   r4   rW   rX   r   r,   r   r   eliminate_zerosre   r   r   )r    output_typer]   rg   s       r!   rc   rc     s    h16Q;;171:>>BII!'RRSSSh171:W#=#=O
 
 	
 j!!G 
{1~~ 	/q==x~--GGIIXafy0<<<AFy0<<<AAHQ]#... h;q>> 			A6Q;;171:??1QQQT7##7||q  ySVV444qwwyy))	.	.	. 6==kJJKKKr#   c                       e Zd ZU dZddgdgdZeed<   ddddZ ed	
          d             Z	 ed	
          d             Z
d Zd Zd Zd Zd ZdS )r   a   Transform between iterable of iterables and a multilabel format.

    Although a list of sets or tuples is a very intuitive format for multilabel
    data, it is unwieldy to process. This transformer converts between this
    intuitive format and the supported multilabel format: a (samples x classes)
    binary matrix indicating the presence of a class label.

    Parameters
    ----------
    classes : array-like of shape (n_classes,), default=None
        Indicates an ordering for the class labels.
        All entries should be unique (cannot contain duplicate classes).

    sparse_output : bool, default=False
        Set to True if output binary array is desired in CSR sparse format.

    Attributes
    ----------
    classes_ : ndarray of shape (n_classes,)
        A copy of the `classes` parameter when provided.
        Otherwise it corresponds to the sorted set of classes found
        when fitting.

    See Also
    --------
    OneHotEncoder : Encode categorical features using a one-hot aka one-of-K
        scheme.

    Examples
    --------
    >>> from sklearn.preprocessing import MultiLabelBinarizer
    >>> mlb = MultiLabelBinarizer()
    >>> mlb.fit_transform([(1, 2), (3,)])
    array([[1, 1, 0],
           [0, 0, 1]])
    >>> mlb.classes_
    array([1, 2, 3])

    >>> mlb.fit_transform([{'sci-fi', 'thriller'}, {'comedy'}])
    array([[0, 1, 1],
           [1, 0, 0]])
    >>> list(mlb.classes_)
    ['comedy', 'sci-fi', 'thriller']

    A common mistake is to pass in a list, which leads to the following issue:

    >>> mlb = MultiLabelBinarizer()
    >>> mlb.fit(['sci-fi', 'thriller', 'comedy'])
    MultiLabelBinarizer()
    >>> mlb.classes_
    array(['-', 'c', 'd', 'e', 'f', 'h', 'i', 'l', 'm', 'o', 'r', 's', 't',
        'y'], dtype=object)

    To correct this, the list of labels should be passed in as:

    >>> mlb = MultiLabelBinarizer()
    >>> mlb.fit([['sci-fi', 'thriller', 'comedy']])
    MultiLabelBinarizer()
    >>> mlb.classes_
    array(['comedy', 'sci-fi', 'thriller'], dtype=object)
    rl   NrD   r]   rH   rI   Fc                "    || _         || _        d S rL   r   )r   r]   rH   s      r!   rM   zMultiLabelBinarizer.__init__  s    *r#   TrN   c                    d| _         | j        :t          t          t          j                            |                              }nMt          t          | j                            t          | j                  k     rt          d          | j        }t          d |D                       rt          nt          }t          j        t          |          |          | _        || j        dd<   | S )a  Fit the label sets binarizer, storing :term:`classes_`.

        Parameters
        ----------
        y : iterable of iterables
            A set of labels (any orderable and hashable object) for each
            sample. If the `classes` parameter is set, `y` will not be
            iterated.

        Returns
        -------
        self : object
            Fitted estimator.
        NztThe classes argument contains duplicate classes. Remove these duplicates before passing them to MultiLabelBinarizer.c              3   @   K   | ]}t          |t                    V  d S rL   r|   r   .0cs     r!   	<genexpr>z*MultiLabelBinarizer.fit.<locals>.<genexpr>  s,      ??!:a--??????r#   ru   )_cached_dictr]   sortedset	itertoolschainfrom_iterabler1   r2   allr   objectr+   emptyr   )r   r    r]   r)   s       r!   r"   zMultiLabelBinarizer.fit  s      !<S!>!>q!A!ABBCCGGT\""##c$,&7&777/   lG??w?????KVWU;;;"aaar#   c                    | j         (|                     |                              |          S d| _        t	          t
                    }|j        |_        |                     ||          }t          ||j
                  }t          d |D                       rt
          nt          }t          j        t          |          |          }||dd<   t          j        |d          \  | _        }t          j        ||j                 |j        j        d          |_        | j        s|                                }|S )	aM  Fit the label sets binarizer and transform the given label sets.

        Parameters
        ----------
        y : iterable of iterables
            A set of labels (any orderable and hashable object) for each
            sample. If the `classes` parameter is set, `y` will not be
            iterated.

        Returns
        -------
        y_indicator : {ndarray, sparse matrix} of shape (n_samples, n_classes)
            A matrix such that `y_indicator[i, j] = 1` iff `classes_[j]`
            is in `y[i]`, and 0 otherwise. Sparse matrix will be of CSR
            format.
        Nkeyc              3   @   K   | ]}t          |t                    V  d S rL   r   r   s     r!   r   z4MultiLabelBinarizer.fit_transform.<locals>.<genexpr>9  s,      ;;!:a--;;;;;;r#   ru   Tr%   F)r)   rz   )r]   r"   r-   r   r   r   __len__default_factory
_transformr   getr   r   r+   r   r1   uniquer   r,   r   r)   rH   re   )r   r    class_mappingyttmpr)   inverses          r!   r'   z!MultiLabelBinarizer.fit_transform  s"   $ <#88A;;((+++  $C(((5(=%__Q.. ](9::: ;;s;;;;;GS777aaa!#=!N!N!NwXgbj19IPUVVV
! 	B	r#   c                     t          |            |                                 }|                     ||          }| j        s|                                }|S )a  Transform the given label sets.

        Parameters
        ----------
        y : iterable of iterables
            A set of labels (any orderable and hashable object) for each
            sample. If the `classes` parameter is set, `y` will not be
            iterated.

        Returns
        -------
        y_indicator : array or CSR matrix, shape (n_samples, n_classes)
            A matrix such that `y_indicator[i, j] = 1` iff `classes_[j]` is in
            `y[i]`, and 0 otherwise.
        )r   _build_cacher   rH   re   )r   r    class_to_indexr   s       r!   r-   zMultiLabelBinarizer.transformE  sS      	**,,__Q//! 	B	r#   c           
          | j         Ft          t          | j        t	          t          | j                                                | _         | j         S rL   )r   rj   zipr   ranger1   r<   s    r!   r   z MultiLabelBinarizer._build_cache_  sB    $ $Sc$->P>P8Q8Q%R%R S SD  r#   c                    t          j         d          }t          j         ddg          }t                      }|D ]}t                      }|D ]C}	 |                    ||                    # t          $ r |                    |           Y @w xY w|                    |           |                    t          |                     |r;t          j        d	                    t          |t                                         t          j        t          |          t                    }	t          j        |	||ft          |          dz
  t          |          f          S )a/  Transforms the label sets with a given mapping.

        Parameters
        ----------
        y : iterable of iterables
            A set of labels (any orderable and hashable object) for each
            sample. If the `classes` parameter is set, `y` will not be
            iterated.

        class_mapping : Mapping
            Maps from label to column index in label indicator matrix.

        Returns
        -------
        y_indicator : sparse matrix of shape (n_samples, n_classes)
            Label indicator matrix. Will be of CSR format.
        r   r   z%unknown class(es) {0} will be ignoredr   ru   rJ   ry   )r,   r   addKeyErrorextendr   r1   warningsr   r~   r   r3   r+   onesr   rW   rd   )
r   r    r   r   r   rs   labelsindexlabelr   s
             r!   r   zMultiLabelBinarizer._transforme  sh   $ +c""S1#&&%% 	( 	(FEEE ' ''IImE23333 ' ' 'KK&&&&&'NN5!!!MM#g,,'''' 	M7>>vgSV?W?W?WXX   ws7||3///}7F#CKK!OS=O=O+P
 
 
 	
s   A,,BBc                     t                      j        d         t           j                  k    r@t	          d                    t           j                  j        d                             t          j                  r                                t          j	                  dk    r<t          t          j        j	        ddg                    dk    rt	          d           fdt          j        dd         j        dd                   D             S t          j        ddg          }t          |          dk    r"t	          d                    |                     fd	D             S )
a  Transform the given indicator matrix into label sets.

        Parameters
        ----------
        yt : {ndarray, sparse matrix} of shape (n_samples, n_classes)
            A matrix containing only 1s ands 0s.

        Returns
        -------
        y : list of tuples
            The set of labels for each sample such that `y[i]` consists of
            `classes_[j]` for each `yt[i, j] == 1`.
        rJ   z/Expected indicator for {0} classes, but got {1}r   z+Expected only 0s and 1s in label indicator.c           	      ~    g | ]9\  }}t          j                            j        ||                             :S r;   )tupler   r   r   )r   startendr   r   s      r!   
<listcomp>z9MultiLabelBinarizer.inverse_transform.<locals>.<listcomp>  sP       E3 dm((E#I)>??@@  r#   Nr{   z8Expected only 0s and 1s in label indicator. Also got {0}c                 ^    g | ])}t          j                            |                    *S r;   )r   r   compress)r   
indicatorsr   s     r!   r   z9MultiLabelBinarizer.inverse_transform.<locals>.<listcomp>  s1    SSS*E$-00<<==SSSr#   )r   rx   r1   r   r2   r~   rW   rX   r   r   r+   r/   r   r   )r   r   
unexpecteds   `` r!   r6   z%MultiLabelBinarizer.inverse_transform  s    	8A;#dm,,,,AHH&&    ;r?? 	TB27||q  Sbg1v)F)F%G%G!%K%K !NOOO    "%binbim"D"D   
 b1a&11J:"" NUU"   
 TSSSPRSSSSr#   c                     ddgiS )Nr9   2dlabelsr;   r<   s    r!   r=   zMultiLabelBinarizer._more_tags  r>   r#   )r?   r@   rA   rB   rI   rj   rk   rM   r   r"   r'   r-   r   r   r6   r=   r;   r#   r!   r   r     s        < <~ !$'#$ $D   
 #'e + + + + + \555  65@ \555) ) 65)V  4! ! !&
 &
 &
P'T 'T 'TR) ) ) ) )r#   r   )(r,   r   r   collectionsr   numbersr   numpyr+   scipy.sparsesparserW   baser   r   r   utilsr	   utils._encoder
   r   utils._param_validationr   r   utils.multiclassr   r   utils.sparsefuncsr   utils.validationr   r   r   __all__r   r   r   rb   rc   r   r;   r#   r!   <module>r      s         # # # # # #                 @ @ @ @ @ @ @ @ @ @             , , , , , , , , ? ? ? ? ? ? ? ? < < < < < < < < , , , , , , I I I I I I I I I I  C) C) C) C) C)#] C) C) C)Ly) y) y) y) y)%} y) y) y)x ^ >hxtIFFFGhxtIFFFG#  #'	 	 	 -.% h h h h	 	hV(; (; (;V)L )L )LXG) G) G) G) G)*M G) G) G) G) G)r#   