Spark ML机器学习:元素智能乘积-ElementwiseProduct

ElementwiseProduct对每一个输入向量乘以一个给定的“权重”向量。换句话说,就是通过一个乘子对数据集的每一列进行缩放。这个转换可以表示为如下的形式:

6.1

实例:

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]|
+—+————-+—————–+

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注