【问题标题】:What type of normalization happens with sklearnsklearn 会发生什么类型的标准化
【发布时间】:2018-07-14 01:27:30
【问题描述】:

我有一个矩阵,我试图通过将每个特征列转换为零均值和单位标准差来对其进行归一化。

我有以下正在使用的代码,但我想知道该方法是否真的能达到我想要的效果,或者它是否使用了不同的方法。

from sklearn import preprocessing

mat_normalized = preprocessing.normalize(mat_from_df)

【问题讨论】:

  • 在使用任何东西之前一定要阅读文档。
  • 我明白了。有没有一种 scikit learn 方法可以实现我想要做的事情?
  • 是的。 preprocessing.scale
  • 谢谢!您可以将其发布为答案,以便我对其进行标记和投票吗?

标签: python pandas scikit-learn normalize


【解决方案1】:

sklearn.preprocessing.normalize 将每个样本向量缩放到单位范数。 (默认轴是 1,而不是 0。)以下是证明:

from sklearn.preprocessing import normalize

np.random.seed(444)
data = np.random.normal(loc=5, scale=2, size=(15, 2))
np.linalg.norm(normalize(data), axis=1)
# array([ 1.,  1.,  1.,  1.,  1.,  1., ...

听起来您正在寻找 sklearn.preprocessing.scale 将每个特征向量缩放到 ~N(0, 1)。

from sklearn.preprocessing import scale

# Are the scaled column-wise means approx. 0.?
np.allclose(scale(data).mean(axis=0), 0.)
# True

# Are the scaled column-wise stdevs. approx. 1.?
np.allclose(scale(data).std(axis=0), 1.)
# True

【讨论】:

    【解决方案2】:

    喜欢the documentation 状态:

    sklearn.preprocessing.normalize(X, norm='l2',
                                    axis=1, copy=True,
                                    return_norm=False)
    

    将输入向量单独缩放到单位范数(向量长度)。

    所以它采用范数(默认为 L2 范数),然后确保向量是单位。

    所以如果我们将一个n×m-矩阵作为输入,那么输出就是一个n×m-矩阵。每个 m 向量都是标准化的。对于norm='l2'(默认值),这意味着计算长度(通过分量平方和的平方根),每个元素除以该长度,结果是一个向量长度1.

    【讨论】:

    • 感谢您花时间解释它。我知道我应该多阅读文档,但有时我仍然无法完全理解。
    猜你喜欢
    • 2020-11-06
    • 2017-06-10
    • 2018-04-16
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多