spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mengxr <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-5503][MLLIB] Example code for Power Ite...
Date Wed, 11 Feb 2015 02:50:34 GMT
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4495#discussion_r24471466
  
    --- Diff: examples/src/main/scala/org/apache/spark/examples/mllib/PowerIterationClusteringExample.scala
---
    @@ -0,0 +1,149 @@
    +/*
    + * 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.examples.mllib
    +
    +import org.apache.log4j.{Level, Logger}
    +import scopt.OptionParser
    +
    +import org.apache.spark.mllib.clustering.PowerIterationClustering
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.{SparkConf, SparkContext}
    +
    +/**
    + * An example Power Iteration Clustering app.  Takes an input of K concentric circles
    + * with a total of "n" sampled points (total here means "across ALL of the circles").
    + * The output should be K clusters - each cluster containing precisely the points associated
    + * with each of the input circles.
    + *
    + * Run with
    + * {{{
    + * ./bin/run-example mllib.PowerIterationClusteringExample [options]
    + *
    + * Where options include:
    + *   k:  Number of circles/ clusters
    + *   n:  Number of sampled points on innermost circle.. There are proportionally more
points
    + *      within the outer/larger circles
    + *   numIterations:   Number of Power Iterations
    + *   outerRadius:  radius of the outermost of the concentric circles
    + * }}}
    + *
    + * Here is a sample run and output:
    + *
    + * ./bin/run-example mllib.PowerIterationClusteringExample
    + * -k 3 --n 30 --numIterations 15
    + *
    + * Cluster assignments: 1 -> [0,1,2,3,4],2 -> [5,6,7,8,9,10,11,12,13,14],
    + * 0 -> [15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
    + *
    + *
    + * If you use it as a template to create your own app, please use `spark-submit` to submit
your app.
    + */
    +object PowerIterationClusteringExample {
    +
    +  def main(args: Array[String]) {
    +    val defaultParams = Params()
    +
    +    val parser = new OptionParser[Params]("PIC Circles") {
    +      head("PowerIterationClusteringExample: an example PIC app using concentric circles.")
    +      opt[Int]('k', "k")
    +          .text(s"number of circles (/clusters), default: ${defaultParams.k}")
    +          .action((x, c) => c.copy(k = x))
    +      opt[Int]('n', "n")
    +          .text(s"number of points, default: ${defaultParams.numPoints}")
    +          .action((x, c) => c.copy(numPoints = x))
    +      opt[Int]("numIterations")
    +          .text(s"number of iterations, default: ${defaultParams.numIterations}")
    +          .action((x, c) => c.copy(numIterations = x))
    +    }
    +
    +    parser.parse(args, defaultParams).map { params =>
    +      run(params)
    +    }.getOrElse {
    +      sys.exit(1)
    +    }
    +  }
    +
    +  def run(params: Params) {
    +    val conf = new SparkConf()
    +        .setMaster("local")
    +        .setAppName(s"PowerIterationClustering with $params")
    +    val sc = new SparkContext(conf)
    +
    +    Logger.getRootLogger.setLevel(Level.WARN)
    +
    +    val circlesRdd = generateCirclesRdd(sc, params.k, params.numPoints, params.outerRadius)
    +    val model = new PowerIterationClustering()
    +        .setK(params.k)
    +        .setMaxIterations(params.numIterations)
    +        .run(circlesRdd)
    +
    +    val clusters = model.assignments.collect.groupBy(_._2).mapValues(_.map(_._1))
    +    val assignments = clusters.toList.sortBy { case (k, v) => v.length}
    +    val assignmentsStr = assignments
    +        .map { case (k, v) => s"$k -> ${v.sorted.mkString("[", ",", "]")}"}.mkString(",")
    +    println(s"Cluster assignments: $assignmentsStr")
    +
    +    sc.stop()
    +  }
    +
    +  def generateCirclesRdd(sc: SparkContext,
    +      nCircles: Int = 3,
    +      nPoints: Int = 30,
    +      outerRadius: Double): RDD[(Long, Long, Double)] = {
    +
    +    val radii = for (cx <- 0 until nCircles) yield outerRadius / (nCircles-cx)
    +    val groupSizes = for (cx <- 0 until nCircles) yield (cx + 1) * nPoints
    +    var ix = 0
    +    val points = for (cx <- 0 until nCircles;
    --- End diff --
    
    Same for `points` about `yield` usage. This line could be rewritten as
    
    ~~~scala
    def generateCircle(radius: Double, n: Int): Seq[(Double, Double)] = {
      val theta = 2.0 * math.Pi / n
      Seq.tabulate(n)(i => (radius * math.cos(i * theta), radius * math.sin(i * theta)))
    }
    
    val points = (0 until nCircles).flatMap { i =>
      generateCircle(radii(i), groupSizes(i))
    }.zipWithIndex
    ~~~
    
    which is easier to read.


---
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