-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-8018][MLlib]KMeans should accept initial cluster centers as param #6737
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
Changes from 14 commits
6959861
e9c35d7
16f1b53
cd5dc5c
3f5fc8e
582e6d9
60c8ce2
714acb5
242ead1
e721dfe
07f8554
d12336e
06d13ef
c446c58
ef95ee2
94b56df
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 |
|---|---|---|
|
|
@@ -156,6 +156,21 @@ class KMeans private ( | |
| this | ||
| } | ||
|
|
||
| // Initial cluster centers can be provided as a KMeansModel object rather than using the | ||
| // random or k-means|| initializationMode | ||
| private var initialModel: Option[KMeansModel] = None | ||
|
|
||
| /** | ||
| * Set the initial starting point, bypassing the random initialization or k-means|| | ||
| * The condition model.k == this.k must be met, failure results | ||
| * in an IllegalArgumentException. | ||
| */ | ||
| def setInitialModel(model: KMeansModel): this.type = { | ||
| require(model.k == k, "mismatched cluster count") | ||
| initialModel = Some(model) | ||
| this | ||
| } | ||
|
|
||
| /** | ||
| * Train a K-means model on the given set of points; `data` should be cached for high | ||
| * performance, because this is an iterative algorithm. | ||
|
|
@@ -193,20 +208,33 @@ class KMeans private ( | |
|
|
||
| val initStartTime = System.nanoTime() | ||
|
|
||
| val centers = if (initializationMode == KMeans.RANDOM) { | ||
| initRandom(data) | ||
| } else { | ||
| initKMeansParallel(data) | ||
| } | ||
| // Only one run is allowed when initialModel is given | ||
| val numRuns = if (initialModel.nonEmpty){ | ||
| if (runs >1 ) logWarning("Ignoring runs; one run is allowed when initialModel is given.") | ||
|
Member
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. Please be careful about Scala style. Look at [https://cwiki.apache.org/confluence/display/SPARK/Spark+Code+Style+Guide] or other parts of the codebase for examples. We try hard to keep a consistent style. Here:
Contributor
Author
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. @jkbradley the if statement is given 2 space indentation..tat's correct rgt?
Member
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. That's correct.
Member
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. Please replace your current code with the snippet I wrote above (defining numRuns). The current code does not follow the style guide. |
||
| 1 | ||
| } else runs | ||
|
|
||
|
|
||
| val centers = initialModel match { | ||
| case Some(kMeansCenters) => { | ||
| Array(kMeansCenters.clusterCenters.map(s => new VectorWithNorm(s))) | ||
| } | ||
| case None => { | ||
| if (initializationMode == KMeans.RANDOM) { | ||
| initRandom(data) | ||
| } else { | ||
| initKMeansParallel(data) | ||
| } | ||
| } | ||
| } | ||
| val initTimeInSeconds = (System.nanoTime() - initStartTime) / 1e9 | ||
| logInfo(s"Initialization with $initializationMode took " + "%.3f".format(initTimeInSeconds) + | ||
| " seconds.") | ||
|
|
||
| val active = Array.fill(runs)(true) | ||
| val costs = Array.fill(runs)(0.0) | ||
| val active = Array.fill(numRuns)(true) | ||
| val costs = Array.fill(numRuns)(0.0) | ||
|
|
||
| var activeRuns = new ArrayBuffer[Int] ++ (0 until runs) | ||
| var activeRuns = new ArrayBuffer[Int] ++ (0 until numRuns) | ||
| var iteration = 0 | ||
|
|
||
| val iterationStartTime = System.nanoTime() | ||
|
|
||
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.
I'm just wondering out loud, don't know if this makes sense -- should the user have to supply a whole model just to specify initial centroids? or can they just specify the centroids here?
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.
@srowen Thanks for the comments. We followed this approach based on @mengxr 's suggestion at https://issues.apache.org/jira/browse/SPARK-8018?focusedCommentId=14571757&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14571757. Please have a look.
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.
Oh I see, that's fine.