|
4 | 4 | import array
|
5 | 5 | import gc
|
6 | 6 |
|
7 |
| -def test_kmeans_two_clusters(): |
8 |
| - """ |
9 |
| - Data that is grouped into high/low should be clusterable into 2 |
10 |
| - """ |
| 7 | +def make_two_cluster_data(typecode): |
11 | 8 |
|
12 | 9 | n_features = 3
|
13 |
| - dataset = array.array('B', [ |
| 10 | + dataset = [ |
| 11 | + # cluster1 |
14 | 12 | 0, 0, 0,
|
15 | 13 | 10, 5, 2,
|
16 |
| - |
| 14 | + |
| 15 | + # cluster2 |
17 | 16 | 200, 50, 100,
|
18 | 17 | 255, 255, 255,
|
19 |
| - ]) |
| 18 | + ] |
20 | 19 |
|
21 |
| - centroids = array.array('B', [ |
| 20 | + centroids = [ |
22 | 21 | 0, 0, 0,
|
23 | 22 | 200, 200, 200,
|
24 |
| - ]) |
| 23 | + ] |
| 24 | + |
| 25 | + if typecode == 'bytearray': |
| 26 | + dataset = bytearray(dataset) |
| 27 | + centroids = bytearray(centroids) |
| 28 | + else: |
| 29 | + dataset = array.array(typecode, dataset) |
| 30 | + centroids = array.array(typecode, centroids) |
| 31 | + |
| 32 | + return dataset, centroids |
| 33 | + |
| 34 | + |
| 35 | +def test_kmeans_two_clusters(): |
| 36 | + """ |
| 37 | + Data that is grouped into high/low should be clusterable into 2 |
| 38 | + """ |
| 39 | + |
| 40 | + n_features = 3 |
| 41 | + # test both with "bytearray" and "array.array" |
| 42 | + for typecode in ['bytearray', 'B']: |
| 43 | + dataset, centroids = make_two_cluster_data(typecode) |
25 | 44 |
|
26 |
| - assignments = emlkmeans.cluster(dataset, centroids, channels=n_features) |
27 |
| - assert len(assignments) == len(dataset)/n_features |
28 |
| - assert list(assignments) == [0, 0, 1, 1], assignments |
| 45 | + assignments = emlkmeans.cluster(dataset, centroids, channels=n_features) |
| 46 | + assert len(assignments) == len(dataset)/n_features |
| 47 | + assert list(assignments) == [0, 0, 1, 1], assignments |
29 | 48 |
|
30 | 49 |
|
31 | 50 | test_kmeans_two_clusters()
|
0 commit comments