元素智能乘积

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

6.1

  下面是一个使用的实例。

  1. import org.apache.spark.SparkContext._
  2. import org.apache.spark.mllib.feature.ElementwiseProduct
  3. import org.apache.spark.mllib.linalg.Vectors
  4. // Create some vector data; also works for sparse vectors
  5. val data = sc.parallelize(Array(Vectors.dense(1.0, 2.0, 3.0), Vectors.dense(4.0, 5.0, 6.0)))
  6. val transformingVector = Vectors.dense(0.0, 1.0, 2.0)
  7. val transformer = new ElementwiseProduct(transformingVector)
  8. // Batch transform and per-row transform give the same results:
  9. val transformedData = transformer.transform(data)
  10. val transformedData2 = data.map(x => transformer.transform(x))

  下面看transform的实现。

  1. override def transform(vector: Vector): Vector = {
  2. vector match {
  3. case dv: DenseVector =>
  4. val values: Array[Double] = dv.values.clone()
  5. val dim = scalingVec.size
  6. var i = 0
  7. while (i < dim) {
  8. //相对应的值相乘
  9. values(i) *= scalingVec(i)
  10. i += 1
  11. }
  12. Vectors.dense(values)
  13. case SparseVector(size, indices, vs) =>
  14. val values = vs.clone()
  15. val dim = values.length
  16. var i = 0
  17. while (i < dim) {
  18. //相对应的值相乘
  19. values(i) *= scalingVec(indices(i))
  20. i += 1
  21. }
  22. Vectors.sparse(size, indices, values)
  23. case v => throw new IllegalArgumentException("Does not support vector type " + v.getClass)
  24. }
  25. }