def test_respawn(self): m = 100 k = 5 X = np.random.randn(m, 2) centroids = np.zeros((k, 2)) centroids = pvml.kmeans_train(X, k, init_centroids=centroids) Z, _ = pvml.kmeans_inference(X, centroids) self.assertListEqual(np.unique(Z).tolist(), list(range(k)))
def test_one_per_class(self): for k in range(1, 12): with self.subTest(k): X = np.random.randn(k, 3) centroids = pvml.kmeans_train(X, k) Y, _ = pvml.kmeans_inference(X, centroids) self.assertListEqual( normalize_labels(Y).tolist(), list(range(k)))
def _linear(self, k, m): Y = np.arange(m) % k a = np.linspace(0, 2 * np.pi, m) X = np.stack([np.cos(a) + 10 * Y, np.sin(a)], 1) centroids = pvml.kmeans_train(X, k) Z, _ = pvml.kmeans_inference(X, centroids) self.assertListEqual( normalize_labels(Y).tolist(), normalize_labels(Z).tolist())
def test_init(self): m = 100 k = 5 X = np.random.randn(m, 2) init_centroids = np.arange(k * 2).reshape(k, 2) centroids = pvml.kmeans_train(X, k, init_centroids=init_centroids, steps=0) self.assertListEqual(init_centroids.tolist(), centroids.tolist())
def train_step(self, X, Y, steps): new_k = Y.max() + 1 if new_k > self.k: # If the classes change centroids are reset self.centroids = None self.k = new_k self.centroids = pvml.kmeans_train(X, self.k, steps=steps, init_centroids=self.centroids) self._sort_centroids(X, Y)
def test_errors4(self): X = np.random.randn(10, 2) with self.assertRaises(ValueError): pvml.kmeans_train(X, 11)
def test_errors3(self): X = np.random.randn(10, 2) with self.assertRaises(ValueError): pvml.kmeans_train(X, 3, init_centroids=np.zeros((3, 1)))