gpt4 book ai didi

python - 谁能解释一下 StandardScaler?

转载 作者:IT老高 更新时间:2023-10-28 20:24:19 24 4
gpt4 key购买 nike

我无法理解 page sklearn 文档中的 StandardScaler

谁能简单的给我解释一下?

最佳答案

简介

我假设您有一个矩阵 X,其中每个 行/行 是一个 样本/观察 并且每个 是一个变量/特征(顺便说一下,这是任何 sklearn ML 函数的预期输入——X.shape 应该是 [number_of_samples, number_of_features])。


方法的核心

主要思想是规范化/标准化,即 μ = 0σ = 1 的特征/变量/列X单独应用任何机器学习模型之前。

StandardScaler() will normalize the features i.e. eachcolumn of X, INDIVIDUALLY, so that each column/feature/variable will have μ = 0 and σ = 1.


P.S:我在这个页面上找到了最受好评的答案,错了。我引用的是“数据集中的每个值都会减去样本平均值”——这既不正确也不正确。


另请参阅:How and why to Standardize your data: A python tutorial


代码示例

from sklearn.preprocessing import StandardScaler
import numpy as np

# 4 samples/observations and 2 variables/features
data = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])
scaler = StandardScaler()
scaled_data = scaler.fit_transform(data)

print(data)
[[0, 0],
[1, 0],
[0, 1],
[1, 1]])

print(scaled_data)
[[-1. -1.]
[ 1. -1.]
[-1. 1.]
[ 1. 1.]]

验证每个特征(列)的均值是否为 0:

scaled_data.mean(axis = 0)
array([0., 0.])

验证每个特征(列)的std为1:

scaled_data.std(axis = 0)
array([1., 1.])

附录:数学

enter image description here


UPDATE 08/2020:关于输入参数 with_meanwith_stdFalse/True ,我在这里提供了答案:StandardScaler difference between “with_std=False or True” and “with_mean=False or True”

关于python - 谁能解释一下 StandardScaler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com