-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-5503][MLLIB] Example code for Power Iteration Clustering #4495
Changes from 9 commits
5864d4a
c509130
03e8de4
efeec45
f7ff43d
cef28f4
d7f0cba
d7ac350
2878675
3c84b14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove space after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
* n: Number of sampled points on innermost circle.. There are proportionally more points | ||
* within the outer/larger circles | ||
* numIterations: Number of Power Iterations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
* outerRadius: radius of the outermost of the concentric circles | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually not configurable. I'm okay with a fixed value, but the doc needs update. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yea.. added configuration param
|
||
* }}} | ||
* | ||
* 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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still use 2-space indentation here. 4-indentation only applies to method/class declarations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. err yes.. back to the drawing board for the IJ auto formatting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fyi I have created a Youtrack issue for Intellij for this formatting limitation: |
||
.action((x, c) => c.copy(k = x)) | ||
opt[Int]('n', "n") | ||
.text(s"number of points, default: ${defaultParams.numPoints}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. number of points for the smallest circle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
.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(",") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This line is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
val groupSizes = for (cx <- 0 until nCircles) yield (cx + 1) * nPoints | ||
var ix = 0 | ||
val points = for (cx <- 0 until nCircles; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
px <- 0 until groupSizes(cx)) yield { | ||
val theta = 2.0 * math.Pi * px / groupSizes(cx) | ||
val out = (ix, (radii(cx) * math.cos(theta), radii(cx) * math.sin(theta))) | ||
ix += 1 | ||
out | ||
} | ||
val rdd = sc.parallelize(points) | ||
val distancesRdd = rdd.cartesian(rdd).flatMap { case ((i0, (x0, y0)), (i1, (x1, y1))) => | ||
if (i0 < i1) { | ||
Some((i0.toLong, i1.toLong, similarity((x0, y0), (x1, y1)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} else { | ||
None | ||
} | ||
} | ||
distancesRdd | ||
} | ||
|
||
private[mllib] def similarity(p1: (Double, Double), p2: (Double, Double)) = { | ||
gaussianSimilarity(p1, p2, 1.0) | ||
} | ||
|
||
/** | ||
* Gaussian Similarity: http://en.wikipedia.org/wiki/Radial_basis_function_kernel | ||
*/ | ||
def gaussianSimilarity(p1: (Double, Double), p2: (Double, Double), sigma: Double) = { | ||
math.exp((p1._1 - p2._1) * (p1._1 - p2._1) + (p1._2 - p2._2) * (p1._2 - p2._2)) | ||
} | ||
|
||
case class Params( | ||
input: String = null, | ||
k: Int = 3, | ||
numPoints: Int = 5, | ||
numIterations: Int = 10, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
outerRadius: Double = 3.0 | ||
) extends AbstractParams[Params] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge this line to the one above. Maybe it is better to move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Merge this line to the one above" |
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update doc for
n
. It is also useful to put a link to the PIC paper.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK