ElementwiseProduct
ElementwiseProduct multiplies each input vector by a provided β€œweight” vector, using element-wise multiplication. In other words, it scales each column of the dataset by a scalar multiplier. This represents the Hadamard product between the input vector, v and transforming vector, w, to yield a result vector.
1
import org.apache.spark.ml.feature.ElementwiseProduct
2
import org.apache.spark.ml.linalg.Vectors
3
// Create some vector data; also works for sparse vectors
4
val dataFrame = spark.createDataFrame(Seq(
5
("a", Vectors.dense(1.0, 2.0, 3.0)),
6
("b", Vectors.dense(4.0, 5.0, 6.0)))).toDF("id", "vector")
7
val transformingVector = Vectors.dense(0.0, 1.0, 2.0)
8
val transformer = new ElementwiseProduct()
9
.setScalingVec(transformingVector)
10
.setInputCol("vector")
11
.setOutputCol("transformedVector")
12
// Batch transform the vectors to create new column:
13
transformer.transform(dataFrame).show()
14
/*
15
Output:
16
+---+-------------+-----------------+
17
| id| vector|transformedVector|
18
+---+-------------+-----------------+
19
| a|[1.0,2.0,3.0]| [0.0,2.0,6.0]|
20
| b|[4.0,5.0,6.0]| [0.0,5.0,12.0]|
21
+---+-------------+-----------------+
22
​
23
*/
Copied!
Last modified 1yr ago
Copy link