<s> from collections import defaultdict <EOL> import itertools <EOL> import array <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> from . . base import BaseEstimator , TransformerMixin <EOL> from . . utils . fixes import np_version <EOL> from . . utils . fixes import sparse_min_max <EOL> from . . utils . fixes import astype <EOL> from . . utils . fixes import in1d <EOL> from . . utils import column_or_1d <EOL> from . . utils . validation import check_array <EOL> from . . utils . validation import check_is_fitted <EOL> from . . utils . validation import _num_samples <EOL> from . . utils . multiclass import unique_labels <EOL> from . . utils . multiclass import type_of_target <EOL> from . . externals import six <EOL> zip = six . moves . zip <EOL> map = six . moves . map <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def _check_numpy_unicode_bug ( labels ) : <EOL> """<STR_LIT>""" <EOL> if np_version [ : <NUM_LIT:3> ] < ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT:0> ) and labels . dtype . kind == '<STR_LIT>' : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class LabelEncoder ( BaseEstimator , TransformerMixin ) : <EOL> """<STR_LIT>""" <EOL> def fit ( self , y ) : <EOL> """<STR_LIT>""" <EOL> y = column_or_1d ( y , warn = True ) <EOL> _check_numpy_unicode_bug ( y ) <EOL> self . classes_ = np . unique ( y ) <EOL> return self <EOL> def fit_transform ( self , y ) : <EOL> """<STR_LIT>""" <EOL> y = column_or_1d ( y , warn = True ) <EOL> _check_numpy_unicode_bug ( y ) <EOL> self . classes_ , y = np . unique ( y , return_inverse = True ) <EOL> return y <EOL> def transform ( self , y ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> y = column_or_1d ( y , warn = True ) <EOL> classes = np . unique ( y ) <EOL> _check_numpy_unicode_bug ( classes ) <EOL> if len ( np . intersect1d ( classes , self . classes_ ) ) < len ( classes ) : <EOL> diff = np . setdiff1d ( classes , self . classes_ ) <EOL> raise ValueError ( "<STR_LIT>" % str ( diff ) ) <EOL> return np . searchsorted ( self . classes_ , y ) <EOL> def inverse_transform ( self , y ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> diff = np . setdiff1d ( y , np . arange ( len ( self . classes_ ) ) ) <EOL> if diff : <EOL> raise ValueError ( "<STR_LIT>" % str ( diff ) ) <EOL> y = np . asarray ( y ) <EOL> return self . classes_ [ y ] <EOL> class LabelBinarizer ( BaseEstimator , TransformerMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , neg_label = <NUM_LIT:0> , pos_label = <NUM_LIT:1> , sparse_output = False ) : <EOL> if neg_label >= pos_label : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( neg_label , pos_label ) ) <EOL> if sparse_output and ( pos_label == <NUM_LIT:0> or neg_label != <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( pos_label , neg_label ) ) <EOL> self . neg_label = neg_label <EOL> self . pos_label = pos_label <EOL> self . sparse_output = sparse_output <EOL> def fit ( self , y ) : <EOL> """<STR_LIT>""" <EOL> self . y_type_ = type_of_target ( y ) <EOL> if '<STR_LIT>' in self . y_type_ : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if _num_samples ( y ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' % y ) <EOL> self . sparse_input_ = sp . issparse ( y ) <EOL> self . classes_ = unique_labels ( y ) <EOL> return self <EOL> def transform ( self , y ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> y_is_multilabel = type_of_target ( y ) . startswith ( '<STR_LIT>' ) <EOL> if y_is_multilabel and not self . y_type_ . startswith ( '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return label_binarize ( y , self . classes_ , <EOL> pos_label = self . pos_label , <EOL> neg_label = self . neg_label , <EOL> sparse_output = self . sparse_output ) <EOL> def inverse_transform ( self , Y , threshold = None ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> if threshold is None : <EOL> threshold = ( self . pos_label + self . neg_label ) / <NUM_LIT> <EOL> if self . y_type_ == "<STR_LIT>" : <EOL> y_inv = _inverse_binarize_multiclass ( Y , self . classes_ ) <EOL> else : <EOL> y_inv = _inverse_binarize_thresholding ( Y , self . y_type_ , <EOL> self . classes_ , threshold ) <EOL> if self . sparse_input_ : <EOL> y_inv = sp . csr_matrix ( y_inv ) <EOL> elif sp . issparse ( y_inv ) : <EOL> y_inv = y_inv . toarray ( ) <EOL> return y_inv <EOL> def label_binarize ( y , classes , neg_label = <NUM_LIT:0> , pos_label = <NUM_LIT:1> , sparse_output = False ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( y , list ) : <EOL> y = check_array ( y , accept_sparse = '<STR_LIT>' , ensure_2d = False , dtype = None ) <EOL> else : <EOL> if _num_samples ( y ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' % y ) <EOL> if neg_label >= pos_label : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( neg_label , pos_label ) ) <EOL> if ( sparse_output and ( pos_label == <NUM_LIT:0> or neg_label != <NUM_LIT:0> ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( pos_label , neg_label ) ) <EOL> pos_switch = pos_label == <NUM_LIT:0> <EOL> if pos_switch : <EOL> pos_label = - neg_label <EOL> y_type = type_of_target ( y ) <EOL> if '<STR_LIT>' in y_type : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if y_type == '<STR_LIT>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> n_samples = y . shape [ <NUM_LIT:0> ] if sp . issparse ( y ) else len ( y ) <EOL> n_classes = len ( classes ) <EOL> classes = np . asarray ( classes ) <EOL> if y_type == "<STR_LIT>" : <EOL> if n_classes == <NUM_LIT:1> : <EOL> if sparse_output : <EOL> return sp . csr_matrix ( ( n_samples , <NUM_LIT:1> ) , dtype = int ) <EOL> else : <EOL> Y = np . zeros ( ( len ( y ) , <NUM_LIT:1> ) , dtype = np . int ) <EOL> Y += neg_label <EOL> return Y <EOL> elif len ( classes ) >= <NUM_LIT:3> : <EOL> y_type = "<STR_LIT>" <EOL> sorted_class = np . sort ( classes ) <EOL> if ( y_type == "<STR_LIT>" and classes . size != y . shape [ <NUM_LIT:1> ] ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( classes , unique_labels ( y ) ) ) <EOL> if y_type in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> y = column_or_1d ( y ) <EOL> y_in_classes = in1d ( y , classes ) <EOL> y_seen = y [ y_in_classes ] <EOL> indices = np . searchsorted ( sorted_class , y_seen ) <EOL> indptr = np . hstack ( ( <NUM_LIT:0> , np . cumsum ( y_in_classes ) ) ) <EOL> data = np . empty_like ( indices ) <EOL> data . fill ( pos_label ) <EOL> Y = sp . csr_matrix ( ( data , indices , indptr ) , <EOL> shape = ( n_samples , n_classes ) ) <EOL> elif y_type == "<STR_LIT>" : <EOL> Y = sp . csr_matrix ( y ) <EOL> if pos_label != <NUM_LIT:1> : <EOL> data = np . empty_like ( Y . data ) <EOL> data . fill ( pos_label ) <EOL> Y . data = data <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % y_type ) <EOL> if not sparse_output : <EOL> Y = Y . toarray ( ) <EOL> Y = astype ( Y , int , copy = False ) <EOL> if neg_label != <NUM_LIT:0> : <EOL> Y [ Y == <NUM_LIT:0> ] = neg_label <EOL> if pos_switch : <EOL> Y [ Y == pos_label ] = <NUM_LIT:0> <EOL> else : <EOL> Y . data = astype ( Y . data , int , copy = False ) <EOL> if np . any ( classes != sorted_class ) : <EOL> indices = np . searchsorted ( sorted_class , classes ) <EOL> Y = Y [ : , indices ] <EOL> if y_type == "<STR_LIT>" : <EOL> if sparse_output : <EOL> Y = Y . getcol ( - <NUM_LIT:1> ) <EOL> else : <EOL> Y = Y [ : , - <NUM_LIT:1> ] . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> return Y <EOL> def _inverse_binarize_multiclass ( y , classes ) : <EOL> """<STR_LIT>""" <EOL> classes = np . asarray ( classes ) <EOL> if sp . issparse ( y ) : <EOL> y = y . tocsr ( ) <EOL> n_samples , n_outputs = y . shape <EOL> outputs = np . arange ( n_outputs ) <EOL> row_max = sparse_min_max ( y , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> row_nnz = np . diff ( y . indptr ) <EOL> y_data_repeated_max = np . repeat ( row_max , row_nnz ) <EOL> y_i_all_argmax = np . flatnonzero ( y_data_repeated_max == y . data ) <EOL> if row_max [ - <NUM_LIT:1> ] == <NUM_LIT:0> : <EOL> y_i_all_argmax = np . append ( y_i_all_argmax , [ len ( y . data ) ] ) <EOL> index_first_argmax = np . searchsorted ( y_i_all_argmax , y . indptr [ : - <NUM_LIT:1> ] ) <EOL> y_ind_ext = np . append ( y . indices , [ <NUM_LIT:0> ] ) <EOL> y_i_argmax = y_ind_ext [ y_i_all_argmax [ index_first_argmax ] ] <EOL> y_i_argmax [ np . where ( row_nnz == <NUM_LIT:0> ) [ <NUM_LIT:0> ] ] = <NUM_LIT:0> <EOL> samples = np . arange ( n_samples ) [ ( row_nnz > <NUM_LIT:0> ) & <EOL> ( row_max . ravel ( ) == <NUM_LIT:0> ) ] <EOL> for i in samples : <EOL> ind = y . indices [ y . indptr [ i ] : y . indptr [ i + <NUM_LIT:1> ] ] <EOL> y_i_argmax [ i ] = classes [ np . setdiff1d ( outputs , ind ) ] [ <NUM_LIT:0> ] <EOL> return classes [ y_i_argmax ] <EOL> else : <EOL> return classes . take ( y . argmax ( axis = <NUM_LIT:1> ) , mode = "<STR_LIT>" ) <EOL> def _inverse_binarize_thresholding ( y , output_type , classes , threshold ) : <EOL> """<STR_LIT>""" <EOL> if output_type == "<STR_LIT>" and y . ndim == <NUM_LIT:2> and y . shape [ <NUM_LIT:1> ] > <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" . <EOL> format ( y . shape ) ) <EOL> if output_type != "<STR_LIT>" and y . shape [ <NUM_LIT:1> ] != len ( classes ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> classes = np . asarray ( classes ) <EOL> if sp . issparse ( y ) : <EOL> if threshold > <NUM_LIT:0> : <EOL> if y . format not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> y = y . tocsr ( ) <EOL> y . data = np . array ( y . data > threshold , dtype = np . int ) <EOL> y . eliminate_zeros ( ) <EOL> else : <EOL> y = np . array ( y . toarray ( ) > threshold , dtype = np . int ) <EOL> else : <EOL> y = np . array ( y > threshold , dtype = np . int ) <EOL> if output_type == "<STR_LIT>" : <EOL> if sp . issparse ( y ) : <EOL> y = y . toarray ( ) <EOL> if y . ndim == <NUM_LIT:2> and y . shape [ <NUM_LIT:1> ] == <NUM_LIT:2> : <EOL> return classes [ y [ : , <NUM_LIT:1> ] ] <EOL> else : <EOL> if len ( classes ) == <NUM_LIT:1> : <EOL> return np . repeat ( classes [ <NUM_LIT:0> ] , len ( y ) ) <EOL> else : <EOL> return classes [ y . ravel ( ) ] <EOL> elif output_type == "<STR_LIT>" : <EOL> return y <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( output_type ) ) <EOL> class MultiLabelBinarizer ( BaseEstimator , TransformerMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , classes = None , sparse_output = False ) : <EOL> self . classes = classes <EOL> self . sparse_output = sparse_output <EOL> def fit ( self , y ) : <EOL> """<STR_LIT>""" <EOL> if self . classes is None : <EOL> classes = sorted ( set ( itertools . chain . from_iterable ( y ) ) ) <EOL> else : <EOL> classes = self . classes <EOL> dtype = np . int if all ( isinstance ( c , int ) for c in classes ) else object <EOL> self . classes_ = np . empty ( len ( classes ) , dtype = dtype ) <EOL> self . classes_ [ : ] = classes <EOL> return self <EOL> def fit_transform ( self , y ) : <EOL> """<STR_LIT>""" <EOL> if self . classes is not None : <EOL> return self . fit ( y ) . transform ( y ) <EOL> class_mapping = defaultdict ( int ) <EOL> class_mapping . default_factory = class_mapping . __len__ <EOL> yt = self . _transform ( y , class_mapping ) <EOL> tmp = sorted ( class_mapping , key = class_mapping . get ) <EOL> dtype = np . int if all ( isinstance ( c , int ) for c in tmp ) else object <EOL> class_mapping = np . empty ( len ( tmp ) , dtype = dtype ) <EOL> class_mapping [ : ] = tmp <EOL> self . classes_ , inverse = np . unique ( class_mapping , return_inverse = True ) <EOL> yt . indices = np . take ( inverse , yt . indices ) <EOL> if not self . sparse_output : <EOL> yt = yt . toarray ( ) <EOL> return yt <EOL> def transform ( self , y ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> class_to_index = dict ( zip ( self . classes_ , range ( len ( self . classes_ ) ) ) ) <EOL> yt = self . _transform ( y , class_to_index ) <EOL> if not self . sparse_output : <EOL> yt = yt . toarray ( ) <EOL> return yt <EOL> def _transform ( self , y , class_mapping ) : <EOL> """<STR_LIT>""" <EOL> indices = array . array ( '<STR_LIT:i>' ) <EOL> indptr = array . array ( '<STR_LIT:i>' , [ <NUM_LIT:0> ] ) <EOL> for labels in y : <EOL> indices . extend ( set ( class_mapping [ label ] for label in labels ) ) <EOL> indptr . append ( len ( indices ) ) <EOL> data = np . ones ( len ( indices ) , dtype = int ) <EOL> return sp . csr_matrix ( ( data , indices , indptr ) , <EOL> shape = ( len ( indptr ) - <NUM_LIT:1> , len ( class_mapping ) ) ) <EOL> def inverse_transform ( self , yt ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , '<STR_LIT>' ) <EOL> if yt . shape [ <NUM_LIT:1> ] != len ( self . classes_ ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( len ( self . classes_ ) , yt . shape [ <NUM_LIT:1> ] ) ) <EOL> if sp . issparse ( yt ) : <EOL> yt = yt . tocsr ( ) <EOL> if len ( yt . data ) != <NUM_LIT:0> and len ( np . setdiff1d ( yt . data , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) > <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return [ tuple ( self . classes_ . take ( yt . indices [ start : end ] ) ) <EOL> for start , end in zip ( yt . indptr [ : - <NUM_LIT:1> ] , yt . indptr [ <NUM_LIT:1> : ] ) ] <EOL> else : <EOL> unexpected = np . setdiff1d ( yt , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> if len ( unexpected ) > <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( unexpected ) ) <EOL> return [ tuple ( self . classes_ . compress ( indicators ) ) for indicators <EOL> in yt ] </s>
