Skip to content

Commit aec217a

Browse files
DB Tsaimengxr
authored andcommitted
[MLlib] Use this.type as return type in k-means' builder pattern
to ensure that the return object is itself. Author: DB Tsai <[email protected]> Closes #1796 from dbtsai/dbtsai-kmeans and squashes the following commits: 658989e [DB Tsai] Alpine Data Labs (cherry picked from commit c7b5201) Signed-off-by: Xiangrui Meng <[email protected]>
1 parent 1da2fdf commit aec217a

File tree

1 file changed

+6
-6
lines changed
  • mllib/src/main/scala/org/apache/spark/mllib/clustering

1 file changed

+6
-6
lines changed

mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeans.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class KMeans private (
5252
def this() = this(2, 20, 1, KMeans.K_MEANS_PARALLEL, 5, 1e-4)
5353

5454
/** Set the number of clusters to create (k). Default: 2. */
55-
def setK(k: Int): KMeans = {
55+
def setK(k: Int): this.type = {
5656
this.k = k
5757
this
5858
}
5959

6060
/** Set maximum number of iterations to run. Default: 20. */
61-
def setMaxIterations(maxIterations: Int): KMeans = {
61+
def setMaxIterations(maxIterations: Int): this.type = {
6262
this.maxIterations = maxIterations
6363
this
6464
}
@@ -68,7 +68,7 @@ class KMeans private (
6868
* initial cluster centers, or "k-means||" to use a parallel variant of k-means++
6969
* (Bahmani et al., Scalable K-Means++, VLDB 2012). Default: k-means||.
7070
*/
71-
def setInitializationMode(initializationMode: String): KMeans = {
71+
def setInitializationMode(initializationMode: String): this.type = {
7272
if (initializationMode != KMeans.RANDOM && initializationMode != KMeans.K_MEANS_PARALLEL) {
7373
throw new IllegalArgumentException("Invalid initialization mode: " + initializationMode)
7474
}
@@ -83,7 +83,7 @@ class KMeans private (
8383
* return the best clustering found over any run. Default: 1.
8484
*/
8585
@Experimental
86-
def setRuns(runs: Int): KMeans = {
86+
def setRuns(runs: Int): this.type = {
8787
if (runs <= 0) {
8888
throw new IllegalArgumentException("Number of runs must be positive")
8989
}
@@ -95,7 +95,7 @@ class KMeans private (
9595
* Set the number of steps for the k-means|| initialization mode. This is an advanced
9696
* setting -- the default of 5 is almost always enough. Default: 5.
9797
*/
98-
def setInitializationSteps(initializationSteps: Int): KMeans = {
98+
def setInitializationSteps(initializationSteps: Int): this.type = {
9999
if (initializationSteps <= 0) {
100100
throw new IllegalArgumentException("Number of initialization steps must be positive")
101101
}
@@ -107,7 +107,7 @@ class KMeans private (
107107
* Set the distance threshold within which we've consider centers to have converged.
108108
* If all centers move less than this Euclidean distance, we stop iterating one run.
109109
*/
110-
def setEpsilon(epsilon: Double): KMeans = {
110+
def setEpsilon(epsilon: Double): this.type = {
111111
this.epsilon = epsilon
112112
this
113113
}

0 commit comments

Comments
 (0)