Example #1
0
    def test_kmeans(self):
        labels, _centroids = kmeans.kmeans(DATA, 2, n_runs=5)
        labels = list(labels)

        # K-means is invariant by label permutations, hence we can always have two
        # different classifications
        assert labels == [0, 0, 0, 1, 1, 1] or labels == [1, 1, 1, 0, 0, 0]
Example #2
0
 def test_kmeans_convergence(self):
     # If no guidance is given, convergence is unlikely to happen
     labels, clusters = kmeans.kmeans(DATA, 2, max_iter=1, n_runs=2)
     assert (labels != [0, 0, 0, 1, 1, 1]).any()
Example #3
0
 def test_trivial_classification_if_number_of_clusters_equal_to_number_of_samples(
     self
 ):
     labels, clusters = kmeans.kmeans(DATA, len(DATA), n_runs=5)
     assert_equal(labels, range(len(DATA)))
     assert_equal(clusters, DATA)
Example #4
0
 def test_kmeans_requires_k_gt_n(self):
     with pytest.raises(ValueError):
         kmeans.kmeans(DATA, len(DATA) + 1, n_runs=5)