Github user yanboliang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/18538#discussion_r131889868
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/evaluation/SquaredEuclideanSilhouette.scala
 ---
    @@ -0,0 +1,115 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.ml.evaluation
    +
    +import org.apache.spark.SparkContext
    +import org.apache.spark.broadcast.Broadcast
    +import org.apache.spark.ml.linalg.{Vector, VectorElementWiseSum}
    +import org.apache.spark.sql.DataFrame
    +import org.apache.spark.sql.functions.{col, count, sum}
    +
    +private[evaluation] object SquaredEuclideanSilhouette {
    +
    +  private[this] var kryoRegistrationPerformed: Boolean = false
    +
    +  /**
    +   * This method registers the class
    +   * 
[[org.apache.spark.ml.evaluation.SquaredEuclideanSilhouette.ClusterStats]]
    +   * for kryo serialization.
    +   *
    +   * @param sc `SparkContext` to be used
    +   */
    +  def registerKryoClasses(sc: SparkContext): Unit = {
    +    if (! kryoRegistrationPerformed) {
    +      sc.getConf.registerKryoClasses(
    +        Array(
    +          classOf[SquaredEuclideanSilhouette.ClusterStats]
    +        )
    +      )
    +      kryoRegistrationPerformed = true
    +    }
    +  }
    +
    +  case class ClusterStats(Y: Vector, psi: Double, count: Long)
    +
    +  def computeCsi(vector: Vector): Double = {
    +    var sumOfSquares = 0.0
    +    vector.foreachActive((_, v) => {
    +      sumOfSquares += v * v
    +    })
    +    sumOfSquares
    +  }
    +
    +  def computeYVectorPsiAndCount(
    +      df: DataFrame,
    +      predictionCol: String,
    +      featuresCol: String): DataFrame = {
    +    val Yudaf = new VectorElementWiseSum()
    +    df.groupBy(predictionCol)
    +      .agg(
    +        count("*").alias("count"),
    +        sum("csi").alias("psi"),
    +        Yudaf(col(featuresCol)).alias("y")
    +      )
    --- End diff --
    
    Aggregate function performance is not ideal for column of non-primitive 
type(like here is vector type). So we would still use RDD-based aggregate. You 
can factor this part of code following 
[```NaiveBayes```](https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/ml/classification/NaiveBayes.scala#L161)
 like:
    ```
        import org.apache.spark.ml.linalg.{BLAS, DenseVector, Vectors}
        import org.apache.spark.sql.functions._
        
        val numFeatures = ...
        val squaredNorm = udf { features: Vector => 
math.pow(Vectors.norm(features, 2.0), 2.0) }
    
        df.select(col(predictionCol), col(featuresCol))
          .withColumn("squaredNorm", squaredNorm(col(featuresCol)))
          .rdd
          .map { row => (row.getDouble(0), (row.getAs[Vector](1), 
row.getDouble(2))) }
          .aggregateByKey[(DenseVector, 
Double)]((Vectors.zeros(numFeatures).toDense, 0.0))(
          seqOp = {
            case ((featureSum: DenseVector, squaredNormSum: Double), (features, 
squaredNorm)) =>
              BLAS.axpy(1.0, features, featureSum)
              (featureSum, squaredNormSum + squaredNorm)
          },
          combOp = {
            case ((featureSum1, squaredNormSum1), (featureSum2, 
squaredNormSum2)) =>
              BLAS.axpy(1.0, featureSum2, featureSum1)
              (featureSum1, squaredNormSum1 + squaredNormSum2)
          }).collect()
    ```
    In my suggestion, you can compute ```csi``` and ```y``` in a single data 
pass, which should be more efficient.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to