Github user sethah commented on a diff in the pull request:
https://github.com/apache/spark/pull/15450#discussion_r84194269
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeans.scala ---
@@ -323,7 +324,10 @@ class KMeans private (
* Initialize a set of cluster centers at random.
*/
private def initRandom(data: RDD[VectorWithNorm]): Array[VectorWithNorm]
= {
- data.takeSample(true, k, new
XORShiftRandom(this.seed).nextInt()).map(_.toDense)
+ // Select without replacement; may still produce duplicates if the
data has < k distinct
+ // points, so deduplicate the centroids to match the behavior of
k-means|| in the same situation
+ data.takeSample(false, k, new XORShiftRandom(this.seed).nextInt()).
+ map(_.vector).distinct.map(new VectorWithNorm(_))
--- End diff --
I think we generally put the `.` on the next line, but I'm not sure it's a
strict style requirement.
Also, we end up recomputing the norm of each sample. For large `k` this can
be inefficient. I'm not sure of a non-ugly way to implement the distinct
otherwise.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]