为什么 Python 中的 sklearn.preprocessing.StandardScaler
标准化与 Matlab 中的 zscore
不同?
Python 中的 sklearn.preprocessing
示例:
>>> from sklearn.preprocessing import StandardScaler
>>> data = [[0, 0], [0, 0], [1, 1], [1, 1]]
>>> scaler = StandardScaler()
>>> scaler.fit(data)
>>> print(scaler.mean_)
[ 0.5 0.5]
>>> print(scaler.var_)
[0.25 0.25]
>>> print(scaler.transform(data))
[[-1. -1.]
[-1. -1.]
[ 1. 1.]
[ 1. 1.]]
Matlab 中使用 zscore
函数的相同示例:
>> data = [[0, 0]; [0, 0]; [1, 1]; [1, 1]];
>> [Sd_data,mean,stdev] = zscore(data)
Sd_data =
-0.8660 -0.8660
-0.8660 -0.8660
0.8660 0.8660
0.8660 0.8660
mean =
0.5000 0.5000
stdev =
0.5774 0.5774
最佳答案
问题似乎在于自由度(ddof - 与标准差估计相关的校正因子),StandardScaler
默认情况下该自由度似乎为 0。
作为替代方案,scipy.stats
的 zscore
函数允许您在缩放时控制此参数:
from scipy.stats import zscore
zscore(data, ddof=1)
array([[-0.8660254, -0.8660254],
[-0.8660254, -0.8660254],
[ 0.8660254, 0.8660254],
[ 0.8660254, 0.8660254]])
您最终会得到与 matlab 函数相同的输出。当您使用 ddof=0
调用 zscore
时,您将获得与 StandardScaler
相同的输出。
关于python - 为什么 Python 的 'StandardScaler' 和 Matlab 的 'zscore' 之间的标准化不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150309/