ElementwiseProduct
对每一个输入向量乘以一个给定的“权重”向量。换句话说,就是通过一个乘子对数据集的每一列进行缩放。这个转换可以表示为如下的形式:
实例:
import org.apache.spark.sql.SparkSession object ElementwiseProductExample { def main(args: Array[String]): Unit = { val sparkConf = new SparkConf(); sparkConf.setMaster("local[*]").setAppName(this.getClass.getSimpleName) val spark = SparkSession .builder .config(sparkConf) .appName("ElementwiseProductExample") .getOrCreate() // $example on$ // Create some vector data; also works for sparse vectors val dataFrame = spark.createDataFrame(Seq( ("a", Vectors.dense(1.0, 2.0, 3.0)), ("b", Vectors.dense(4.0, 5.0, 6.0)))).toDF("id", "vector") val transformingVector = Vectors.dense(0.0, 1.0, 2.0) val transformer = new ElementwiseProduct() .setScalingVec(transformingVector) .setInputCol("vector") .setOutputCol("transformedVector") // Batch transform the vectors to create new column: transformer.transform(dataFrame).show() // $example off$ spark.stop() } }
输出结果:
+—+————-+—————–+
| id| vector|transformedVector|
+—+————-+—————–+
| a|[1.0,2.0,3.0]| [0.0,2.0,6.0]|
| b|[4.0,5.0,6.0]| [0.0,5.0,12.0]|
+—+————-+—————–+