Github user freeman-lab commented on a diff in the pull request:
https://github.com/apache/spark/pull/6499#discussion_r32751544
--- Diff: python/pyspark/mllib/tests.py ---
@@ -863,6 +876,107 @@ def test_model_transform(self):
eprod.transform(sparsevec), SparseVector(3, [0], [3]))
+class StreamingKMeansTest(MLLibStreamingTestCase):
+ def test_model_params(self):
+ stkm = StreamingKMeans()
+ stkm.setK(5).setDecayFactor(0.0)
+ self.assertEquals(stkm._k, 5)
+ self.assertEquals(stkm._decayFactor, 0.0)
+
+ # Model not set yet.
+ self.assertIsNone(stkm.latestModel)
+ self.assertRaises(ValueError, stkm.trainOn, [0.0, 1.0])
+
+ stkm.setInitialCenters([[0.0, 0.0], [1.0, 1.0]], [1.0, 1.0])
+ self.assertEquals(stkm.latestModel.centers, [[0.0, 0.0], [1.0,
1.0]])
+ self.assertEquals(stkm.latestModel.getClusterWeights, [1.0, 1.0])
+
+ @staticmethod
+ def _ssc_wait(start_time, end_time, sleep_time):
+ while time() - start_time < end_time:
+ sleep(0.01)
+
+ def test_accuracy_for_single_center(self):
+ numBatches, numPoints, k, d, r, seed = 5, 5, 1, 5, 0.1, 0
+ centers, batches = self.streamingKMeansDataGenerator(
+ numBatches, numPoints, k, d, r, seed)
+ stkm = StreamingKMeans(1)
+ stkm.setInitialCenters([[0., 0., 0., 0., 0.]], [0.])
+ input_stream = self.ssc.queueStream(
+ [self.sc.parallelize(batch, 1) for batch in batches])
+ stkm.trainOn(input_stream)
+ t = time()
+ self.ssc.start()
+ self._ssc_wait(t, 10.0, 0.01)
+ self.assertEquals(stkm.latestModel.getClusterWeights, [25.0])
+ realCenters = array_sum(array(centers), axis=0)
+ for i in range(d):
+ modelCenters = stkm.latestModel.centers[0][i]
+ self.assertAlmostEqual(centers[0][i], modelCenters, 1)
+ self.assertAlmostEqual(realCenters[i], modelCenters, 1)
+
+ def streamingKMeansDataGenerator(self, batches, numPoints,
+ k, d, r, seed, centers=None):
+ rng = random.RandomState(seed)
+
+ # Generate centers.
+ centers = [rng.randn(d) for i in range(k)]
+
+ return centers, [[Vectors.dense(centers[j % k] + r * rng.randn(d))
+ for j in range(numPoints)]
+ for i in range(batches)]
+
+ def test_trainOn_model(self):
+ # Test the model on toy data with four clusters.
+ stkm = StreamingKMeans()
+ initCenters = [[1.0, 1.0], [-1.0, 1.0], [-1.0, -1.0], [1.0, -1.0]]
+ weights = [1.0, 1.0, 1.0, 1.0]
+ stkm.setInitialCenters(initCenters, weights)
+
+ # Create a toy dataset by setting a tiny offest for each point.
+ offsets = [[0, 0.1], [0, -0.1], [0.1, 0], [-0.1, 0]]
+ batches = []
+ for offset in offsets:
+ batches.append([[offset[0] + center[0], offset[1] + center[1]]
+ for center in initCenters])
+
+ batches = [self.sc.parallelize(batch, 1) for batch in batches]
+ input_stream = self.ssc.queueStream(batches)
+ stkm.trainOn(input_stream)
+ t = time()
+ self.ssc.start()
+
+ # Give enough time to train the model.
+ self._ssc_wait(t, 6.0, 0.01)
+ finalModel = stkm.latestModel
+ self.assertTrue(all(finalModel.centers == array(initCenters)))
+ self.assertEquals(finalModel.getClusterWeights, [5.0, 5.0, 5.0,
5.0])
+
+ def test_predictOn_model(self):
+ initCenters = [[1.0, 1.0], [-1.0, 1.0], [-1.0, -1.0], [1.0, -1.0]]
+ weights = [1.0, 1.0, 1.0, 1.0]
+ stkm = StreamingKMeans()
+ stkm.latestModel = StreamingKMeansModel(initCenters, weights)
+
+ predict_data = [[[1.5, 1.5]], [[-1.5, 1.5]], [[-1.5, -1.5]],
[[1.5, -1.5]]]
+ predict_data = [sc.parallelize(batch, 1) for batch in predict_data]
+ predict_stream = self.ssc.queueStream(predict_data)
+ predict_val = stkm.predictOn(predict_stream)
+
+ result = []
+
+ def update(rdd):
+ rdd_collect = rdd.collect()
+ if rdd_collect:
+ result.append(rdd_collect)
+
+ predict_val.foreachRDD(update)
+ t = time()
+ self.ssc.start()
+ self._ssc_wait(t, 6.0, 0.01)
+ self.assertEquals(result, [[0], [1], [2], [3]])
+
+
--- End diff --
Maybe add a test that combines training and prediction? Specifically, show
that predictions are incorrect at the beginning and correct at the end, because
the training correctly updates the model. We had a bug related to this early on
(now fixed) on the scala side, would be good to catch anything similar here
too!
---
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]