Scale input vectors individually to unit norm (vector length). Read more in the :ref:`User Guide `. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) The data to normalize, element by element. scipy.s
(X, norm="l2", *, axis=1, copy=True, return_norm=False)
| 1976 | prefer_skip_nested_validation=True, |
| 1977 | ) |
| 1978 | def normalize(X, norm="l2", *, axis=1, copy=True, return_norm=False): |
| 1979 | """Scale input vectors individually to unit norm (vector length). |
| 1980 | |
| 1981 | Read more in the :ref:`User Guide <preprocessing_normalization>`. |
| 1982 | |
| 1983 | Parameters |
| 1984 | ---------- |
| 1985 | X : {array-like, sparse matrix} of shape (n_samples, n_features) |
| 1986 | The data to normalize, element by element. |
| 1987 | scipy.sparse matrices should be in CSR format to avoid an |
| 1988 | un-necessary copy. |
| 1989 | |
| 1990 | norm : {'l1', 'l2', 'max'}, default='l2' |
| 1991 | The norm to use to normalize each non zero sample (or each non-zero |
| 1992 | feature if axis is 0). |
| 1993 | |
| 1994 | axis : {0, 1}, default=1 |
| 1995 | Define axis used to normalize the data along. If 1, independently |
| 1996 | normalize each sample, otherwise (if 0) normalize each feature. |
| 1997 | |
| 1998 | copy : bool, default=True |
| 1999 | If False, try to avoid a copy and normalize in place. |
| 2000 | This is not guaranteed to always work in place; e.g. if the data is |
| 2001 | a numpy array with an int dtype, a copy will be returned even with |
| 2002 | copy=False. |
| 2003 | |
| 2004 | return_norm : bool, default=False |
| 2005 | Whether to return the computed norms. |
| 2006 | |
| 2007 | Returns |
| 2008 | ------- |
| 2009 | X : {ndarray, sparse matrix} of shape (n_samples, n_features) |
| 2010 | Normalized input X. |
| 2011 | |
| 2012 | norms : ndarray of shape (n_samples, ) if axis=1 else (n_features, ) |
| 2013 | An array of norms along given axis for X. |
| 2014 | When X is sparse, a NotImplementedError will be raised |
| 2015 | for norm 'l1' or 'l2'. |
| 2016 | |
| 2017 | See Also |
| 2018 | -------- |
| 2019 | Normalizer : Performs normalization using the Transformer API |
| 2020 | (e.g. as part of a preprocessing :class:`~sklearn.pipeline.Pipeline`). |
| 2021 | |
| 2022 | Notes |
| 2023 | ----- |
| 2024 | For a comparison of the different scalers, transformers, and normalizers, |
| 2025 | see: :ref:`sphx_glr_auto_examples_preprocessing_plot_all_scaling.py`. |
| 2026 | |
| 2027 | Examples |
| 2028 | -------- |
| 2029 | >>> from sklearn.preprocessing import normalize |
| 2030 | >>> X = [[-2, 1, 2], [-1, 0, 1]] |
| 2031 | >>> normalize(X, norm="l1") # L1 normalization each row independently |
| 2032 | array([[-0.4, 0.2, 0.4], |
| 2033 | [-0.5, 0. , 0.5]]) |
| 2034 | >>> normalize(X, norm="l2") # L2 normalization each row independently |
| 2035 | array([[-0.67, 0.33, 0.67], |
searching dependent graphs…