
    Ki,                        S SK r S SKrS SKrS SKJr  S SKJr  S SKrS SK	J
r  S SKJrJrJr  S SKJr  S SKJrJrJrJrJrJrJrJrJr  S SKJrJr  S SKJ r J!r!  S S	K"J#r#J$r$  S S
K%J&r&  S SK'J(r(J)r)J*r*  / SQr+ " S S\\SS9r, " S S\\SS9r-\!" SS/S/\ " \SSSS9/\ " \SSSS9/S/S.SS9S SSS.S j5       r.S"S jr/S"S jr0 " S  S!\\SS9r1g)#    N)defaultdict)Integral)BaseEstimatorTransformerMixin_fit_context)column_or_1d)	_convert_to_numpy_find_matching_floating_dtype_is_numpy_namespace_isindeviceget_namespaceget_namespace_and_deviceindexing_dtypexpx)_encode_unique)Intervalvalidate_params)type_of_targetunique_labels)min_max_axis)_num_samplescheck_arraycheck_is_fitted)LabelBinarizerLabelEncoderMultiLabelBinarizerlabel_binarizec                   D   ^  \ rS rSrSrS rS rS rS rU 4S jr	Sr
U =r$ )	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.preprocessing import LabelEncoder
>>> le = 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 = LabelEncoder()
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()
>>> list(le.classes_)
[np.str_('amsterdam'), np.str_('paris'), np.str_('tokyo')]
>>> le.transform(["tokyo", "tokyo", "paris"])
array([2, 2, 1]...)
>>> list(le.inverse_transform([2, 2, 1]))
[np.str_('tokyo'), np.str_('tokyo'), np.str_('paris')]
c                 :    [        USS9n[        U5      U l        U $ )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     ^/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/sklearn/preprocessing/_label.pyfitLabelEncoder.fitZ   s      &
    c                 >    [        USS9n[        USS9u  U l        nU$ )zFit 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_transformLabelEncoder.fit_transformk   s(     &"1T:qr-   c                     [        U 5        [        U5      u  p#[        XR                  R                  SS9n[        U5      S:X  a  UR                  / 5      $ [        XR                  S9$ )zTransform 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&   r4   r   asarrayr   )r(   r)   xp_s       r*   	transformLabelEncoder.transform|   sW     	a --"5"5DA?a::b>!q--00r-   c           	         [        U 5        [        U5      u  p#[        USS9n[        U5      S:X  a  UR	                  / 5      $ [
        R                  " UUR                  U R                  R                  S   [        U5      S9US9nUR                  S   (       a  [        S[        U5      -  5      eUR	                  U5      nUR                  U R                  USS9$ )zTransform labels back to original encoding.

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

Returns
-------
y_original : ndarray of shape (n_samples,)
    Original encoding.
Tr#   r   r   r7   z'y contains previously unseen labels: %saxis)r   r   r   r   r6   r   	setdiff1daranger&   shaper   
ValueErrorstrtake)r(   r)   r7   r8   diffs        r*   inverse_transformLabelEncoder.inverse_transform   s     	a &?a::b>!}}IIdmm))!,VAYI?

 ::a=FTRSSJJqMwwt}}aaw00r-   c                 v   > [         TU ]  5       nSUl        SUR                  l        SUR
                  l        U$ )NTF)super__sklearn_tags__array_api_support
