SparkML包中的⼏种归⼀化⽅法总结
org.apache.spark.ml.feature包中包含了4种不同的归⼀化⽅法:
Normalizer
泰宁县StandardScaler
MinMaxScaler
MaxAbsScaler
有时感觉会容易混淆,借助官⽅⽂档和实际数据的变换,在这⾥做⼀次总结。我为什么要学习
原⽂地址:
0 数据准备
import org.apache.spark.ml.linalg.Vectors
val dataFrame = ateDataFrame(Seq(
(0, Vectors.den(1.0, 0.5, -1.0)),
(1, Vectors.den(2.0, 1.0, 1.0)),
(2, Vectors.den(4.0, 10.0, 2.0))
)).toDF("id", "features")
dataFrame.show
// 原始数据
+---+--------------+
| id| features|
+---+--------------+
| 0|[1.0,0.5,-1.0]|
| 1| [2.0,1.0,1.0]|
| 2|[4.0,10.0,2.0]|
+---+--------------+
1 Normalizer
Normalizer的作⽤范围是每⼀⾏,使每⼀个⾏向量的范数变换为⼀个单位范数,下⾯的⽰例代码都来⾃加上少量改写和注释。
import org.apache.spark.ml.feature.Normalizer
关于读书名人名言
// 正则化每个向量到1阶范数
val normalizer = new Normalizer()
.tInputCol("features")
.tOutputCol("normFeatures")
.tP(1.0)
val l1NormData = ansform(dataFrame)
println("Normalized using L^1 norm")
l1NormData.show()
// 将每⼀⾏的规整为1阶范数为1的向量,1阶范数即所有值绝对值之和。
蜿蜒读音+---+--------------+------------------+
| id| features| normFeatures|
+---+--------------+------------------+dare用法
| 0|[1.0,0.5,-1.0]| [0.4,0.2,-0.4]|
| 1| [2.0,1.0,1.0]| [0.5,0.25,0.25]|
| 2|[4.0,10.0,2.0]|[0.25,0.625,0.125]|
+---+--------------+------------------+
// 正则化每个向量到⽆穷阶范数
val lInfNormData = ansform(dataFrame, normalizer.p -> Double.PositiveInfinity)
println("Normalized using L^inf norm")
lInfNormData.show()
// 向量的⽆穷阶范数即向量中所有值中的最⼤值
+---+--------------+--------------+
| id| features| normFeatures|
+---+--------------+--------------+
| 0|[1.0,0.5,-1.0]|[1.0,0.5,-1.0]|
| 1| [2.0,1.0,1.0]| [1.0,0.5,0.5]|
| 2|[4.0,10.0,2.0]| [0.4,1.0,0.2]|
+---+--------------+--------------+
2 StandardScaler
StandardScaler处理的对象是每⼀列,也就是每⼀维特征,将特征标准化为单位标准差或是0均值,或是0均值单位标准差。主要有两个参数可以设置:
- withStd: 默认为真。将数据标准化到单位标准差。
- withMean: 默认为假。是否变换为0均值。
StandardScaler需要fit数据,获取每⼀维的均值和标准差,来缩放每⼀维特征。
import org.apache.spark.ml.feature.StandardScaler
val scaler = new StandardScaler()
.tInputCol("features")
.tOutputCol("scaledFeatures")
.tWithStd(true)
男科偏方
.tWithMean(fal)
// Compute summary statistics by fitting the StandardScaler.
val scalerModel = scaler.fit(dataFrame)
// Normalize each feature to have unit standard deviation.
val scaledData = ansform(dataFrame)
scaledData.show
// 将每⼀列的标准差缩放到1。
+---+--------------+------------------------------------------------------------+
|id |features |scaledFeatures |
+---+--------------+------------------------------------------------------------+
|0 |[1.0,0.5,-1.0]|[0.6546536707079772,0.09352195295828244,-0.6546536707079771]|
|1 |[2.0,1.0,1.0] |[1.3093073414159544,0.1870439059165649,0.6546536707079771] |
|2 |[4.0,10.0,2.0]|[2.618614682831909,1.870439059165649,1.3093073414159542] |
+---+--------------+------------------------------------------------------------+
3 MinMaxScaler
MinMaxScaler作⽤同样是每⼀列,即每⼀维特征。将每⼀维特征线性地映射到指定的区间,通常是[0, 1]。它也有两个参数可以设置:
- min: 默认为0。指定区间的下限。
- max: 默认为1。指定区间的上限。
import org.apache.spark.ml.feature.MinMaxScaler
val scaler = new MinMaxScaler()
巨屏手机
.tInputCol("features")
.tOutputCol("scaledFeatures")
// Compute summary statistics and generate MinMaxScalerModel
val scalerModel = scaler.fit(dataFrame)
// rescale each feature to range [min, max].
val scaledData = ansform(dataFrame)
println(s"Features scaled to range: [${Min}, ${Max}]")
scaledData.lect("features", "scaledFeatures").show
// 每维特征线性地映射,最⼩值映射到0,最⼤值映射到1。
+--------------+-----------------------------------------------------------+
|features |scaledFeatures |
渐冻人+--------------+-----------------------------------------------------------+
|[1.0,0.5,-1.0]|[0.0,0.0,0.0] |
|[2.0,1.0,1.0] |[0.3333333333333333,0.05263157894736842,0.6666666666666666]|
|[4.0,10.0,2.0]|[1.0,1.0,1.0] |
+--------------+-----------------------------------------------------------+
4 MaxAbsScaler
MaxAbsScaler将每⼀维的特征变换到[-1, 1]闭区间上,通过除以每⼀维特征上的最⼤的绝对值,它不会平移整个分布,也不会破坏原来每⼀个特征向量的稀疏性。
import org.apache.spark.ml.feature.MaxAbsScaler
val scaler = new MaxAbsScaler()
.tInputCol("features")
.tOutputCol("scaledFeatures")
// Compute summary statistics and generate MaxAbsScalerModel
val scalerModel = scaler.fit(dataFrame)
// rescale each feature to range [-1, 1]
val scaledData = ansform(dataFrame)
scaledData.lect("features", "scaledFeatures").show()
// 每⼀维的绝对值的最⼤值为[4, 10, 2]
+--------------+----------------+
| features| scaledFeatures|
+--------------+----------------+
|[1.0,0.5,-1.0]|[0.25,0.05,-0.5]|
| [2.0,1.0,1.0]| [0.5,0.1,0.5]|
|[4.0,10.0,2.0]| [1.0,1.0,1.0]|
+--------------+----------------+
总结
所有4种归⼀化⽅法都是线性的变换,当某⼀维特征上具有⾮线性的分布时,还需要配合其它的特征预处理⽅法。