Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scikit learn .transform()在sklearn StandardScaler中到底做了什么?_Scikit Learn - Fatal编程技术网

Scikit learn .transform()在sklearn StandardScaler中到底做了什么?

Scikit learn .transform()在sklearn StandardScaler中到底做了什么?,scikit-learn,Scikit Learn,我现在正在学习StandardScaler。然而,我不明白转换到底是做什么的。在API文档中,它只是说“通过居中和缩放来执行标准化”,有人能解释一下吗?如果您单击右侧的[source],您可以看到。从第796行到第807行,您将看到 if sparse.issparse(X): if self.with_mean: raise ValueError( "Cannot center sparse matrices: pass `with_m

我现在正在学习StandardScaler。然而,我不明白转换到底是做什么的。在API文档中,它只是说“通过居中和缩放来执行标准化”,有人能解释一下吗?

如果您单击右侧的
[source]
,您可以看到。从第796行到第807行,您将看到

if sparse.issparse(X):
    if self.with_mean:
        raise ValueError(
            "Cannot center sparse matrices: pass `with_mean=False` "
            "instead. See docstring for motivation and alternatives.")
    if self.scale_ is not None:
        inplace_column_scale(X, 1 / self.scale_)
else:
    if self.with_mean:
        X -= self.mean_
    if self.with_std:
        X /= self.scale_

您可以看到它在if-else块中执行标准化。

标准定标器函数具有以下公式:

z = (x - u) / s
这里,

此元素转换是按列进行的。 因此,当您调用拟合时,将计算
平均值
标准偏差
的值

例如:

输出:

array([[26,  9],
       [29, 39],
       [23, 26],
       [29, 22],
       [28, 41],
       [11,  6],
       [42, 40],
       [ 1, 25],
       [ 0, 39],
       [44, 45]])
array([[ 0.18793099, -1.53862621],
       [ 0.3967432 ,  0.74646222],
       [-0.02088122, -0.24374277],
       [ 0.3967432 , -0.54842122],
       [ 0.32713913,  0.89880145],
       [-0.85613006, -1.76713506],
       [ 1.3015961 ,  0.82263184],
       [-1.55217075, -0.31991238],
       [-1.62177482,  0.74646222],
       [ 1.44080424,  1.20347991]])
现在,安装标准定标器

scale = StandardScaler()
scale.fit(x)
您可以使用
StandardScaler
对象的内置方法查看平均值和标准偏差

# Mean
scale.mean_   # array([23.3, 29.2])

# Standard Deviation
scale.scale_  # array([14.36697602, 13.12859475])
可以使用“变换”方法变换这些值

scale.transform(x)
输出:

array([[26,  9],
       [29, 39],
       [23, 26],
       [29, 22],
       [28, 41],
       [11,  6],
       [42, 40],
       [ 1, 25],
       [ 0, 39],
       [44, 45]])
array([[ 0.18793099, -1.53862621],
       [ 0.3967432 ,  0.74646222],
       [-0.02088122, -0.24374277],
       [ 0.3967432 , -0.54842122],
       [ 0.32713913,  0.89880145],
       [-0.85613006, -1.76713506],
       [ 1.3015961 ,  0.82263184],
       [-1.55217075, -0.31991238],
       [-1.62177482,  0.74646222],
       [ 1.44080424,  1.20347991]])
第一个元素的计算:

z = (26 - 23.3) /  14.36697602
z = 0.18793099