input_tagstwo_d_arraytarget_tagsone_d_labelsr(   tags	__class__s     r*   rK   LabelEncoder.__sklearn_tags__   s7    w')!%&+#(,%r-   )r&   )__name__
__module____qualname____firstlineno____doc__r+   r1   r9   rG   rK   __static_attributes____classcell__rS   s   @r*   r   r   (   s'    /b""1,1< r-   r   )auto_wrap_output_keysc                      ^  \ rS rSr% Sr\/\/S/S.r\\S'   SSSS.S	 jr	\
" S
S9S 5       rS rS rSS jrU 4S jrSrU =r$ )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.preprocessing import LabelBinarizer
>>> lb = 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 = 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                (    Xl         X l        X0l        g Nra   )r(   rb   rc   rd   s       r*   __init__LabelBinarizer.__init__  s    ""*r-   Tprefer_skip_nested_validationc                    U R                   U R                  :  a&  [        SU R                    SU R                   S35      eU R                  (       aE  U R                  S:X  d  U R                   S:w  a%  [        SU R                   SU R                    35      e[	        U5      u  p#U(       a:  U R                  (       a)  [        U5      (       d  [        SUR                   S35      e[        US	S
9U l        SU R                  ;   a  [        S5      e[        U5      S:X  a  [        SU-  5      e[        R                  " U5      U l        [        U5      U l        U $ )a  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=>`sparse_output=True` is not supported for array API namespace <. Use `sparse_output=False` to return a dense array instead.r)   )
input_namemultioutput@Multioutput target data is not supported with label binarizationy has 0 samples: %r)rb   rc   rC   rd   r   r   rU   r   y_type_r   spissparsesparse_input_r   r&   )r(   r)   r7   is_array_apis       r*   r+   LabelBinarizer.fit  sI    >>T^^+T^^, -!^^,A/ 
 4>>Q#6$..A:M!^^,ODNN;KM  )+D..7J27N7N[[M *MM  &aC8DLL(R  ?a2Q677[[^%a(r-   c                 B    U R                  U5      R                  U5      $ )aQ  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+   r9   r'   s     r*   r1   LabelBinarizer.fit_transformN  s    ( xx{$$Q''r-   c                    [        U 5        [        U5      u  p#U(       a:  U R                  (       a)  [        U5      (       d  [	        SUR
                   S35      e[        U5      R                  S5      nU(       a+  U R                  R                  S5      (       d  [	        S5      e[        UU R                  U R                  U R                  U R                  S9$ )aK  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.
ro   rp   
multilabelz0The object was not fitted with multilabel input.)classesrc   rb   rd   )r   r   rd   r   rC   rU   r   
startswithru   r   r&   rc   rb   )r(   r)   r7   ry   y_is_multilabels        r*   r9   LabelBinarizer.transformd  s    ( 	(+D..7J27N7N[[M *MM  )+66|D4<<#:#:<#H#HOPPMMnnnn,,
 	
r-   c                 &   [        U 5        [        U5      u  p4U(       a:  U R                  (       a)  [        U5      (       d  [	        SUR
                   S35      eUc  U R                  U R                  -   S-  nU R                  S:X  a  [        XR                  US9nO[        XR                  U R                  X#S9nU R                  (       a  [        R                  " U5      nU$ [        R                  " U5      (       a  UR                  5       nU$ )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_original : {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`.
zY`LabelBinarizer` was fitted on a sparse matrix, and therefore cannot inverse transform a z array back to a sparse matrix.g       @
multiclassr=   )r   r   rx   r   rC   rU   rc   rb   ru   _inverse_binarize_multiclassr&   _inverse_binarize_thresholdingrv   
csr_matrixrw   toarray)r(   Y	thresholdr7   ry   y_invs         r*   rG    LabelBinarizer.inverse_transform  s    @ 	(+D..7J27N7N''){{m3RT 
 $..8C?I<<<'0MMbIE2<<	E MM%(E  [[MMOEr-   c                 h   > [         TU ]  5       nSUR                  l        SUR                  l        U$ NFT)rJ   rK   rM   rN   rO   rP   rQ   s     r*   rK   LabelBinarizer.__sklearn_tags__  /    w')&+#(,%r-   )r&   rb   rc   rx   rd   ru   rh   )rU   rV   rW   rX   rY   r   re   dict__annotations__ri   r   r+   r1   r9   rG   rK   rZ   r[   r\   s   @r*   r   r      sm    Vr ZZ#$D  %&% +
 5/ 6/b(,)
V9v r-   r   
array-likezsparse matrixneither)closedr`   )r)   r   rb   rc   rd   Trk   rf   Fra   c          	      	   [        U [        5      (       d  [        U SSSSS9n O[        U 5      S:X  a  [	        SU -  5      eX#:  a  [	        SR                  X#5      5      eU(       a&  US:X  d  US:w  a  [	        S	R                  X25      5      eUS:H  nU(       a  U* n[        U 5      nS
U;   a  [	        S5      eUS:X  a  [	        S5      e[        U 5      u  pxn	U(       a0  U(       a)  [        U5      (       d  [	        SUR                   S35      e UR                  XS9n[        U S5      (       a  U R                  S   O
[        U 5      nUR                  S   n[        U S5      (       a.  UR                  U R                   S5      (       a  U R                   nO[#        U5      nUS:X  aH  US:X  a:  U(       a  [$        R&                  " US4[(        S9$ UR+                  US4US9nX-  nU$ US:  a  SnUR-                  U5      nUS:X  aX  [        U S5      (       a  U R                  S   O[        U S   5      nUU:w  a$  [	        SR                  U[/        U 5      5      5      eUS;   a  [1        U 5      n [3        XUS9nU U   nUR5                  UU5      nUR7                  UU5      nUR9                  UR                  S/U	S9UR;                  USS945      nUR=                  UU5      n[$        R&                  " [?        UUS9[?        UUS9[?        UUS94X4S 9nU(       d  UR                  URA                  5       U	S9nOUS:X  a  U(       a@  [$        R&                  " U 5      nUS:w  a#  UR=                  URB                  U5      nUUl!        OW[$        RD                  " U 5      (       a  U RA                  5       n UR                  X	S!S"9nUS:w  a  X>US:g  '   O[	        S#U-  5      eU(       d,  US:w  a  X.US:H  '   U(       a  SXU:H  '   UR7                  XSS$9nO#URB                  R7                  [(        SS$9Ul!        URG                  X:g  5      (       a  UR5                  X5      nUSS2U4   nUS:X  a,  U(       a  USS2S%/4   nU$ URI                  USS2S%4   S&5      nU$ ! [        [        4 a  n
[	        SUR                   S35      U
eSn
A
ff = f)'an  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 or sparse matrix
    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)rq   accept_sparse	ensure_2dr4   r   rt   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}rr   rs   unknownz$The type of target data is not knownz?`sparse_output=True` is not supported for array API 'namespace z='. Use `sparse_output=False` to return a dense array instead.r<   z>`classes` contains unsupported dtype for array API namespace 'z'.rB   r4   integralbinaryrf   r4      r   multilabel-indicatorz:classes {0} mismatch with the labels {1} found in the data)r   r   r=   r>   rB   T)r   copyz7%s target data is not supported with label binarization)r   )r   rf   )%
isinstancelistr   r   rC   formatr   r   r   rU   r6   	TypeErrorhasattrrB   lenisdtyper4   r   rv   r   intzerossortr   r   r   searchsortedastypeconcatcumulative_sum	full_liker	   r   datarw   anyreshape)r)   r   rb   rc   rd   
pos_switchy_typer7   ry   device_e	n_samples	n_classes
int_dtype_r   sorted_classy_n_classesy_in_classesy_seenindicesindptrr   s                         r*   r   r     s   L a #Ue4
 ?a2Q677ELL
 	
 )q.IN vi+	
 	
 aJJ	AFN
 	
 ?@@ 8 ;Bg.A".E.E++ 'II
 	
**W*5 &a11
s1vIa Iq'rzz!'':>>WW
#B'
>}}i^3??HHi^:H>!^!F777#L''$+Aw$7$7aggajS1Y#LSS]1-  ))O QB/<//,7yyz:

A3w
/!!,Q!7
 ||GY/ MM!$2.!'b1!&R0
 (
 

199;w
7A	)	)a AA~||AFFI6{{1~~IIK

14
8AA~%!q&	 EN
 	
 >!a1fI !A9nIIa%I0s/ 
vvg%&&//,8ajM!bT'
A H 

1QU8W-AHW 	"  }B 
 	s   R S,SSc                    [         R                  " U 5      (       Ga  [        R                  " U5      nU R	                  5       n U R
                  u  p4[        R                  " U5      n[        U S5      S   n[        R                  " U R                  5      n[        R                  " Xg5      n[        R                  " XR                  :H  5      n	US   S:X  a+  [        R                  " U	[        U R                  5      /5      n	[        R                  " XR                  SS 5      n
[        R                  " U R                   S/5      nXU
      nSU[        R"                  " US:H  5      S   '   [        R                  " U5      US:  UR%                  5       S:H  -     nU HM  nU R                   U R                  U   U R                  US-       nU[        R&                  " X_5         S   X'   MO     X   $ [)        XS9u  nnnUR                  UUS9nUR+                  U SS9nUR-                  USUR
                  S   S-
  5      nUU   $ )zuInverse label binarization transformation for multiclass.

Multiclass uses the maximal score instead of a threshold.
rf   r   r   Nr=   r<   r>   )rv   rw   npr6   tocsrrB   rA   r   rF   r   repeatflatnonzeror   appendr   r   r   whereravelr@   r   argmaxclip)r)   r   r7   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indr8   r   r   s                      r*   r   r     s   
 
{{1~~**W% GGI ww	))I&q!$Q'''!((# ii9(;vv(EF 2;!YY~AFF}EN  __^XXcr]KIIaii!-	.@AB
01
288GqL)!,- ))I&!18L'MNA))AHHQK!((1q5/:C#BLL$>?BJM  ""1!;Aw**WW*5))AA)&'''1gmmA&6&:;wr-   c                    US:X  aG  U R                   S:X  a7  U R                  S   S:  a$  [        SR                  U R                  5      5      e[	        XS9u  pEnUR                  X&S9nUS:w  a+  U R                  S   UR                  S   :w  a  [        S5      e[        XS9n[        U S	5      (       a.  UR                  U R                  S
5      (       a  U R                  nO[        U5      n[        R                  " U 5      (       a  US:  a\  U R                  S;  a  U R                  5       n [        R                  " U R                   U:  ["        S9U l        U R%                  5         ODUR                  U R'                  5       U:  XS9n O"UR                  UR                  XUS9U:  UUS9n US:X  a  [        R                  " U 5      (       a  U R'                  5       n U R                   S:X  a  U R                  S   S:X  a  X SS2S4      $ UR                  S   S:X  a  UR)                  US   [+        U 5      5      $ X$R-                  U S5         $ US:X  a  U $ [        SR                  U5      5      e)z=Inverse label binarization transformation using thresholding.r      rf   z'output_type='binary', but y.shape = {0}r=   r<   r   zAThe number of class is not equal to the number of dimension of y.r4   r   )r   cscr   )r4   r   N)r   r   z{0} format is not supported)ndimrB   rC   r   r   r6   r
   r   r   r4   r   rv   rw   r   r   arrayr   r   eliminate_zerosr   r   r   r   )	r)   output_typer   r   r7   r8   r   dtype_r   s	            r*   r   r     s    h166Q;1771:>BII!''RSS-a7NB7jjj1Gh1771:q1A#AO
 	
 +14Fq'rzz!'':>>WW
#B'
 
{{1~~q=xx~-GGIXXaffy0<AF

199;2*
UAJJJJqwJ7)C  
 h;;q>>		A66Q;1771:?QT7##}}Q1$yySV44zz!U344	.	. 6==kJKKr-   c                      ^  \ rS rSr% SrSS/S/S.r\\S'   SSS.S	 jr\	" S
S9S 5       r
\	" S
S9S 5       rS rS rS rS rU 4S jrSrU =r$ )r   i  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)
r   Nr`   r   rd   re   Fc                    Xl         X l        g rh   r   )r(   r   rd   s      r*   ri   MultiLabelBinarizer.__init___  s    *r-   Trk   c                    SU l         U R                  c2  [        [        [        R
                  R                  U5      5      5      nOL[        [        U R                  5      5      [        U R                  5      :  a  [        S5      eU R                  n[        S U 5       5      (       a  [        O[        n[        R                  " [        U5      US9U l        X R                  SS& U $ )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   B   #    U  H  n[        U[        5      v   M     g 7frh   r   r   .0cs     r*   	<genexpr>*MultiLabelBinarizer.fit.<locals>.<genexpr>  s     ?w!:a--w   r   )_cached_dictr   sortedset	itertoolschainfrom_iterabler   rC   allr   objectr   emptyr&   )r(   r)   r   r4   s       r*   r+   MultiLabelBinarizer.fitc  s      !<<S!>!>q!ABCGT\\"#c$,,&77/  llG?w???VWU;"ar-   c                 p   U R                   b   U R                  U5      R                  U5      $ SU l        [	        [
        5      nUR                  Ul        U R                  X5      n[        X"R                  S9n[        S U 5       5      (       a  [
        O[        n[        R                  " [        U5      US9nXBSS& [        R                   " USS9u  U l        n[        R$                  " XcR&                     UR&                  R(                  S9Ul        U R*                  (       d  UR-                  5       nU$ )a  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   B   #    U  H  n[        U[        5      v   M     g 7frh   r   r   s     r*   r   4MultiLabelBinarizer.fit_transform.<locals>.<genexpr>  s     ;s!:a--sr   r   Tr/   )r   r+   r9   r   r   r   __len__default_factory
_transformr   getr   r   r   r   r   uniquer&   r6   r   r4   rd   r   )r(   r)   class_mappingyttmpr4   inverses          r*   r1   !MultiLabelBinarizer.fit_transform  s    $ <<#88A;((++  $C((5(=(=%__Q. ](9(9: ;s;;;S7a!#=!NwZZ

 32::;K;KL
!!B	r-   c                     [        U 5        U R                  5       nU R                  X5      nU R                  (       d  UR	                  5       nU$ )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   rd   r   )r(   r)   class_to_indexr  s       r*   r9   MultiLabelBinarizer.transform  sA      	**,__Q/!!B	r-   c           
          U R                   c@  [        [        U R                  [	        [        U R                  5      5      5      5      U l         U R                   $ rh   )r   r   zipr&   ranger   )r(   s    r*   r   MultiLabelBinarizer._build_cache  s@    $ $Sc$-->P8Q%R SD   r-   c           	      |   [         R                   " S5      n[         R                   " SS/5      n[        5       nU HU  n[        5       nU H  n UR                  X(   5        M     UR	                  U5        UR                  [        U5      5        MW     U(       a1  [        R                  " SR                  [        U[        S95      5        [        R                  " [        U5      [        S9n	[        R                   " XU4[        U5      S-
  [        U5      4S9$ ! [         a    UR                  U5         M  f = f)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   r   rf   r   )r   r   addKeyErrorextendr   r   warningsr$   r   r   rD   r   onesr   rv   r   )
r(   r)   r  r   r   r   labelsindexlabelr   s
             r*   r   MultiLabelBinarizer._transform  s    $ ++c"S1#&%FEE'IIm23  
 NN5!MM#g,'  MM7>>vgSV?WX wws7|3/}}F#CK!OS=O+P
 	
   'KK&'s   DD;:D;c                    [        U 5        UR                  S   [        U R                  5      :w  a;  [	        SR                  [        U R                  5      UR                  S   5      5      e[        R                  " U5      (       a  UR                  5       n[        UR                  5      S:w  a;  [        [        R                  " UR                  SS/5      5      S:  a  [	        S5      e[        UR                  SS UR                  SS 5       VVs/ s H5  u  p#[        U R                  R                  UR                   X# 5      5      PM7     snn$ [        R                  " USS/5      n[        U5      S:  a  [	        SR                  U5      5      eU Vs/ s H'  n[        U R                  R#                  U5      5      PM)     sn$ s  snnf s  snf )aP  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_original : list of tuples
    The set of labels for each sample such that `y[i]` consists of
    `classes_[j]` for each `yt[i, j] == 1`.
rf   z/Expected indicator for {0} classes, but got {1}r   z+Expected only 0s and 1s in label indicator.Nr   z8Expected only 0s and 1s in label indicator. Also got {0})r   rB   r   r&   rC   r   rv   rw   r   r   r   r@   r  r   tuplerE   r   compress)r(   r  startend
unexpected
indicatorss         r*   rG   %MultiLabelBinarizer.inverse_transform  s    	88A;#dmm,,AHH&  ;;r??B277|q Sbgg1v)F%G!%K !NOO #&biinbiim"D"DJE dmm((E)>?@"D 
 b1a&1J:" NUU" 
 QSSPR*E$--00<=PRSS Ts   <G.Gc                 h   > [         TU ]  5       nSUR                  l        SUR                  l        U$ r   )rJ   rK   rM   rN   rO   two_d_labelsrQ   s     r*   rK   $MultiLabelBinarizer.__sklearn_tags__!  r   r-   )r   r   r&   rd   )rU   rV   rW   rX   rY   re   r   r   ri   r   r+   r1   r9   r  r   rG   rK   rZ   r[   r\   s   @r*   r   r     s    <~ !$'#$D 
 #'e + 5 6@ 5) 6)V4!&
P'TR r-   r   rh   )2r   r   r  collectionsr   numbersr   numpyr   scipy.sparsesparserv   sklearn.baser   r   r   sklearn.utilsr   sklearn.utils._array_apir	   r
   r   r   r   r   r   r   r   sklearn.utils._encoder   r   sklearn.utils._param_validationr   r   sklearn.utils.multiclassr   r   sklearn.utils.sparsefuncsr   sklearn.utils.validationr   r   r   __all__r   r   r   r   r   r    r-   r*   <module>r2     s       #    F F &
 
 
 3 E B 2 O OM#]$ M`V%}D Vr O, >xtIFGxtIFG# #'	 -.% W	Wt, ^4LnJ*MQU Jr-   