spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From freeman-lab <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-6517][mllib] Implement the Algorithm of...
Date Mon, 27 Apr 2015 05:20:32 GMT
Github user freeman-lab commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5267#discussion_r29121153
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/clustering/HierarchicalClustering.scala
---
    @@ -0,0 +1,574 @@
    +/*
    + * 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.mllib.clustering
    +
    +import breeze.linalg.{DenseVector => BDV, SparseVector => BSV, Vector => BV,
norm => breezeNorm}
    +import org.apache.spark.mllib.linalg.{Vector, Vectors}
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.util.random.XORShiftRandom
    +import org.apache.spark.{Logging, SparkException}
    +
    +import scala.collection.{Map, mutable}
    +
    +
    +object HierarchicalClustering extends Logging {
    +
    +  private[clustering] val ROOT_INDEX_KEY: Long = 1
    +
    +  /**
    +   * Finds the closes cluster's center
    +   *
    +   * @param metric a distance metric
    +   * @param centers centers of the clusters
    +   * @param point a target point
    +   * @return an index of the array of clusters
    +   */
    +  private[mllib]
    +  def findClosestCenter(metric: Function2[BV[Double], BV[Double], Double])
    +        (centers: Seq[BV[Double]])(point: BV[Double]): Int = {
    +    val (closestCenter, closestIndex) =
    +      centers.zipWithIndex.map { case (center, idx) => (metric(center, point), idx)}.minBy(_._1)
    +    closestIndex
    +  }
    +}
    +
    +/**
    + * This is a divisive hierarchical clustering algorithm based on bi-sect k-means algorithm.
    + *
    + * The main idea of this algorithm is based on "A comparison of document clustering techniques",
    + * M. Steinbach, G. Karypis and V. Kumar. Workshop on Text Mining, KDD, 2000.
    + * http://cs.fit.edu/~pkc/classes/ml-internet/papers/steinbach00tr.pdf
    + *
    + * @param numClusters tne number of clusters you want
    + * @param clusterMap the pairs of cluster and its index as Map
    + * @param maxIterations the number of maximal iterations
    + * @param maxRetries the number of maximum retries
    + * @param seed a random seed
    + */
    +class HierarchicalClustering private (
    +  private var numClusters: Int,
    +  private var clusterMap: Map[Long, ClusterTree],
    +  private var maxIterations: Int,
    +  private var maxRetries: Int,
    +  private var seed: Long) extends Logging {
    +
    +  /**
    +   * Constructs with the default configuration
    +   */
    +  def this() = this(20, mutable.ListMap.empty[Long, ClusterTree], 20, 10, 1)
    +
    +  /**
    +   * Sets the number of clusters you want
    +   */
    +  def setNumClusters(numClusters: Int): this.type = {
    +    this.numClusters = numClusters
    +    this
    +  }
    +
    +  def getNumClusters: Int = this.numClusters
    +
    +  /**
    +   * Sets the number of maximal iterations in each clustering step
    +   */
    +  def setMaxIterations(maxIterations: Int): this.type = {
    +    this.maxIterations = maxIterations
    +    this
    +  }
    +
    +  def getSubIterations: Int = this.maxIterations
    +
    +  /**
    +   * Sets the number of maximum retries of each clustering step
    +   */
    +  def setMaxRetries(maxRetries: Int): this.type = {
    +    this.maxRetries = maxRetries
    +    this
    +  }
    +
    +  def getMaxRetries: Int = this.maxRetries
    +
    +  /**
    +   * Sets the random seed
    +   */
    +  def setSeed(seed: Long): this.type = {
    +    this.seed = seed
    +    this
    +  }
    +
    +  def getSeed: Long = this.seed
    +
    +  /**
    +   * Runs the hierarchical clustering algorithm
    +   * @param input RDD of vectors
    +   * @return model for the hierarchical clustering
    +   */
    +  def run(input: RDD[Vector]): HierarchicalClusteringModel = {
    +    val sc = input.sparkContext
    +    log.info(s"${sc.appName} starts a hierarchical clustering algorithm")
    +
    +    var data = initData(input).cache()
    +    val startTime = System.currentTimeMillis()
    +
    +    // `clusters` is described as binary tree structure
    +    // `clusters(1)` means the root of a binary tree
    +    var clusters = summarizeAsClusters(data)
    +    var leafClusters = clusters
    +    var step = 1
    +    var numDividedClusters = 0
    +    var noMoreDividable = false
    +    var rddArray = Array.empty[RDD[(Long, BV[Double])]]
    +    // the number of maximum nodes of a binary tree by given parameter
    +    val multiplier = math.ceil(math.log10(this.numClusters) / math.log10(2.0)) + 1
    +    val maxAllNodesInTree = math.pow(2, multiplier).toInt
    +
    +    while (clusters.size < maxAllNodesInTree && noMoreDividable == false)
{
    +      log.info(s"${sc.appName} starts step ${step}")
    +
    +      // enough to be clustered if the number of divided clusters is equal to 0
    +      val divided = getDividedClusters(data, leafClusters)
    +      if (divided.size == 0) {
    +        noMoreDividable = true
    +      }
    +      else {
    +        // update each index
    +        val newData = updateClusterIndex(data, divided).cache()
    +        rddArray = rddArray ++ Array(data)
    +        data = newData
    +
    +        // keep recent 2 cached RDDs in order to run more quickly
    +        if (rddArray.size > 1) {
    +          val head = rddArray.head
    +          head.unpersist()
    +          rddArray = rddArray.filterNot(_.hashCode() == head.hashCode())
    +        }
    +
    +        // merge the divided clusters with the map as the cluster tree
    +        clusters = clusters ++ divided
    +        numDividedClusters = data.map(_._1).distinct().count().toInt
    +        leafClusters = divided
    +        step += 1
    +
    +        log.info(s"${sc.appName} adding ${divided.size} new clusters at step:${step}")
    +      }
    +    }
    +    // unpersist kept RDDs
    +    rddArray.foreach(_.unpersist())
    +
    +    // build a cluster tree by Map class which is expressed
    +    log.info(s"Building the cluster tree is started in ${sc.appName}")
    +    val root = buildTree(clusters, HierarchicalClustering.ROOT_INDEX_KEY, this.numClusters)
    +    if (root == None) {
    +      new SparkException("Failed to build a cluster tree from a Map type of clusters")
    +    }
    +
    +    // set the elapsed time for training
    +    val finishTime = (System.currentTimeMillis() - startTime) / 1000.0
    +    log.info(s"Elapsed Time for Hierarchical Clustering Training: ${finishTime} [sec]")
    +
    +    // make a hierarchical clustering model
    +    val model = new HierarchicalClusteringModel(root.get)
    +    val leavesNodes = model.getClusters()
    +    if (leavesNodes.size < this.numClusters) {
    +      log.warn(s"# clusters is less than you have expected: ${leavesNodes.size} / ${numClusters}.
")
    +    }
    +    model
    +  }
    +
    +  /**
    +   * Assigns the initial cluster index id to all data
    +   */
    +  private[clustering]
    +  def initData(data: RDD[Vector]): RDD[(Long, BV[Double])] = {
    +    data.map { v: Vector => (HierarchicalClustering.ROOT_INDEX_KEY, v.toBreeze)}.cache
    +  }
    +
    +  /**
    +   * Summarizes data by each cluster as ClusterTree2 classes
    --- End diff --
    
    What is "ClusterTree2"? Did you mean "ClusterTree"?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message