#NEWLINE# def normalize( X , norm = 'l2' , axis = 1 , copy = True , return_norm = False ) : #NEWLINE# #INDENT# "Scale input vectors individually to unit norm (vector length).\n\n    Read more in the :ref:`User Guide <preprocessing_normalization>`.\n\n    Parameters\n    ----------\n    X : {array-like, sparse matrix}, shape [n_samples, n_features]\n        The data to normalize, element by element.\n        scipy.sparse matrices should be in CSR format to avoid an\n        un-necessary copy.\n\n    norm : 'l1', 'l2', or 'max', optional ('l2' by default)\n        The norm to use to normalize each non zero sample (or each non-zero\n        feature if axis is 0).\n\n    axis : 0 or 1, optional (1 by default)\n        axis used to normalize the data along. If 1, independently normalize\n        each sample, otherwise (if 0) normalize each feature.\n\n    copy : boolean, optional, default True\n        set to False to perform inplace row normalization and avoid a\n        copy (if the input is already a numpy array or a scipy.sparse\n        CSR matrix and if axis is 1).\n\n    return_norm : boolean, default False\n        whether to return the computed norms\n\n    See also\n    --------\n    :class:`sklearn.preprocessing.Normalizer` to perform normalization\n    using the ``Transformer`` API (e.g. as part of a preprocessing\n    :class:`sklearn.pipeline.Pipeline`)\n    " #NEWLINE# if ( norm not in ( 'l1' , 'l2' , 'max' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( "'%s' is not a supported norm" % norm ) ) #NEWLINE# #UNINDENT# if ( axis == 0 ) : #NEWLINE# #INDENT# sparse_format = 'csc' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( axis == 1 ) : #NEWLINE# #INDENT# sparse_format = 'csr' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( "'%d' is not a supported axis" % norm ) ) #NEWLINE# #UNINDENT# #UNINDENT# X = check_array ( X , sparse_format , copy = copy , warn_on_dtype = True , estimator = 'the normalize function' , dtype = FLOAT_DTYPES ) #NEWLINE# if ( axis == 0 ) : #NEWLINE# #INDENT# X = X . T #NEWLINE# #UNINDENT# if sparse . issparse ( X ) : #NEWLINE# #INDENT# if ( norm == 'l1' ) : #NEWLINE# #INDENT# inplace_csr_row_normalize_l1 ( X ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( norm == 'l2' ) : #NEWLINE# #INDENT# inplace_csr_row_normalize_l2 ( X ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( norm == 'max' ) : #NEWLINE# #INDENT# ( _ , norms ) = min_max_axis ( X , 1 ) #NEWLINE# norms = norms . repeat ( np . diff ( X . indptr ) ) #NEWLINE# mask = ( norms != 0 ) #NEWLINE# X . data [ mask ] /= norms [ mask ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( norm == 'l1' ) : #NEWLINE# #INDENT# norms = np . abs ( X ) . sum ( axis = 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( norm == 'l2' ) : #NEWLINE# #INDENT# norms = row_norms ( X ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( norm == 'max' ) : #NEWLINE# #INDENT# norms = np . max ( X , axis = 1 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# norms = _handle_zeros_in_scale ( norms , copy = False ) #NEWLINE# X /= norms [ : , np . newaxis ] #NEWLINE# #UNINDENT# if ( axis == 0 ) : #NEWLINE# #INDENT# X = X . T #NEWLINE# #UNINDENT# if return_norm : #NEWLINE# #INDENT# return ( X , norms ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return X