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

    https://github.com/apache/spark/pull/18538#discussion_r138021102
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/evaluation/ClusteringEvaluator.scala 
---
    @@ -0,0 +1,437 @@
    +/*
    + * 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.annotation.{Experimental, Since}
    +import org.apache.spark.broadcast.Broadcast
    +import org.apache.spark.ml.linalg.{BLAS, DenseVector, Vector, Vectors, 
VectorUDT}
    +import org.apache.spark.ml.param.{Param, ParamMap, ParamValidators}
    +import org.apache.spark.ml.param.shared.{HasFeaturesCol, HasPredictionCol}
    +import org.apache.spark.ml.util.{DefaultParamsReadable, 
DefaultParamsWritable, Identifiable, SchemaUtils}
    +import org.apache.spark.sql.{DataFrame, Dataset}
    +import org.apache.spark.sql.functions.{avg, col, udf}
    +import org.apache.spark.sql.types.IntegerType
    +
    +/**
    + * :: Experimental ::
    + * Evaluator for clustering results.
    + * The metric computes the Silhouette measure
    + * using the squared Euclidean distance.
    + *
    + * The Silhouette is a measure for the validation
    + * of the consistency within clusters. It ranges
    + * between 1 and -1, where a value close to 1
    + * means that the points in a cluster are close
    + * to the other points in the same cluster and
    + * far from the points of the other clusters.
    + */
    +@Experimental
    +@Since("2.3.0")
    +class ClusteringEvaluator @Since("2.3.0") (@Since("2.3.0") override val 
uid: String)
    +  extends Evaluator with HasPredictionCol with HasFeaturesCol with 
DefaultParamsWritable {
    +
    +  @Since("2.3.0")
    +  def this() = this(Identifiable.randomUID("cluEval"))
    +
    +  @Since("2.3.0")
    +  override def copy(pMap: ParamMap): ClusteringEvaluator = 
this.defaultCopy(pMap)
    +
    +  @Since("2.3.0")
    +  override def isLargerBetter: Boolean = true
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setPredictionCol(value: String): this.type = set(predictionCol, 
value)
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setFeaturesCol(value: String): this.type = set(featuresCol, value)
    +
    +  /**
    +   * param for metric name in evaluation
    +   * (supports `"squaredSilhouette"` (default))
    +   * @group param
    +   */
    +  @Since("2.3.0")
    +  val metricName: Param[String] = {
    +    val allowedParams = ParamValidators.inArray(Array("squaredSilhouette"))
    +    new Param(
    +      this,
    +      "metricName",
    +      "metric name in evaluation (squaredSilhouette)",
    +      allowedParams
    +    )
    +  }
    +
    +  /** @group getParam */
    +  @Since("2.3.0")
    +  def getMetricName: String = $(metricName)
    +
    +  /** @group setParam */
    +  @Since("2.3.0")
    +  def setMetricName(value: String): this.type = set(metricName, value)
    +
    +  setDefault(metricName -> "squaredSilhouette")
    +
    +  @Since("2.3.0")
    +  override def evaluate(dataset: Dataset[_]): Double = {
    +    SchemaUtils.checkColumnType(dataset.schema, $(featuresCol), new 
VectorUDT)
    +    SchemaUtils.checkColumnType(dataset.schema, $(predictionCol), 
IntegerType)
    +
    +    // Silhouette is reasonable only when the number of clusters is grater 
then 1
    +    assert(dataset.select($(predictionCol)).distinct().count() > 1,
    +      "Number of clusters must be greater than one.")
    +
    +    $(metricName) match {
    +      case "squaredSilhouette" => 
SquaredEuclideanSilhouette.computeSilhouetteScore(
    +        dataset,
    +        $(predictionCol),
    +        $(featuresCol)
    +      )
    +    }
    +  }
    +}
    +
    +
    +@Since("2.3.0")
    +object ClusteringEvaluator
    +  extends DefaultParamsReadable[ClusteringEvaluator] {
    +
    +  @Since("2.3.0")
    +  override def load(path: String): ClusteringEvaluator = super.load(path)
    +
    +}
    +
    +
    +/**
    + * SquaredEuclideanSilhouette computes the average of the
    + * Silhouette over all the data of the dataset, which is
    + * a measure of how appropriately the data have been clustered.
    + *
    + * The Silhouette for each point `i` is defined as:
    + *
    + * <blockquote>
    + *   $$
    + *   s_{i} = \frac{b_{i}-a_{i}}{max\{a_{i},b_{i}\}}
    + *   $$
    + * </blockquote>
    + *
    + * which can be rewritten as
    + *
    + * <blockquote>
    + *   $$
    + *   s_{i}= \begin{cases}
    + *   1-\frac{a_{i}}{b_{i}} & \text{if } a_{i} \leq b_{i} \\
    + *   \frac{b_{i}}{a_{i}}-1 & \text{if } a_{i} \gt b_{i} \end{cases}
    + *   $$
    + * </blockquote>
    + *
    + * where `$a_{i}$` is the average dissimilarity of `i` with all other data
    + * within the same cluster, `$b_{i}$` is the lowest average dissimilarity
    + * of `i` to any other cluster, of which `i` is not a member.
    + * `$a_{i}$` can be interpreted as as how well `i` is assigned to its 
cluster
    --- End diff --
    
    Remove duplicated ```as```.


---

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

Reply via email to