Difference between R.scale() and sklearn.preprocessing.scale()

落爺英雄遲暮 提交于 2020-06-24 13:59:15

问题


I am currently moving my data analysis from R to Python. When scaling a dataset in R i would use R.scale(), which in my understanding would do the following: (x-mean(x))/sd(x)

To replace that function I tried to use sklearn.preprocessing.scale(). From my understanding of the description it does the same thing. Nonetheless I ran a little test-file and found out, that both of these methods have different return-values. Obviously the standard deviations are not the same... Is someone able to explain why the standard deviations "deviate" from one another?

MWE:

# import packages
from sklearn import preprocessing
import numpy
import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr
rpy2.robjects.numpy2ri.activate()
# Set up R namespaces
R = rpy2.robjects.r


np1 = numpy.array([[1.0,2.0],[3.0,1.0]])
print "Numpy-array:"
print np1

print "Scaled numpy array through R.scale()"
print R.scale(np1)
print "-------"
print "Scaled numpy array through preprocessing.scale()"
print preprocessing.scale(np1, axis = 0, with_mean = True, with_std = True)
scaler = preprocessing.StandardScaler()
scaler.fit(np1)
print "Mean of preprocessing.scale():"
print scaler.mean_
print "Std of preprocessing.scale():"
print scaler.std_

Output: Output generated by the MWE


回答1:


It seems to have to do with how standard deviation is calculated.

>>> 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 documentation,

ddof : int, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default ddof is zero.

Apparently, R.scale() uses ddof=1, but sklearn.preprocessing.StandardScaler() uses ddof=0.

EDIT: (To explain how to use alternate ddof)

There doesn't seem to be a straightforward way to calculate std with alternate ddof, without accessing the variables of the StandardScaler() object itself.

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)



回答2:


R.scale documentation says:

The root-mean-square for a (possibly centered) column is defined as sqrt(sum(x^2)/(n-1)), where x is a vector of the non-missing values and n is the number of non-missing values. In the case center = TRUE, this is the same as the standard deviation, but in general it is not. (To scale by the standard deviations without centering, use scale(x, center = FALSE, scale = apply(x, 2, sd, na.rm = TRUE)).)

However, sklearn.preprocessing.StandardScale always scale with standard deviation.

In my case, I want to replicate R.scale in Python without centered,I followed @Sid advice in a slightly different way:

import numpy as np

def get_scale_1d(v):
    # I copy this function from R source code haha
    v = v[~np.isnan(v)]
    std = np.sqrt(
        np.sum(v ** 2) / np.max([1, len(v) - 1])
    )
    return std

sc = StandardScaler()
sc.fit(data)
sc.std_ = np.apply_along_axis(func1d=get_scale_1d, axis=0, arr=x)
sc.transform(data)


来源:https://stackoverflow.com/questions/27296387/difference-between-r-scale-and-sklearn-preprocessing-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!