@Test
public void testKMeansClustering() {
ArrayList<DoubleVector> lst = getClusteringInput();
KMeansClustering clusterer = new KMeansClustering(2, lst, false);
EuclidianDistance dist = new EuclidianDistance();
List<Cluster> assignments = clusterer.cluster(100, dist, 0.1d, false);
DoubleVector[] centers = clusterer.getCenters();
assertEquals(49.5, centers[0].get(0), 1e-4);
assertEquals(24.5, centers[0].get(1), 1e-4);
assertEquals(49.5, centers[1].get(0), 1e-4);
assertEquals(74.5, centers[1].get(1), 1e-4);
// the centers should partition the space in half
assertEquals(2, assignments.size());
assertEquals(5000, assignments.get(0).getAssignments().size());
assertEquals(5000, assignments.get(1).getAssignments().size());
// now verify the assignments
for (DoubleVector v : assignments.get(0).getAssignments()) {
double distRightCenter = dist.measureDistance(v, centers[0]);
double distOtherCenter = dist.measureDistance(v, centers[1]);
assertTrue(distRightCenter < distOtherCenter);
}
for (DoubleVector v : assignments.get(1).getAssignments()) {
double distRightCenter = dist.measureDistance(v, centers[1]);
double distOtherCenter = dist.measureDistance(v, centers[0]);
assertTrue(distRightCenter < distOtherCenter);
}
}