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

    https://github.com/apache/spark/pull/18538#discussion_r136306135
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/ml/evaluation/ClusteringEvaluator.scala 
---
    @@ -0,0 +1,379 @@
    +/*
    + * 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.ParamMap
    +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
    +
    +/**
    + * 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.
    + *
    + * The implementation follows the proposal explained
    + * <a 
href="https://drive.google.com/file/d/0B0Hyo%5f%5fbG%5f3fdkNvSVNYX2E3ZU0/view";>
    + * in this document</a>.
    + */
    +@Experimental
    +@Since("2.3.0")
    +class ClusteringEvaluator (val uid: String)
    +  extends Evaluator with HasPredictionCol with HasFeaturesCol with 
DefaultParamsWritable {
    +
    +  def this() = this(Identifiable.randomUID("cluEval"))
    +
    +  override def copy(pMap: ParamMap): ClusteringEvaluator = 
this.defaultCopy(pMap)
    +
    +  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)
    +
    +  @Since("2.3.0")
    +  override def evaluate(dataset: Dataset[_]): Double = {
    +    SchemaUtils.checkColumnType(dataset.schema, $(featuresCol), new 
VectorUDT)
    +    SchemaUtils.checkColumnType(dataset.schema, $(predictionCol), 
IntegerType)
    +
    +    SquaredEuclideanSilhouette.computeSilhouetteScore(
    +      dataset,
    +      $(predictionCol),
    +      $(featuresCol)
    +    )
    +  }
    +}
    +
    +
    +object ClusteringEvaluator
    +  extends DefaultParamsReadable[ClusteringEvaluator] {
    +
    +  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}=\left\{ \begin{tabular}{cc}
    + *   $1-\frac{a_{i}}{b_{i}}$ & if $a_{i} \leq b_{i}$ \\
    + *   $\frac{b_{i}}{a_{i}}-1$ & if $a_{i} \gt b_{i}$
    + * </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 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
    + * (the smaller the value, the better the assignment), while `b(i)` is
    + * a measure of how well `i` has not been assigned to its "neighboring 
cluster",
    + * ie. the nearest cluster to `i`.
    + *
    + * Unfortunately, the naive implementation of the algorithm requires to 
compute
    + * the distance of each couple of points in the dataset. Since the 
computation of
    + * the distance measure takes `D` operations - if `D` is the number of 
dimensions
    + * of each point, the computational complexity of the algorithm is 
`O(N^2*D)`, where
    + * `N` is the cardinality of the dataset. Of course this is not scalable 
in `N`,
    + * which is the critical number in a Big Data context.
    + *
    + * The algorithm which is implemented in this object, instead, is an 
efficient
    + * and parallel implementation of the Silhouette using the squared 
Euclidean
    + * distance measure.
    + *
    + * With this assumption, the average of the distance of the point `X`
    + * to the points `C_{i}` belonging to the cluster `\Gamma` is:
    + *
    + * <blockquote>
    + *   \sum\limits_{i=1}^N d(X, C_{i} )^2 =
    + *   \sum\limits_{i=1}^N \Big( \sum\limits_{j=1}^D (x_{j}-c_{ij})^2 \Big)
    + *   = \sum\limits_{i=1}^N \Big( \sum\limits_{j=1}^D x_{j}^2 +
    + *   \sum\limits_{j=1}^D c_{ij}^2 -2\sum\limits_{j=1}^D x_{i}c_{ij} \Big)
    + *   = \sum\limits_{i=1}^N \sum\limits_{j=1}^D x_{j}^2 +
    + *   \sum\limits_{i=1}^N \sum\limits_{j=1}^D c_{ij}^2
    + *   -2 \sum\limits_{i=1}^N \sum\limits_{j=1}^D x_{i}c_{ij}
    + * </blockquote>
    + *
    + * where `x_{j}` is the `j`-th dimension of the point `X` and
    + * `c_{ij} is the `j`-th dimension of the `i`-th point in cluster `\Gamma`.
    + *
    + * Then, the first term of the equation can be rewritten as:
    + *
    + * <blockquote>
    + *   \sum\limits_{i=1}^N \sum\limits_{j=1}^D x_{j}^2 = N \xi_{X} ,
    + *   with \xi_{X} = \sum\limits_{j=1}^D x_{j}^2
    + * </blockquote>
    + *
    + * where `\xi_{X}` is fixed for each point and it can be precomputed.
    + *
    + * Moreover, the second term is fixed for each cluster too,
    + * thus we can name it `\Psi_{\Gamma}`
    + *
    + * <blockquote>
    + *   sum\limits_{i=1}^N \sum\limits_{j=1}^D c_{ij}^2 =
    --- End diff --
    
    Ditto, there is syntax error in this latex formula.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to