normalize#
- sklearn.preprocessing.normalize(X, norm='l2', *, axis=1, copy=True, return_norm=False)[source]#
将输入向量单独归一化为单位范数(向量长度)。
在用户指南中了解更多信息。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
要归一化的数据,元素按元素处理。scipy.sparse 矩阵应采用 CSR 格式,以避免不必要的复制。
- norm{‘l1’, ‘l2’, ‘max’}, default=’l2’
用于归一化每个非零样本(如果 axis 为 0,则归一化每个非零特征)的范数。
- axis{0, 1}, default=1
定义用于沿其归一化数据的轴。如果为 1,则独立归一化每个样本;否则(如果为 0),则归一化每个特征。
- copy布尔值, 默认为 True
如果为 False,则尝试避免复制并就地归一化。这不保证始终就地工作;例如,如果数据是具有 int dtype 的 numpy 数组,即使 copy=False,也会返回一个副本。
- return_normbool, default=False
是否返回计算出的范数。
- 返回:
- X{ndarray, sparse matrix} of shape (n_samples, n_features)
归一化的输入 X。
- normsndarray of shape (n_samples, ) if axis=1 else (n_features, )
X 沿给定轴的范数数组。当 X 为稀疏矩阵时,对于范数 ‘l1’ 或 ‘l2’ 将引发 NotImplementedError。
另请参阅
Normalizer使用 Transformer API 执行归一化(例如,作为预处理
Pipeline的一部分)。
注意事项
有关不同缩放器、变换器和归一器的比较,请参阅:比较不同缩放器对异常值数据的效果。
示例
>>> from sklearn.preprocessing import normalize >>> X = [[-2, 1, 2], [-1, 0, 1]] >>> normalize(X, norm="l1") # L1 normalization each row independently array([[-0.4, 0.2, 0.4], [-0.5, 0. , 0.5]]) >>> normalize(X, norm="l2") # L2 normalization each row independently array([[-0.67, 0.33, 0.67], [-0.71, 0. , 0.71]])