【发布时间】:2019-01-16 02:19:17
【问题描述】:
知道为什么 spark 会为StandardScaler 这样做吗?根据StandardScaler的定义:
StandardScaler 将一组特征标准化为均值为零 标准差为 1。withStd 标志会将数据缩放到 单位标准偏差,而标志 withMean(默认为 false) 将在缩放之前将数据居中。
>>> 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]|
+----+----+----+------------+-------------------------------------------+
在 numpy 世界中
>>> x
array([2., 8., 5.])
>>> (x - x.mean())/x.std()
array([-1.22474487, 1.22474487, 0. ])
在 sklearn 世界中
>>> 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. ]]
【问题讨论】:
标签: apache-spark pyspark apache-spark-ml