|
| 1 | +package org.apache.spark.mllib.clustering |
| 2 | + |
| 3 | +import scala.collection.mutable.ArrayBuffer |
| 4 | +import scala.util.Random |
| 5 | + |
| 6 | +import org.scalatest.FunSuite |
| 7 | + |
| 8 | +import org.apache.spark.mllib.linalg.{Vectors, Vector} |
| 9 | +import org.apache.spark.mllib.util.TestingUtils._ |
| 10 | +import org.apache.spark.streaming.dstream.DStream |
| 11 | +import org.apache.spark.streaming.TestSuiteBase |
| 12 | + |
| 13 | +class StreamingKMeansSuite extends FunSuite with TestSuiteBase { |
| 14 | + |
| 15 | + override def maxWaitTimeMillis = 30000 |
| 16 | + |
| 17 | + test("accuracy for single center and equivalence to grand average") { |
| 18 | + |
| 19 | + // set parameters |
| 20 | + val numBatches = 10 |
| 21 | + val numPoints = 50 |
| 22 | + val k = 1 |
| 23 | + val d = 5 |
| 24 | + val r = 0.1 |
| 25 | + |
| 26 | + // create model with one cluster |
| 27 | + val model = new StreamingKMeans() |
| 28 | + .setK(1) |
| 29 | + .setDecayFactor(1.0) |
| 30 | + .setInitialCenters(Array(Vectors.dense(0.0, 0.0, 0.0, 0.0, 0.0))) |
| 31 | + |
| 32 | + // generate random data for kmeans |
| 33 | + val (input, centers) = StreamingKMeansDataGenerator(numPoints, numBatches, k, d, r, 42) |
| 34 | + |
| 35 | + // setup and run the model training |
| 36 | + val ssc = setupStreams(input, (inputDStream: DStream[Vector]) => { |
| 37 | + model.trainOn(inputDStream) |
| 38 | + inputDStream.count() |
| 39 | + }) |
| 40 | + runStreams(ssc, numBatches, numBatches) |
| 41 | + |
| 42 | + // estimated center should be close to true center |
| 43 | + assert(centers(0) ~== model.latestModel().clusterCenters(0) absTol 1E-1) |
| 44 | + |
| 45 | + // estimated center from streaming should exactly match the arithmetic mean of all data points |
| 46 | + val grandMean = input.flatten.map(x => x.toBreeze).reduce(_+_) / (numBatches * numPoints).toDouble |
| 47 | + assert(model.latestModel().clusterCenters(0) ~== Vectors.dense(grandMean.toArray) absTol 1E-5) |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + test("accuracy for two centers") { |
| 52 | + |
| 53 | + val numBatches = 10 |
| 54 | + val numPoints = 5 |
| 55 | + val k = 2 |
| 56 | + val d = 5 |
| 57 | + val r = 0.1 |
| 58 | + |
| 59 | + // create model with two clusters |
| 60 | + val model = new StreamingKMeans() |
| 61 | + .setK(2) |
| 62 | + .setDecayFactor(1.0) |
| 63 | + .setInitialCenters(Array(Vectors.dense(-0.1, 0.1, -0.2, -0.3, -0.1), |
| 64 | + Vectors.dense(0.1, -0.2, 0.0, 0.2, 0.1))) |
| 65 | + |
| 66 | + // generate random data for kmeans |
| 67 | + val (input, centers) = StreamingKMeansDataGenerator(numPoints, numBatches, k, d, r, 42) |
| 68 | + |
| 69 | + // setup and run the model training |
| 70 | + val ssc = setupStreams(input, (inputDStream: DStream[Vector]) => { |
| 71 | + model.trainOn(inputDStream) |
| 72 | + inputDStream.count() |
| 73 | + }) |
| 74 | + runStreams(ssc, numBatches, numBatches) |
| 75 | + |
| 76 | + // check that estimated centers are close to true centers |
| 77 | + // NOTE this depends on the initialization! allow for binary flip |
| 78 | + assert(centers(0) ~== model.latestModel().clusterCenters(0) absTol 1E-1) |
| 79 | + assert(centers(1) ~== model.latestModel().clusterCenters(1) absTol 1E-1) |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + test("drifting with fractional decay in units of batches") { |
| 84 | + |
| 85 | + val numBatches1 = 50 |
| 86 | + val numBatches2 = 50 |
| 87 | + val numPoints = 1 |
| 88 | + val q = 0.25 |
| 89 | + val k = 1 |
| 90 | + val d = 1 |
| 91 | + val r = 2.0 |
| 92 | + |
| 93 | + // create model with two clusters |
| 94 | + val model = new StreamingKMeans() |
| 95 | + .setK(1) |
| 96 | + .setDecayFractionBatches(q) |
| 97 | + .setInitialCenters(Array(Vectors.dense(0.0))) |
| 98 | + |
| 99 | + // create two batches of data with different, pre-specified centers |
| 100 | + // to simulate a transition from one cluster to another |
| 101 | + val (input1, centers1) = StreamingKMeansDataGenerator( |
| 102 | + numPoints, numBatches1, k, d, r, 42, initCenters = Array(Vectors.dense(100.0))) |
| 103 | + val (input2, centers2) = StreamingKMeansDataGenerator( |
| 104 | + numPoints, numBatches2, k, d, r, 84, initCenters = Array(Vectors.dense(0.0))) |
| 105 | + |
| 106 | + // store the history |
| 107 | + val history = new ArrayBuffer[Double](numBatches1 + numBatches2) |
| 108 | + |
| 109 | + // setup and run the model training |
| 110 | + val ssc = setupStreams(input1 ++ input2, (inputDStream: DStream[Vector]) => { |
| 111 | + model.trainOn(inputDStream) |
| 112 | + // extract the center (in this case one-dimensional) |
| 113 | + inputDStream.foreachRDD(x => history.append(model.latestModel().clusterCenters(0)(0))) |
| 114 | + inputDStream.count() |
| 115 | + }) |
| 116 | + runStreams(ssc, numBatches1 + numBatches2, numBatches1 + numBatches2) |
| 117 | + |
| 118 | + // check that the fraction of batches required to reach 50 |
| 119 | + // equals the setting of q, by finding the index of the first batch |
| 120 | + // below 50 and comparing to total number of batches received |
| 121 | + val halvedIndex = history.zipWithIndex.filter( x => x._1 < 50)(0)._2.toDouble |
| 122 | + val fraction = (halvedIndex - numBatches1.toDouble) / halvedIndex |
| 123 | + assert(fraction ~== q absTol 1E-1) |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + test("drifting with fractional decay in units of points") { |
| 128 | + |
| 129 | + val numBatches1 = 50 |
| 130 | + val numBatches2 = 50 |
| 131 | + val numPoints = 10 |
| 132 | + val q = 0.25 |
| 133 | + val k = 1 |
| 134 | + val d = 1 |
| 135 | + val r = 2.0 |
| 136 | + |
| 137 | + // create model with two clusters |
| 138 | + val model = new StreamingKMeans() |
| 139 | + .setK(1) |
| 140 | + .setDecayFractionPoints(q, numPoints) |
| 141 | + .setInitialCenters(Array(Vectors.dense(0.0))) |
| 142 | + |
| 143 | + // create two batches of data with different, pre-specified centers |
| 144 | + // to simulate a transition from one cluster to another |
| 145 | + val (input1, centers1) = StreamingKMeansDataGenerator( |
| 146 | + numPoints, numBatches1, k, d, r, 42, initCenters = Array(Vectors.dense(100.0))) |
| 147 | + val (input2, centers2) = StreamingKMeansDataGenerator( |
| 148 | + numPoints, numBatches2, k, d, r, 84, initCenters = Array(Vectors.dense(0.0))) |
| 149 | + |
| 150 | + // store the history |
| 151 | + val history = new ArrayBuffer[Double](numBatches1 + numBatches2) |
| 152 | + |
| 153 | + // setup and run the model training |
| 154 | + val ssc = setupStreams(input1 ++ input2, (inputDStream: DStream[Vector]) => { |
| 155 | + model.trainOn(inputDStream) |
| 156 | + // extract the center (in this case one-dimensional) |
| 157 | + inputDStream.foreachRDD(x => history.append(model.latestModel().clusterCenters(0)(0))) |
| 158 | + inputDStream.count() |
| 159 | + }) |
| 160 | + runStreams(ssc, numBatches1 + numBatches2, numBatches1 + numBatches2) |
| 161 | + |
| 162 | + // check that the fraction of batches required to reach 50 |
| 163 | + // equals the setting of q, by finding the index of the first batch |
| 164 | + // below 50 and comparing to total number of batches received |
| 165 | + val halvedIndex = history.zipWithIndex.filter( x => x._1 < 50)(0)._2.toDouble |
| 166 | + val fraction = (halvedIndex - numBatches1.toDouble) / halvedIndex |
| 167 | + assert(fraction ~== q absTol 1E-1) |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + def StreamingKMeansDataGenerator( |
| 172 | + numPoints: Int, |
| 173 | + numBatches: Int, |
| 174 | + k: Int, |
| 175 | + d: Int, |
| 176 | + r: Double, |
| 177 | + seed: Int, |
| 178 | + initCenters: Array[Vector] = null): (IndexedSeq[IndexedSeq[Vector]], Array[Vector]) = { |
| 179 | + val rand = new Random(seed) |
| 180 | + val centers = initCenters match { |
| 181 | + case null => Array.fill(k)(Vectors.dense(Array.fill(d)(rand.nextGaussian()))) |
| 182 | + case _ => initCenters |
| 183 | + } |
| 184 | + val data = (0 until numBatches).map { i => |
| 185 | + (0 until numPoints).map { idx => |
| 186 | + val center = centers(idx % k) |
| 187 | + Vectors.dense(Array.tabulate(d)(x => center(x) + rand.nextGaussian() * r)) |
| 188 | + } |
| 189 | + } |
| 190 | + (data, centers) |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | +} |
0 commit comments