- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
知道为什么 spark 会为 StandardScaler
这样做吗? ?根据 StandardScaler
的定义:
The StandardScaler standardizes a set of features to have zero mean and a standard deviation of 1. The flag withStd will scale the data to unit standard deviation while the flag withMean (false by default) will center the data prior to scaling it.
>>> tmpdf.show(4)
+----+----+----+------------+
|int1|int2|int3|temp_feature|
+----+----+----+------------+
| 1| 2| 3| [2.0]|
| 7| 8| 9| [8.0]|
| 4| 5| 6| [5.0]|
+----+----+----+------------+
>>> sScaler = StandardScaler(withMean=True, withStd=True).setInputCol("temp_feature")
>>> sScaler.fit(tmpdf).transform(tmpdf).show()
+----+----+----+------------+-------------------------------------------+
|int1|int2|int3|temp_feature|StandardScaler_4fe08ca180ab163e4120__output|
+----+----+----+------------+-------------------------------------------+
| 1| 2| 3| [2.0]| [-1.0]|
| 7| 8| 9| [8.0]| [1.0]|
| 4| 5| 6| [5.0]| [0.0]|
+----+----+----+------------+-------------------------------------------+
>>> x
array([2., 8., 5.])
>>> (x - x.mean())/x.std()
array([-1.22474487, 1.22474487, 0. ])
>>> scaler = StandardScaler(with_mean=True, with_std=True)
>>> data
[[2.0], [8.0], [5.0]]
>>> print(scaler.fit(data).transform(data))
[[-1.22474487]
[ 1.22474487]
[ 0. ]]
最佳答案
您的结果不符合预期的原因是因为 pyspark.ml.feature.StandardScaler
使用无偏样本标准差而不是总体标准差。
从文档:
The “unit std” is computed using the corrected sample standard deviation, which is computed as the square root of the unbiased sample variance.
numpy
代码与样本标准偏差,你会看到相同的结果:
import numpy as np
x = np.array([2., 8., 5.])
print((x - x.mean())/x.std(ddof=1))
#array([-1., 1., 0.])
# using the same x as above
def popstd(x):
return np.sqrt(sum((xi - x.mean())**2/len(x) for xi in x))
print(popstd(x))
#2.4494897427831779
print(x.std())
#2.4494897427831779
len(x)-1
而不是
len(x)
.因此,如果您添加了一个等于平均值的样本,则可以在不影响整体平均值的情况下增加分母。
df = spark.createDataFrame(
np.array(range(1,10,1)).reshape(3,3).tolist(),
["int1", "int2", "int3"]
)
df.show()
#+----+----+----+
#|int1|int2|int3|
#+----+----+----+
#| 1| 2| 3|
#| 4| 5| 6|
#| 7| 8| 9|
#+----+----+----+
import pyspark.sql.functions as f
# This is equivalent to UNION ALL in SQL
df2 = df.union(df.select(*[f.avg(c).alias(c) for c in df.columns]))
from pyspark.ml.feature import VectorAssembler, StandardScaler
va = VectorAssembler(inputCols=["int2"], outputCol="temp_feature")
tmpdf = va.transform(df2)
sScaler = StandardScaler(
withMean=True, withStd=True, inputCol="temp_feature", outputCol="scaled"
)
sScaler.fit(tmpdf).transform(tmpdf).show()
#+----+----+----+------------+---------------------+
#|int1|int2|int3|temp_feature|scaled |
#+----+----+----+------------+---------------------+
#|1.0 |2.0 |3.0 |[2.0] |[-1.2247448713915892]|
#|4.0 |5.0 |6.0 |[5.0] |[0.0] |
#|7.0 |8.0 |9.0 |[8.0] |[1.2247448713915892] |
#|4.0 |5.0 |6.0 |[5.0] |[0.0] |
#+----+----+----+------------+---------------------+
关于apache-spark - Spark 中的 StandardScaler 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753088/
我有一些数据结构如下,试图根据特征预测 t。 train_df t: time to predict f1: feature1 f2: feature2 f3:...... t 是否可以使用 Stan
我正在使用 sklearn StandardScaler 来转换/规范化数据,如下所示: scaler = StandardScaler() data = scaler.fit_transform(d
既然使用sklearns的StandardScaler对初始数据进行了归一化,那么初始数据不再相同,这不是有问题吗? 示例: from sklearn.preprocessing import Sta
我正在尝试使用 StandardScaler 来缩放神经网络的特征。 假设神经网络具有以下特征: 1.0 2.0 3.0 4.0 5.0 6.0 4.0 11.0 12.0 etc .
我使用 preprocessing.standardscaler 在 sklearn 中标准化了我的数据。问题是如何将其保存在我的本地以备后用? 谢谢 最佳答案 如果我对你的理解是正确的,你想保存你训
我无法理解 page sklearn 文档中的 StandardScaler。 谁能简单的给我解释一下? 最佳答案 简介 我假设您有一个矩阵 X,其中每个 行/行 是一个 样本/观察 并且每个 列 是
我不明白为什么这个神经轴管道不起作用。 我只想要缩放数据并应用 LinearSVC。 我做错了什么? 这就是我想要做的: import numpy as np from sklearn.ensembl
我正在使用一个模型,在分成训练和测试之后,我想应用 StandardScaler()。但是,此转换将我的数据转换为数组,我想保留以前的格式。我该怎么做? 基本上,我有: from sklearn.pr
所以我所理解的是 StandardScaler().fit_transform(X, y) 不会更改目标特征 (y)。同时,对于某些算法(例如基于权重或基于距离的算法)我们还需要对目标特征进行缩放。
我正在使用一个模型,在分成训练和测试之后,我想应用 StandardScaler()。但是,此转换将我的数据转换为数组,我想保留以前的格式。我该怎么做? 基本上,我有: from sklearn.pr
所以我所理解的是 StandardScaler().fit_transform(X, y) 不会更改目标特征 (y)。同时,对于某些算法(例如基于权重或基于距离的算法)我们还需要对目标特征进行缩放。
是否有一种Python式的方法可以将sklearn的StandardScaler实例链接在一起以独立地按组缩放数据?即,如果我想找到独立缩放虹膜数据集的特征;我可以使用以下代码: from sklea
我有一个具有以下形状的数据集: X =(样本,4) y =(样本,) 我正在使用 sckikit-learn 中的 StandardScaler 来标准化 X 的数据 我想使用 X 数据集四列之一中使
我有一个 numpy 数组 X,它有 3 列,如下所示: array([[ 3791, 2629, 0], [ 1198760, 113989,
我不是来自统计学,但是通过使用机器学习和神经网络进行一项工作,我发现缩放数据会产生很多危害。据我所知,在训练测试之前缩放数据并不是一个好的选择,但是请看一下这个在训练测试分离之后进行缩放的例子。 im
我正在使用 StandardScaler 来缩放我的数据,如教程中所示。但它不起作用。 我尝试复制教程中使用的相同代码,但仍然显示错误。 from sklearn.preprocessing impo
我使用 sklearn standardscaler(均值去除和方差缩放)来缩放数据框,并将其与我“手动”减去均值并除以标准差的数据框进行比较。比较显示一致的小差异。谁能解释为什么?(我使用的数据集是
我有一个从以前的模型中保存的 sklearn StandardScaler,我正在尝试将其应用于新数据 scaler = myOldStandardScaler print("ORIG:", X) p
这个问题在这里已经有了答案: pandas dataframe columns scaling with sklearn (7 个答案) 关闭 9 个月前。 我想使用 sklearn 的 Stand
我有一个如下所示的 DataFrame: +-----+--------------------+ | uid| features| +-----+--------------
我是一名优秀的程序员,十分优秀!