Skip to content

Commit 90f7e39

Browse files
jkbradleyogeagla
authored andcommitted
small cleanups
1 parent 4595165 commit 90f7e39

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

mllib/src/main/scala/org/apache/spark/ml/feature/ElementwiseProduct.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import org.apache.spark.sql.types.DataType
2727
/**
2828
* :: AlphaComponent ::
2929
* Outputs the Hadamard product (i.e., the element-wise product) of each input vector with a
30-
* provided "weight" vector. In other words, it scales each column of the dataset by a scalar multiplier.
30+
* provided "weight" vector. In other words, it scales each column of the dataset by a scalar
31+
* multiplier.
3132
*/
3233
@AlphaComponent
3334
class ElementwiseProduct extends UnaryTransformer[Vector, Vector, ElementwiseProduct] {

mllib/src/main/scala/org/apache/spark/mllib/feature/ElementwiseProduct.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717

1818
package org.apache.spark.mllib.feature
1919

20-
import org.apache.spark.annotation.AlphaComponent
20+
import org.apache.spark.annotation.Experimental
2121
import org.apache.spark.mllib.linalg._
2222

2323
/**
24-
* :: AlphaComponent ::
25-
* Outputs the Hadamard product (i.e., the element-wise product) of each input vector with a provided
26-
* "weight" vector. In other words, it scales each column of the dataset by a scalar multiplier.
24+
* :: Experimental ::
25+
* Outputs the Hadamard product (i.e., the element-wise product) of each input vector with a
26+
* provided "weight" vector. In other words, it scales each column of the dataset by a scalar
27+
* multiplier.
2728
* @param scalingVector The values used to scale the reference vector's individual components.
2829
*/
29-
@AlphaComponent
30+
@Experimental
3031
class ElementwiseProduct(val scalingVector: Vector) extends VectorTransformer {
3132

3233
/**
@@ -37,7 +38,7 @@ class ElementwiseProduct(val scalingVector: Vector) extends VectorTransformer {
3738
*/
3839
override def transform(vector: Vector): Vector = {
3940
require(vector.size == scalingVector.size,
40-
s"vector sizes do not match: ${scalingVector.size} ${vector.size}")
41+
s"vector sizes do not match: Expected ${scalingVector.size} but found ${vector.size}")
4142
vector match {
4243
case dv: DenseVector =>
4344
val values: Array[Double] = dv.values.clone()
@@ -50,7 +51,7 @@ class ElementwiseProduct(val scalingVector: Vector) extends VectorTransformer {
5051
Vectors.dense(values)
5152
case SparseVector(size, indices, vs) =>
5253
val values = vs.clone()
53-
val dim = values.size
54+
val dim = values.length
5455
var i = 0
5556
while (i < dim) {
5657
values(i) *= scalingVector(indices(i))

mllib/src/test/scala/org/apache/spark/mllib/feature/ElementwiseProductSuite.scala

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,24 @@ import org.apache.spark.mllib.util.TestingUtils._
2525

2626
class ElementwiseProductSuite extends FunSuite with MLlibTestSparkContext {
2727

28-
val denseData = Array(
29-
Vectors.dense(1.0, 4.0, 1.9, -9.0)
30-
)
31-
32-
val sparseData = Array(
33-
Vectors.sparse(3, Seq((1, -1.0), (2, -3.0)))
34-
)
35-
36-
val scalingVector = Vectors.dense(2.0, 0.5, 0.0, 0.25)
37-
3828
test("elementwise (hadamard) product should properly apply vector to dense data set") {
39-
val transformer = new ElementwiseProduct(scalingVector)
29+
val denseData = Array(
30+
Vectors.dense(1.0, 4.0, 1.9, -9.0)
31+
)
32+
val scalingVec = Vectors.dense(2.0, 0.5, 0.0, 0.25)
33+
val transformer = new ElementwiseProduct(scalingVec)
4034
val transformedData = transformer.transform(sc.makeRDD(denseData))
4135
val transformedVecs = transformedData.collect()
42-
val transformedVec = transformedVecs(0).toArray
43-
44-
assert(transformedVec(0) === 2.0, "product by 2.0 should have been applied")
45-
assert(transformedVec(1) === 2.0, "product by 0.5 should have been applied")
46-
assert(transformedVec(2) === 0.0, "product by 0.0 should have been applied")
47-
assert(transformedVec(3) === -2.25, "product by 0.25 should have been applied")
36+
val transformedVec = transformedVecs(0)
37+
val expectedVec = Vectors.dense(2.0, 2.0, 0.0, -2.25)
38+
assert(transformedVec ~== expectedVec absTol 1E-5,
39+
s"Expected transformed vector $expectedVec but found $transformedVec")
4840
}
4941

5042
test("elementwise (hadamard) product should properly apply vector to sparse data set") {
43+
val sparseData = Array(
44+
Vectors.sparse(3, Seq((1, -1.0), (2, -3.0)))
45+
)
5146
val dataRDD = sc.parallelize(sparseData, 3)
5247
val scalingVec = Vectors.dense(1.0, 0.0, 0.5)
5348
val transformer = new ElementwiseProduct(scalingVec)

0 commit comments

Comments
 (0)