Skip to content

Commit cb520e6

Browse files
committed
[SPARK-5726] [MLLIB] Rename HadamardProduct to ElementwiseProduct
1 parent 4922722 commit cb520e6

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

docs/mllib-feature-extraction.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,9 @@ sc.stop();
477477
</div>
478478
</div>
479479

480-
## HadamardProduct
480+
## ElementwiseProduct
481481

482-
HadamardProduct scales individual vector samples by a provided weighting vector component-wise. This represents the [Hadamard product](https://en.wikipedia.org/wiki/Hadamard_product_%28matrices%29) between the input vector, `v` and weighting vector, `w`, to yield a result vector.
482+
ElementwiseProduct multiplies individual vector samples by a provided weighting vector component-wise. This represents the [Hadamard product](https://en.wikipedia.org/wiki/Hadamard_product_%28matrices%29) between the input vector, `v` and transforming vector, `w`, to yield a result vector.
483483

484484
`\[ \begin{pmatrix}
485485
v_1 \\
@@ -497,53 +497,53 @@ v_N
497497
\end{pmatrix}
498498
\]`
499499

500-
[`HadamardProduct`](api/scala/index.html#org.apache.spark.mllib.feature.HadamardProduct) has the following parameter in the constructor:
500+
[`ElementwiseProduct`](api/scala/index.html#org.apache.spark.mllib.feature.ElementwiseProduct) has the following parameter in the constructor:
501501

502-
* `w` Vector, the scaling vector.
502+
* `w` Vector, the transforming vector.
503503

504-
`HadamardProduct` implements [`VectorTransformer`](api/scala/index.html#org.apache.spark.mllib.feature.VectorTransformer) which can apply the weighting on a `Vector` to produce a transformed `Vector` or on an `RDD[Vector]` to produce a transformed `RDD[Vector]`.
504+
`ElementwiseProduct` implements [`VectorTransformer`](api/scala/index.html#org.apache.spark.mllib.feature.VectorTransformer) which can apply the weighting on a `Vector` to produce a transformed `Vector` or on an `RDD[Vector]` to produce a transformed `RDD[Vector]`.
505505

506506
### Example
507507

508-
This example below demonstrates how to load a simple vectors file, extract a set of vectors, then weight those vectors using a weighting vector value.
508+
This example below demonstrates how to load a simple vectors file, extract a set of vectors, then transform those vectors using a transforming vector value.
509509

510510

511511
<div class="codetabs">
512512
<div data-lang="scala">
513513
{% highlight scala %}
514514
import org.apache.spark.SparkContext._
515-
import org.apache.spark.mllib.feature.HadamardProduct
515+
import org.apache.spark.mllib.feature.ElementwiseProduct
516516
import org.apache.spark.mllib.linalg.Vectors
517517

518518
//load and parse the data
519519
val data = sc.textFile("data/mllib/kmeans_data.txt")
520520
val parsedData = data.map(s => Vectors.dense(s.split(' ').map(_.toDouble)))
521521

522-
val weightingVector = Vectors.dense(0.0, 1.0, 2.0)
523-
val scaler = new HadamardProduct(weightingVector)
522+
val transformingVector = Vectors.dense(0.0, 1.0, 2.0)
523+
val transformer = new ElementwiseProduct(transformingVector)
524524

525525
//same results:
526-
val weightedData = scaler.transform(parsedData)
527-
val weightedData2 = parsedData.map(x => scaler.transform(x))
526+
val transformedData = transformer.transform(parsedData)
527+
val transformedData2 = parsedData.map(x => transformer.transform(x))
528528

529529
{% endhighlight %}
530530
</div>
531531

532532
<div data-lang="python">
533533
{% highlight python %}
534534
from pyspark.mllib.linalg import Vectors
535-
from pyspark.mllib.feature import HadamardProduct
535+
from pyspark.mllib.feature import ElementwiseProduct
536536

537537
# Load and parse the data
538538
data = sc.textFile("data/mllib/kmeans_data.txt")
539539
parsedData = data.map(lambda line: array([float(x) for x in line.split(' ')]))
540540

541-
weightingVector = Vectors.dense(0.0, 1.0, 2.0)
542-
scaler = HadamardProduct(weightingVector)
541+
transformingVector = Vectors.dense(0.0, 1.0, 2.0)
542+
transformer = ElementwiseProduct(transformingVector)
543543

544544
# Same results:
545-
weightedData = scaler.transform(parsedData)
546-
weightedData2 = parsedData.map(lambda x: scaler.transform(x))
545+
transformedData = transformer.transform(parsedData)
546+
transformedData2 = parsedData.map(lambda x: transformer.transform(x))
547547

548548
{% endhighlight %}
549549
</div>

mllib/src/main/scala/org/apache/spark/ml/feature/HadamardProductTF.scala renamed to mllib/src/main/scala/org/apache/spark/ml/feature/ElementwiseProductTF.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package org.apache.spark.ml.feature
2020
import org.apache.spark.annotation.AlphaComponent
2121
import org.apache.spark.ml.UnaryTransformer
2222
import org.apache.spark.ml.param.{Param, ParamMap}
23-
import org.apache.spark.mllib.feature.HadamardProduct
23+
import org.apache.spark.mllib.feature.ElementwiseProduct
2424
import org.apache.spark.mllib.linalg.{Vector, VectorUDT}
2525
import org.apache.spark.sql.types.DataType
2626

@@ -29,16 +29,16 @@ import org.apache.spark.sql.types.DataType
2929
* Maps a vector to the hadamard product of it and a reference vector.
3030
*/
3131
@AlphaComponent
32-
class HadamardProductTF extends UnaryTransformer[Vector, Vector, HadamardProductTF] {
32+
class ElementwiseProductTF extends UnaryTransformer[Vector, Vector, ElementwiseProductTF] {
3333

3434
/** the vector to multiply with input vectors */
3535
val scalingVec : Param[Vector] = new Param(this, "scalingVector", "vector for hadamard product")
3636
def setScalingVec(value: Vector) = set(scalingVec, value)
3737
def getScalingVec: Vector = get(scalingVec)
3838

3939
override protected def createTransformFunc(paramMap: ParamMap): Vector => Vector = {
40-
val hadScaler = new HadamardProduct(paramMap(scalingVec))
41-
hadScaler.transform
40+
val elemScaler = new ElementwiseProduct(paramMap(scalingVec))
41+
elemScaler.transform
4242
}
4343

4444
override protected def outputDataType: DataType = new VectorUDT()

mllib/src/main/scala/org/apache/spark/mllib/feature/HadamardProduct.scala renamed to mllib/src/main/scala/org/apache/spark/mllib/feature/ElementwiseProduct.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import org.apache.spark.mllib.linalg._
2222

2323
/**
2424
* :: Experimental ::
25-
* Component-wise scaling of dense vectors by a provided vector's components.
25+
* Element-wise product of dense vectors by a provided vector's components.
2626
*
2727
* @param scalingVector The values used to scale the reference vector's individual components.
2828
*/
2929
@Experimental
30-
class HadamardProduct(val scalingVector: Vector) extends VectorTransformer {
30+
class ElementwiseProduct(val scalingVector: Vector) extends VectorTransformer {
3131

3232
/**
3333
* Does the hadamard product transformation.

mllib/src/test/scala/org/apache/spark/mllib/feature/HadamardProductSuite.scala renamed to mllib/src/test/scala/org/apache/spark/mllib/feature/ElementwiseProductSuite.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.apache.spark.mllib.util.MLlibTestSparkContext
2222
import org.scalatest.FunSuite
2323
import org.apache.spark.mllib.util.TestingUtils._
2424

25-
class HadamardProductSuite extends FunSuite with MLlibTestSparkContext{
25+
class ElementwiseProductSuite extends FunSuite with MLlibTestSparkContext{
2626

2727
val denseData = Array(
2828
Vectors.dense(1.0, 1.0, 0.0, 0.0),
@@ -43,31 +43,31 @@ class HadamardProductSuite extends FunSuite with MLlibTestSparkContext{
4343

4444
val scalingVector = Vectors.dense(2.0, 0.5, 0.0, 0.25)
4545

46-
test("hadamard product should properly apply vector to dense data set") {
46+
test("elementwise (hadamard) product should properly apply vector to dense data set") {
4747

48-
val scaler = new HadamardProduct(scalingVector)
49-
val scaledData = scaler.transform(sc.makeRDD(denseData))
48+
val transformer = new ElementwiseProduct(scalingVector)
49+
val transformedData = transformer.transform(sc.makeRDD(denseData))
5050

51-
val scaledVecs = scaledData.collect()
51+
val transformedVecs = transformedData.collect()
5252

53-
val fourthVec = scaledVecs.apply(3).toArray
53+
val fourthVec = transformedVecs.apply(3).toArray
5454

5555
assert(fourthVec.apply(0) === 2.0, "product by 2.0 should have been applied")
5656
assert(fourthVec.apply(1) === 2.0, "product by 0.5 should have been applied")
5757
assert(fourthVec.apply(2) === 0.0, "product by 0.0 should have been applied")
5858
assert(fourthVec.apply(3) === -2.25, "product by 0.25 should have been applied")
5959
}
6060

61-
test("hadamard product should properly apply vector to sparse data set") {
61+
test("elementwise (hadamard) product should properly apply vector to sparse data set") {
6262

6363
val dataRDD = sc.parallelize(sparseData, 3)
6464

6565
val scalingVec = Vectors.dense(1.0, 0.0, 0.5)
6666

67-
val hadScaler = new HadamardProduct(scalingVec)
67+
val transformer = new ElementwiseProduct(scalingVec)
6868

69-
val data2 = sparseData.map(hadScaler.transform)
70-
val data2RDD = hadScaler.transform(dataRDD)
69+
val data2 = sparseData.map(transformer.transform)
70+
val data2RDD = transformer.transform(dataRDD)
7171

7272
assert((sparseData, data2, data2RDD.collect()).zipped.forall {
7373
case (v1: DenseVector, v2: DenseVector, v3: DenseVector) => true

0 commit comments

Comments
 (0)