def test_predict(self): """Tests GaussianMixture.predict()""" x_train = np.array([[1, 2], [-1, -2], [2, 1], [-2, -1]]) ds_x_train = ds.array(x_train, block_size=(2, 2)) gm = GaussianMixture(n_components=2, random_state=666) gm.fit(ds_x_train) x_test = np.concatenate((x_train, [[2, 2], [-1, -3]])) ds_x_test = ds.array(x_test, block_size=(2, 2)) pred = gm.predict(ds_x_test).collect() self.assertTrue(pred[0] != pred[1]) self.assertTrue(pred[0] == pred[2] == pred[4]) self.assertTrue(pred[1] == pred[3] == pred[5])
def test_fit_predict_vs_fit_and_predict(self): """Tests GaussianMixture fit_predict() eq. fit() and predict() for both converged and not converged runs (and a fixed random_state).""" x0 = np.random.normal(size=(1000, 2)) x1 = np.random.normal(size=(2000, 2)) x0 = np.dot(x0, [[1.2, 1], [0, 0.5]]) + [0, 3] x1 = np.dot(x1, [[0.4, 0], [1, 2.5]]) + [1, 0] x = np.concatenate((x0, x1)) x_ds = ds.array(x, (1500, 2)) # We check the cases with and without convergence with warnings.catch_warnings(): warnings.simplefilter("ignore", ConvergenceWarning) for max_iter, converges in ((5, False), (100, True)): gm1 = GaussianMixture(n_components=2, max_iter=max_iter, random_state=0) gm1.fit(x_ds) labels1 = gm1.predict(x_ds) gm2 = GaussianMixture(n_components=2, max_iter=max_iter, random_state=0) labels2 = gm2.fit_predict(x_ds) self.assertTrue(np.all(labels1.collect() == labels2.collect())) self.assertEqual(gm1.n_iter, gm2.n_iter) self.assertEqual(converges, gm1.converged_) self.assertEqual(gm1.converged_, gm2.converged_) self.assertEqual(gm1.lower_bound_, gm2.lower_bound_) gm1.weights_ = compss_wait_on(gm1.weights_) gm1.means_ = compss_wait_on(gm1.means_) gm1.covariances_ = compss_wait_on(gm1.covariances_) gm2.weights_ = compss_wait_on(gm2.weights_) gm2.means_ = compss_wait_on(gm2.means_) gm2.covariances_ = compss_wait_on(gm2.covariances_) self.assertTrue(np.all(gm1.weights_ == gm2.weights_)) self.assertTrue(np.all(gm1.means_ == gm2.means_)) self.assertTrue(np.all(gm1.covariances_ == gm2.covariances_))