这似乎与标准差的计算方式有关。
>>> import numpy as np
>>> a = np.array([[1, 2],[3, 1]])
>>> np.std(a, axis=0)
array([ 1. , 0.5])
>>> np.std(a, axis=0, ddof=1)
array([ 1.41421356, 0.70710678])
From numpy.std
文档 http://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html,
ddof :整数,可选
表示自由度 Delta。计算中使用的除数是 N - ddof,其中 N 表示元素的数量。默认情况下 ddof 为零。
显然,R.scale()
uses ddof=1
, but sklearn.preprocessing.StandardScaler()
uses ddof=0
.
编辑:(解释如何使用备用 ddof)
似乎没有一种直接的方法可以在不访问 StandardScaler() 对象本身的变量的情况下使用替代 ddof 计算 std。
sc = StandardScaler()
sc.fit(data)
# Now, sc.mean_ and sc.std_ are the mean and standard deviation of the data
# Replace the sc.std_ value using std calculated using numpy
sc.std_ = numpy.std(data, axis=0, ddof=1)