Spark MaxAbsScaler 绝对值最大标准化
package com.home.spark.ml import org.apache.spark.SparkConf import org.apache.spark.ml.feature.MaxAbsScaler import org.apache.spark.ml.linalg.Vectors import org.apache.spark.sql.SparkSession /** * @Description: 绝对值最大标准化:MaxAbsScaler * 同样是对某一个特征操作,各特征值除以最大绝对值,因此缩放到[-1,1]之间。且不移动中心点。不会将稀疏矩阵变得稠密。 * 例如一个叫长度的特征,有三个样本有此特征,特征向量为[-1000,100,10],最大绝对值为1000, * 转换为[-1000/1000,100/100,10/1000]=[-1,0.1,0.01]。 * * 如果最大绝对值是一个离群点,显然这种处理方式是很不合理的 **/ object Ex_MaxAbsScaler { def main(args: Array[String]): Unit = { val conf: SparkConf = new SparkConf(true).setMaster("local[2]").setAppName("spark ml") val spark = SparkSession.builder().config(conf).getOrCreate() val dataFrame = spark.createDataFrame(Seq( (0, Vectors.dense(1.0, 0.1, -8.0)), (1, Vectors.dense(2.0, 1.0, -4.0)), (2, Vectors.dense(4.0, 10.0, 8.0)) )).toDF("id", "features") val scaler = new MaxAbsScaler() .setInputCol("features") .setOutputCol("scaledFeatures") // Compute summary statistics and generate MaxAbsScalerModel val scalerModel = scaler.fit(dataFrame) // rescale each feature to range [-1, 1] val scaledData = scalerModel.transform(dataFrame) scaledData.select("*").show() spark.stop() } }
相关推荐
Johnson0 2020-07-28
Oeljeklaus 2020-05-29
登峰小蚁 2020-05-11
Hhanwen 2020-05-04
Oeljeklaus 2020-04-19
Hhanwen 2020-07-26
zhixingheyitian 2020-07-19
yanqianglifei 2020-07-07
Hhanwen 2020-07-05
Hhanwen 2020-06-25
rongwenbin 2020-06-15
sxyhetao 2020-06-12
hovermenu 2020-06-10
Oeljeklaus 2020-06-10
zhixingheyitian 2020-06-08
Johnson0 2020-06-08