aboutsummaryrefslogtreecommitdiff
path: root/mllib/src/test/scala/org/apache/spark/mllib/clustering/BisectingKMeansSuite.scala
blob: 41b9d5c0d93bb6d373ca7a3417045376a3b7d5eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * 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.mllib.clustering

import org.apache.spark.SparkFunSuite
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.util.MLlibTestSparkContext
import org.apache.spark.mllib.util.TestingUtils._

class BisectingKMeansSuite extends SparkFunSuite with MLlibTestSparkContext {

  test("default values") {
    val bkm0 = new BisectingKMeans()
    assert(bkm0.getK === 4)
    assert(bkm0.getMaxIterations === 20)
    assert(bkm0.getMinDivisibleClusterSize === 1.0)
    val bkm1 = new BisectingKMeans()
    assert(bkm0.getSeed === bkm1.getSeed, "The default seed should be constant.")
  }

  test("setter/getter") {
    val bkm = new BisectingKMeans()

    val k = 10
    assert(bkm.getK !== k)
    assert(bkm.setK(k).getK === k)
    val maxIter = 100
    assert(bkm.getMaxIterations !== maxIter)
    assert(bkm.setMaxIterations(maxIter).getMaxIterations === maxIter)
    val minSize = 2.0
    assert(bkm.getMinDivisibleClusterSize !== minSize)
    assert(bkm.setMinDivisibleClusterSize(minSize).getMinDivisibleClusterSize === minSize)
    val seed = 10L
    assert(bkm.getSeed !== seed)
    assert(bkm.setSeed(seed).getSeed === seed)

    intercept[IllegalArgumentException] {
      bkm.setK(0)
    }
    intercept[IllegalArgumentException] {
      bkm.setMaxIterations(0)
    }
    intercept[IllegalArgumentException] {
      bkm.setMinDivisibleClusterSize(0.0)
    }
  }

  test("1D data") {
    val points = Vectors.sparse(1, Array.empty, Array.empty) +:
      (1 until 8).map(i => Vectors.dense(i))
    val data = sc.parallelize(points, 2)
    val bkm = new BisectingKMeans()
      .setK(4)
      .setMaxIterations(1)
      .setSeed(1L)
    // The clusters should be
    // (0, 1, 2, 3, 4, 5, 6, 7)
    //   - (0, 1, 2, 3)
    //     - (0, 1)
    //     - (2, 3)
    //   - (4, 5, 6, 7)
    //     - (4, 5)
    //     - (6, 7)
    val model = bkm.run(data)
    assert(model.k === 4)
    // The total cost should be 8 * 0.5 * 0.5 = 2.0.
    assert(model.computeCost(data) ~== 2.0 relTol 1e-12)
    val predictions = data.map(v => (v(0), model.predict(v))).collectAsMap()
    Range(0, 8, 2).foreach { i =>
      assert(predictions(i) === predictions(i + 1),
        s"$i and ${i + 1} should belong to the same cluster.")
    }
    val root = model.root
    assert(root.center(0) ~== 3.5 relTol 1e-12)
    assert(root.height ~== 2.0 relTol 1e-12)
    assert(root.children.length === 2)
    assert(root.children(0).height ~== 1.0 relTol 1e-12)
    assert(root.children(1).height ~== 1.0 relTol 1e-12)
  }

  test("points are the same") {
    val data = sc.parallelize(Seq.fill(8)(Vectors.dense(1.0, 1.0)), 2)
    val bkm = new BisectingKMeans()
      .setK(2)
      .setMaxIterations(1)
      .setSeed(1L)
    val model = bkm.run(data)
    assert(model.k === 1)
  }

  test("more desired clusters than points") {
    val data = sc.parallelize(Seq.tabulate(4)(i => Vectors.dense(i)), 2)
    val bkm = new BisectingKMeans()
      .setK(8)
      .setMaxIterations(2)
      .setSeed(1L)
    val model = bkm.run(data)
    assert(model.k === 4)
  }

  test("min divisible cluster") {
    val data = sc.parallelize(
      Seq.tabulate(16)(i => Vectors.dense(i)) ++ Seq.tabulate(4)(i => Vectors.dense(-100.0 - i)),
      2)
    val bkm = new BisectingKMeans()
      .setK(4)
      .setMinDivisibleClusterSize(10)
      .setMaxIterations(1)
      .setSeed(1L)
    val model = bkm.run(data)
    assert(model.k === 3)
    assert(model.predict(Vectors.dense(-100)) === model.predict(Vectors.dense(-97)))
    assert(model.predict(Vectors.dense(7)) !== model.predict(Vectors.dense(8)))

    bkm.setMinDivisibleClusterSize(0.5)
    val sameModel = bkm.run(data)
    assert(sameModel.k === 3)
  }

  test("larger clusters get selected first") {
    val data = sc.parallelize(
      Seq.tabulate(16)(i => Vectors.dense(i)) ++ Seq.tabulate(4)(i => Vectors.dense(-100.0 - i)),
      2)
    val bkm = new BisectingKMeans()
      .setK(3)
      .setMaxIterations(1)
      .setSeed(1L)
    val model = bkm.run(data)
    assert(model.k === 3)
    assert(model.predict(Vectors.dense(-100)) === model.predict(Vectors.dense(-97)))
    assert(model.predict(Vectors.dense(7)) !== model.predict(Vectors.dense(8)))
  }

  test("2D data") {
    val points = Seq(
      (11, 10), (9, 10), (10, 9), (10, 11),
      (11, -10), (9, -10), (10, -9), (10, -11),
      (0, 1), (0, -1)
    ).map { case (x, y) =>
      if (x == 0) {
        Vectors.sparse(2, Array(1), Array(y))
      } else {
        Vectors.dense(x, y)
      }
    }
    val data = sc.parallelize(points, 2)
    val bkm = new BisectingKMeans()
      .setK(3)
      .setMaxIterations(4)
      .setSeed(1L)
    val model = bkm.run(data)
    assert(model.k === 3)
    assert(model.root.center ~== Vectors.dense(8, 0) relTol 1e-12)
    model.root.leafNodes.foreach { node =>
      if (node.center(0) < 5) {
        assert(node.size === 2)
        assert(node.center ~== Vectors.dense(0, 0) relTol 1e-12)
      } else if (node.center(1) > 0) {
        assert(node.size === 4)
        assert(node.center ~== Vectors.dense(10, 10) relTol 1e-12)
      } else {
        assert(node.size === 4)
        assert(node.center ~== Vectors.dense(10, -10) relTol 1e-12)
      }
    }
  }
}