-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac0b2b7
commit 5864d4a
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
...ples/src/main/scala/org/apache/spark/examples/mllib/PowerIterationClusteringExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.{SparkConf, SparkContext} | ||
import org.apache.spark.mllib.clustering.KMeans | ||
import org.apache.spark.mllib.linalg.Vectors | ||
|
||
/** | ||
* An example k-means app. Run with | ||
* {{{ | ||
* ./bin/run-example org.apache.spark.examples.mllib.DenseKMeans [options] <input> | ||
* }}} | ||
* If you use it as a template to create your own app, please use `spark-submit` to submit your app. | ||
*/ | ||
object PowerIterationClusteringExample { | ||
|
||
object InitializationMode extends Enumeration { | ||
type InitializationMode = Value | ||
val Random, Parallel = Value | ||
} | ||
|
||
import InitializationMode._ | ||
|
||
case class Params( | ||
input: String = null, | ||
k: Int = -1, | ||
numIterations: Int = 10, | ||
initializationMode: InitializationMode = Parallel) extends AbstractParams[Params] | ||
|
||
def main(args: Array[String]) { | ||
val defaultParams = Params() | ||
|
||
val parser = new OptionParser[Params]("DenseKMeans") { | ||
head("DenseKMeans: an example k-means app for dense data.") | ||
opt[Int]('k', "k") | ||
.required() | ||
.text(s"number of clusters, required") | ||
.action((x, c) => c.copy(k = x)) | ||
opt[Int]("numIterations") | ||
.text(s"number of iterations, default; ${defaultParams.numIterations}") | ||
.action((x, c) => c.copy(numIterations = x)) | ||
opt[String]("initMode") | ||
.text(s"initialization mode (${InitializationMode.values.mkString(",")}), " + | ||
s"default: ${defaultParams.initializationMode}") | ||
.action((x, c) => c.copy(initializationMode = InitializationMode.withName(x))) | ||
arg[String]("<input>") | ||
.text("input paths to examples") | ||
.required() | ||
.action((x, c) => c.copy(input = x)) | ||
} | ||
|
||
parser.parse(args, defaultParams).map { params => | ||
run(params) | ||
}.getOrElse { | ||
sys.exit(1) | ||
} | ||
} | ||
|
||
def run(params: Params) { | ||
val conf = new SparkConf().setAppName(s"DenseKMeans with $params") | ||
val sc = new SparkContext(conf) | ||
|
||
Logger.getRootLogger.setLevel(Level.WARN) | ||
|
||
val examples = sc.textFile(params.input).map { line => | ||
Vectors.dense(line.split(' ').map(_.toDouble)) | ||
}.cache() | ||
|
||
val numExamples = examples.count() | ||
|
||
println(s"numExamples = $numExamples.") | ||
|
||
val initMode = params.initializationMode match { | ||
case Random => KMeans.RANDOM | ||
case Parallel => KMeans.K_MEANS_PARALLEL | ||
} | ||
|
||
val model = new KMeans() | ||
.setInitializationMode(initMode) | ||
.setK(params.k) | ||
.setMaxIterations(params.numIterations) | ||
.run(examples) | ||
|
||
val cost = model.computeCost(examples) | ||
|
||
println(s"Total cost = $cost.") | ||
|
||
sc.stop() | ||
} | ||
} |