def test_all_distances(self): coords1 = [[0, 0, 0], [0.5, 0.5, 0.5]] coords2 = [[1, 2, -1], [1, 0, 0], [1, 0, 0]] result = [[2.44948974, 1, 1], [2.17944947, 0.8660254, 0.8660254]] self.assertArrayAlmostEqual(all_distances(coords1, coords2), result, 4)
def get_labels(self, points, centroids): # For each element in the dataset, chose the closest centroid. # Make that centroid the element's label. dists = all_distances(points, centroids) min_dists = np.min(dists, axis=1) return np.where(dists == min_dists[:, None])[1], np.sum(min_dists ** 2)