ó
‡ˆ\c           @   s»  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l j Z	 d d l
 m Z m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z e j j Z e j j Z d d d d g Z d e d „ Z  d e d „ Z! d e d „ Z" e d „ Z# d e e f d „  ƒ  YZ$ d e e f d „  ƒ  YZ% d d e d „ Z& d „  Z' d „  Z( d e e f d „  ƒ  YZ) d S(   iÿÿÿÿ(   t   defaultdictNi   (   t   BaseEstimatort   TransformerMixin(   t   min_max_axis(   t   column_or_1d(   t   check_array(   t   check_is_fitted(   t   _num_samples(   t   unique_labels(   t   type_of_target(   t   sixt   label_binarizet   LabelBinarizert   LabelEncodert   MultiLabelBinarizerc         C   sŸ   | d  k rG | r7 t j |  d t ƒ\ } } | | f St j |  ƒ Sn  | r— t |  | ƒ } | r{ t d t | ƒ ƒ ‚ n  t j | |  ƒ } | | f S| Sd  S(   Nt   return_inverses'   y contains previously unseen labels: %s(   t   Nonet   npt   uniquet   Truet   _encode_check_unknownt
   ValueErrort   strt   searchsorted(   t   valuest   uniquest   encodet   encodedt   diff(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   _encode_numpy(   s    

c         C   s½   | d  k r9 t t |  ƒ ƒ } t j | d |  j ƒ} n  | rµ d „  t | ƒ Dƒ } y* t j g  |  D] } | | ^ qe ƒ } Wn) t k
 rª } t d t	 | ƒ ƒ ‚ n X| | f S| Sd  S(   Nt   dtypec         S   s   i  |  ] \ } } | | “ q S(    (    (   t   .0t   it   val(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pys
   <dictcomp>B   s   	 s'   y contains previously unseen labels: %s(
   R   t   sortedt   setR   t   arrayR   t	   enumeratet   KeyErrorR   R   (   R   R   R   t   tablet   vR   t   e(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   _encode_python<   s    *
c         C   s3   |  j  t k r t |  | | ƒ St |  | | ƒ Sd S(   s  Helper function to factorize (find uniques) and encode values.

    Uses pure python method for object dtype, and numpy method for
    all other dtypes.
    The numpy method has the limitation that the `uniques` need to
    be sorted. Importantly, this is not checked but assumed to already be
    the case. The calling method needs to ensure this for all non-object
    values.

    Parameters
    ----------
    values : array
        Values to factorize or encode.
    uniques : array, optional
        If passed, uniques are not determined from passed values (this
        can be because the user specified categories, or because they
        already have been determined in fit).
    encode : bool, default False
        If True, also encode the values into integer codes based on `uniques`.

    Returns
    -------
    uniques
        If ``encode=False``. The unique values are sorted if the `uniques`
        parameter was None (and thus inferred from the data).
    (uniques, encoded)
        If ``encode=True``.

    N(   R   t   objectR*   R   (   R   R   R   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   _encodeM   s    c         C   s  |  j  t k r” t | ƒ } t t |  ƒ | ƒ } | r | rh t j g  |  D] } | | k ^ qJ ƒ } n t j t |  ƒ d t ƒ} | | f S| Snw t j	 |  ƒ } t t j
 | | d t ƒƒ } | r| râ t j |  | ƒ } n t j t |  ƒ d t ƒ} | | f S| Sd S(   s—  
    Helper function to check for unknowns in values to be encoded.

    Uses pure python method for object dtype, and numpy method for
    all other dtypes.

    Parameters
    ----------
    values : array
        Values to check for unknowns.
    uniques : array
        Allowed uniques values.
    return_mask : bool, default False
        If True, return a mask of the same shape as `values` indicating
        the valid values.

    Returns
    -------
    diff : list
        The unique values present in `values` and not in `uniques` (the
        unknown values).
    valid_mask : boolean array
        Additionally returned if ``return_mask=True``.

    R   t   assume_uniqueN(   R   R+   R#   t   listR   R$   t   onest   lent   boolR   t	   setdiff1dR   t   in1d(   R   R   t   return_maskt   uniques_setR   R!   t
   valid_maskt   unique_values(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR   q   s"    +

c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sô  Encode labels with value between 0 and n_classes-1.

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

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

    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]) #doctest: +ELLIPSIS
    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"]) #doctest: +ELLIPSIS
    array([2, 2, 1]...)
    >>> list(le.inverse_transform([2, 2, 1]))
    ['tokyo', 'tokyo', 'paris']

    See also
    --------
    sklearn.preprocessing.OrdinalEncoder : encode categorical features
        using a one-hot or ordinal encoding scheme.
    c         C   s%   t  | d t ƒ} t | ƒ |  _ |  S(   sÖ   Fit label encoder

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

        Returns
        -------
        self : returns an instance of self.
        t   warn(   R   R   R,   t   classes_(   t   selft   y(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   fitÏ   s    c         C   s1   t  | d t ƒ} t | d t ƒ\ |  _ } | S(   sï   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]
        R8   R   (   R   R   R,   R9   (   R:   R;   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   fit_transformß   s    c         C   sc   t  |  d ƒ t | d t ƒ} t | ƒ d k r> t j g  ƒ St | d |  j d t ƒ\ } } | S(   sì   Transform labels to normalized encoding.

        Parameters
        ----------
        y : array-like of shape [n_samples]
            Target values.

        Returns
        -------
        y : array-like of shape [n_samples]
        R9   R8   i    R   R   (   R   R   R   R   R   R$   R,   R9   (   R:   R;   t   _(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt	   transformï   s    !c         C   s¡   t  |  d ƒ t | d t ƒ} t | ƒ d k r> t j g  ƒ St j | t j t |  j	 ƒ ƒ ƒ } t | ƒ r‡ t
 d t | ƒ ƒ ‚ n  t j | ƒ } |  j	 | S(   sñ   Transform labels back to original encoding.

        Parameters
        ----------
        y : numpy array of shape [n_samples]
            Target values.

        Returns
        -------
        y : numpy array of shape [n_samples]
        R9   R8   i    s'   y contains previously unseen labels: %s(   R   R   R   R   R   R$   R2   t   arangeR0   R9   R   R   t   asarray(   R:   R;   R   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   inverse_transform  s    $(   t   __name__t
   __module__t   __doc__R<   R=   R?   RB   (    (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR   £   s
   *			c           B   sG   e  Z d  Z d d e d „ Z d „  Z d „  Z d „  Z d d „ Z	 RS(	   s0  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 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 : boolean (default: False)
        True if the returned array from transform is desired to be in sparse
        CSR format.

    Attributes
    ----------

    classes_ : array of shape [n_class]
        Holds the label for each class.

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

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

    Examples
    --------
    >>> from sklearn import preprocessing
    >>> lb = preprocessing.LabelBinarizer()
    >>> lb.fit([1, 2, 6, 4, 2])
    LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False)
    >>> 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(neg_label=0, pos_label=1, sparse_output=False)
    >>> 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]])

    See also
    --------
    label_binarize : function to perform the transform operation of
        LabelBinarizer with fixed classes.
    sklearn.preprocessing.OneHotEncoder : encode categorical features
        using a one-hot aka one-of-K scheme.
    i    i   c         C   s   | | k r' t  d j | | ƒ ƒ ‚ n  | r` | d k sE | d k r` t  d j | | ƒ ƒ ‚ n  | |  _ | |  _ | |  _ d  S(   Ns7   neg_label={0} must be strictly less than pos_label={1}.i    su   Sparse binarization is only supported with non zero pos_label and zero neg_label, got pos_label={0} and neg_label={1}(   R   t   formatt	   neg_labelt	   pos_labelt   sparse_output(   R:   RG   RH   RI   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   __init__y  s    				c         C   sw   t  | ƒ |  _ d |  j k r- t d ƒ ‚ n  t | ƒ d k rR t d | ƒ ‚ n  t j | ƒ |  _ t | ƒ |  _ |  S(   sK  Fit label binarizer

        Parameters
        ----------
        y : array 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 : returns an instance of self.
        t   multioutputs@   Multioutput target data is not supported with label binarizationi    s   y has 0 samples: %r(	   R	   t   y_type_R   R   t   spt   issparset   sparse_input_R   R9   (   R:   R;   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR<   ˆ  s    c         C   s   |  j  | ƒ j | ƒ S(   s”  Fit label binarizer and transform multi-class labels to binary
        labels.

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

        Parameters
        ----------
        y : array or 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 : array or CSR matrix of shape [n_samples, n_classes]
            Shape will be [n_samples, 1] for binary problems.
        (   R<   R?   (   R:   R;   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR=      s    c      	   C   su   t  |  d ƒ t | ƒ j d ƒ } | rJ |  j j d ƒ rJ t d ƒ ‚ n  t | |  j d |  j d |  j d |  j	 ƒS(   s†  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 or 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 : numpy array or CSR matrix of shape [n_samples, n_classes]
            Shape will be [n_samples, 1] for binary problems.
        R9   t
   multilabels0   The object was not fitted with multilabel input.RH   RG   RI   (
   R   R	   t
   startswithRL   R   R   R9   RH   RG   RI   (   R:   R;   t   y_is_multilabel(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR?   ¶  s    		c         C   s¬   t  |  d ƒ | d k r0 |  j |  j d } n  |  j d k rT t | |  j ƒ } n t | |  j |  j | ƒ } |  j rŠ t	 j
 | ƒ } n t	 j | ƒ r¨ | j ƒ  } n  | S(   sB  Transform binary labels back to multi-class labels

        Parameters
        ----------
        Y : numpy array or sparse matrix with shape [n_samples, n_classes]
            Target values. All sparse matrices are converted to CSR before
            inverse transformation.

        threshold : float or None
            Threshold used in the binary and multi-label cases.

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

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

        Returns
        -------
        y : numpy array or CSR matrix of shape [n_samples] Target values.

        Notes
        -----
        In the case when the binary labels are fractional
        (probabilistic), inverse_transform chooses the class with the
        greatest value. Typically, this allows to use the output of a
        linear model's decision_function method directly as the input
        of inverse_transform.
        R9   g       @t
   multiclassN(   R   R   RH   RG   RL   t   _inverse_binarize_multiclassR9   t   _inverse_binarize_thresholdingRO   RM   t
   csr_matrixRN   t   toarray(   R:   t   Yt	   thresholdt   y_inv(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyRB   Õ  s    	N(
   RC   RD   RE   t   FalseRJ   R<   R=   R?   R   RB   (    (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR     s   Y			i    i   c         C   sE  t  |  t ƒ s0 t |  d d d t d d ƒ}  n% t |  ƒ d k rU t d |  ƒ ‚ n  | | k r| t d j | | ƒ ƒ ‚ n  | rµ | d k sš | d k rµ t d j | | ƒ ƒ ‚ n  | d k } | rÑ | } n  t |  ƒ } d	 | k rø t d
 ƒ ‚ n  | d k rt d ƒ ‚ n  t	 j
 |  ƒ r/|  j d n	 t |  ƒ } t | ƒ } t j | ƒ } | d k rà| d k rÂ| rt	 j | d f d t ƒSt j t |  ƒ d f d t j ƒ}	 |	 | 7}	 |	 Sqàt | ƒ d k ràd } qàn  t j | ƒ }
 | d k r2| j |  j d k r2t d j | t |  ƒ ƒ ƒ ‚ n  | d k rÙt |  ƒ }  t j |  | ƒ } |  | } t j |
 | ƒ } t j d t j | ƒ f ƒ } t j | ƒ } | j | ƒ t	 j | | | f d | | f ƒ}	 ne | d k r.t	 j |  ƒ }	 | d k r>t j |	 j ƒ } | j | ƒ | |	 _ q>n t d | ƒ ‚ | s |	 j ƒ  }	 |	 j t d t ƒ}	 | d k r„| |	 |	 d k <n  | r»d |	 |	 | k <q»n |	 j j t d t ƒ|	 _ t j | |
 k ƒ rût j |
 | ƒ } |	 d d … | f }	 n  | d k rA| r|	 j  d ƒ }	 qA|	 d d … d f j! d ƒ }	 n  |	 S(   sæ  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 : boolean (default: False),
        Set to true if output binary array is desired in CSR sparse format

    Returns
    -------
    Y : numpy array or CSR matrix of shape [n_samples, n_classes]
        Shape will be [n_samples, 1] for binary problems.

    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]])

    See also
    --------
    LabelBinarizer : class used to wrap the functionality of label_binarize and
        allow for fitting to classes independently of the transform operation
    t   accept_sparset   csrt	   ensure_2dR   i    s   y has 0 samples: %rs7   neg_label={0} must be strictly less than pos_label={1}.su   Sparse binarization is only supported with non zero pos_label and zero neg_label, got pos_label={0} and neg_label={1}RK   s@   Multioutput target data is not supported with label binarizationt   unknowns$   The type of target data is not knownt   binaryi   i   RS   s   multilabel-indicators:   classes {0} missmatch with the labels {1}found in the datat   shapes7   %s target data is not supported with label binarizationt   copyNiÿÿÿÿ(   R`   RS   (   iÿÿÿÿi   ("   t
   isinstanceR.   R   R[   R   R   R   RF   R	   RM   RN   Ra   R0   R   RA   RV   t   intt   zerost   sortt   sizeR   R   R3   R   t   hstackt   cumsumt
   empty_liket   fillt   dataRW   t   astypet   anyt   getcolt   reshape(   R;   t   classesRG   RH   RI   t
   pos_switcht   y_typet	   n_samplest	   n_classesRX   t   sorted_classt   y_in_classest   y_seent   indicest   indptrRl   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR     sˆ    ;!		
($
"	

"c         C   sÉ  t  j | ƒ } t j |  ƒ r¦|  j ƒ  }  |  j \ } } t  j | ƒ } t |  d ƒ d } t  j |  j	 ƒ } t  j
 | | ƒ } t  j | |  j k ƒ } | d d k rÈ t  j | t |  j ƒ g ƒ } n  t  j | |  j	 d  ƒ }	 t  j |  j d g ƒ }
 |
 | |	 } d | t  j | d k ƒ d <t  j | ƒ | d k | j ƒ  d k @} xN | D]F } |  j |  j	 | |  j	 | d !} | t  j | | ƒ d | | <qTW| | S| j |  j d d ƒ d d ƒSd S(   s}   Inverse label binarization transformation for multiclass.

    Multiclass uses the maximal score instead of a threshold.
    i   iÿÿÿÿi    t   axist   modet   clipN(   R   RA   RM   RN   t   tocsrRa   R@   R   R   Rz   t   repeatt   flatnonzeroRl   t   appendR0   R   Ry   t   wheret   ravelR2   t   taket   argmax(   R;   Rq   Rt   t	   n_outputst   outputst   row_maxt   row_nnzt   y_data_repeated_maxt   y_i_all_argmaxt   index_first_argmaxt	   y_ind_extt
   y_i_argmaxt   samplesR    t   ind(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyRT   ¤  s,    !""c         C   s  | d k rI |  j  d k rI |  j d d k rI t d j |  j ƒ ƒ ‚ n  | d k r} |  j d t | ƒ k r} t d ƒ ‚ n  t j | ƒ } t j |  ƒ r| d k rö |  j d k rÅ |  j	 ƒ  }  n  t j
 |  j | k d	 t j ƒ|  _ |  j ƒ  q;t j
 |  j ƒ  | k d	 t j ƒ}  n t j
 |  | k d	 t j ƒ}  | d k rÜt j |  ƒ re|  j ƒ  }  n  |  j  d k rŸ|  j d d k rŸ| |  d
 d
 … d f St | ƒ d k rËt j | d t |  ƒ ƒ S| |  j ƒ  Sn% | d k rì|  St d j | ƒ ƒ ‚ d
 S(   s=   Inverse label binarization transformation using thresholding.R`   i   i   s'   output_type='binary', but y.shape = {0}sA   The number of class is not equal to the number of dimension of y.i    R]   t   cscR   Ns   multilabel-indicators   {0} format is not supported(   R]   R‘   (   t   ndimRa   R   RF   R0   R   RA   RM   RN   R~   R$   Rl   Rd   t   eliminate_zerosRW   R   Rƒ   (   R;   t   output_typeRq   RY   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyRU   Ð  s2    .	%$'"c           B   sJ   e  Z d  Z d e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   s¼  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] (optional)
        Indicates an ordering for the class labels.
        All entries should be unique (cannot contain duplicate classes).

    sparse_output : boolean (default: False),
        Set to true if output binary array is desired in CSR sparse format

    Attributes
    ----------
    classes_ : array of labels
        A copy of the `classes` parameter where provided,
        or otherwise, the sorted set of classes found when fitting.

    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([set(['sci-fi', 'thriller']), set(['comedy'])])
    array([[0, 1, 1],
           [1, 0, 0]])
    >>> list(mlb.classes_)
    ['comedy', 'sci-fi', 'thriller']

    See also
    --------
    sklearn.preprocessing.OneHotEncoder : encode categorical features
        using a one-hot aka one-of-K scheme.
    c         C   s   | |  _  | |  _ d  S(   N(   Rq   RI   (   R:   Rq   RI   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyRJ   )  s    	c         C   s½   |  j  d k r0 t t t j j | ƒ ƒ ƒ } n< t t |  j  ƒ ƒ t |  j  ƒ k  rc t d ƒ ‚ n	 |  j  } t	 d „  | Dƒ ƒ r‹ t
 j n t } t
 j t | ƒ d | ƒ|  _ | |  j (|  S(   s‚  Fit the label sets binarizer, storing `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 : returns this MultiLabelBinarizer instance
        st   The classes argument contains duplicate classes. Remove these duplicates before passing them to MultiLabelBinarizer.c         s   s   |  ] } t  | t ƒ Vq d  S(   N(   Rc   Rd   (   R   t   c(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pys	   <genexpr>C  s    R   N(   Rq   R   R"   R#   t	   itertoolst   chaint   from_iterableR0   R   t   allR   Rd   R+   t   emptyR9   (   R:   R;   Rq   R   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR<   -  s    !$	%
c         C   s  |  j  d k	 r% |  j | ƒ j | ƒ St t ƒ } | j | _ |  j | | ƒ } t	 | d | j
 ƒ} t d „  | Dƒ ƒ rƒ t j n t } t j t | ƒ d | ƒ} | | (t j | d t ƒ\ |  _ } t j | | j d | j j d t ƒ| _ |  j s| j ƒ  } n  | S(   s  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 : 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.
        t   keyc         s   s   |  ] } t  | t ƒ Vq d  S(   N(   Rc   Rd   (   R   R•   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pys	   <genexpr>d  s    R   R   Rb   N(   Rq   R   R<   R?   R    Rd   t   __len__t   default_factoryt
   _transformR"   t   getR™   R   R+   Rš   R0   R   R   R9   R$   Ry   R   R[   RI   RW   (   R:   R;   t   class_mappingt   ytt   tmpR   t   inverse(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR=   H  s    %	c         C   sb   t  |  d ƒ t t |  j t t |  j ƒ ƒ ƒ ƒ } |  j | | ƒ } |  j s^ | j ƒ  } n  | S(   só  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.
        R9   (	   R   t   dictt   zipR9   t   rangeR0   Rž   RI   RW   (   R:   R;   t   class_to_indexR¡   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR?   q  s    '	c   
      C   s&  t  j  d ƒ } t  j  d d g ƒ } t ƒ  } x~ | D]v } t ƒ  } xD | D]< } y | j | | ƒ WqJ t k
 r… | j | ƒ qJ XqJ W| j | ƒ | j t | ƒ ƒ q4 W| rÙ t j d j	 t
 | d t ƒƒ ƒ n  t j t | ƒ d t ƒ}	 t j |	 | | f d t | ƒ d t | ƒ f ƒS(   sp  Transforms the label sets with a given mapping

        Parameters
        ----------
        y : iterable of iterables
        class_mapping : Mapping
            Maps from label to column index in label indicator matrix

        Returns
        -------
        y_indicator : sparse CSR matrix, shape (n_samples, n_classes)
            Label indicator matrix
        R    i    s%   unknown class(es) {0} will be ignoredR›   R   Ra   i   (   R$   R#   t   addR&   t   extendR   R0   t   warningsR8   RF   R"   R   R   R/   Rd   RM   RV   (
   R:   R;   R    Ry   Rz   R_   t   labelst   indext   labelRl   (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyRž   ‹  s$    		c         C   sy  t  |  d ƒ | j d t |  j ƒ k rT t d j t |  j ƒ | j d ƒ ƒ ‚ n  t j | ƒ r
| j ƒ  } t | j	 ƒ d k rº t t
 j | j	 d d g ƒ ƒ d k rº t d ƒ ‚ n  g  t | j d  | j d ƒ D]. \ } } t |  j j | j | | !ƒ ƒ ^ qØ St
 j | d d g ƒ } t | ƒ d k rLt d j | ƒ ƒ ‚ n  g  | D] } t |  j j | ƒ ƒ ^ qSSd S(	   s”  Transform the given indicator matrix into label sets

        Parameters
        ----------
        yt : array or 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`.
        R9   i   s/   Expected indicator for {0} classes, but got {1}i    s+   Expected only 0s and 1s in label indicator.iÿÿÿÿs8   Expected only 0s and 1s in label indicator. Also got {0}N(   R   Ra   R0   R9   R   RF   RM   RN   R~   Rl   R   R2   R¥   Rz   t   tupleR„   Ry   t   compress(   R:   R¡   t   startt   endt
   unexpectedt
   indicators(    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyRB   ­  s     	"<M	N(   RC   RD   RE   R   R[   RJ   R<   R=   R?   Rž   RB   (    (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyR   ü  s   +		)		"(*   t   collectionsR    R–   R$   Rª   t   numpyR   t   scipy.sparset   sparseRM   t   baseR   R   t   utils.sparsefuncsR   t   utilsR   t   utils.validationR   R   R   t   utils.multiclassR   R	   t	   externalsR
   t   movesR¥   t   mapt   __all__R   R[   R   R*   R,   R   R   R   R   RT   RU   R   (    (    (    s:   lib/python2.7/site-packages/sklearn/preprocessing/label.pyt   <module>	   s<   	$2{é	,	,