Beispiel #1
0
 def test_init_random(self):
     """ Tests GaussianMixture random initialization """
     x = ds.random_array((50, 3), (10, 3), random_state=0)
     gm = GaussianMixture(init_params='random', n_components=4,
                          arity=2, random_state=170)
     gm.fit(x)
     self.assertGreater(gm.n_iter, 5)
Beispiel #2
0
    def test_fit(self):
        """Tests GaussianMixture.fit()"""

        x = np.array([[1, 2], [2, 1], [-3, -3], [-1, -2], [-2, -1], [3, 3]])
        ds_x = ds.array(x, block_size=(3, 2))

        gm = GaussianMixture(n_components=2, random_state=666)
        gm.fit(ds_x)

        expected_weights = np.array([0.5, 0.5])
        expected_means = np.array([[-2, -2], [2, 2]])
        expected_cov = np.array([[[0.66671688, 0.33338255],
                                  [0.33338255, 0.66671688]],

                                 [[0.66671688, 0.33338255],
                                  [0.33338255, 0.66671688]]])
        expected_pc = np.array([[[1.22469875, -0.70714834],
                                 [0., 1.4141944]],

                                [[1.22469875, -0.70714834],
                                 [0., 1.4141944]]])

        gm.weights_ = compss_wait_on(gm.weights_)
        gm.means_ = compss_wait_on(gm.means_)
        gm.covariances_ = compss_wait_on(gm.covariances_)
        gm.precisions_cholesky_ = compss_wait_on(gm.precisions_cholesky_)

        self.assertTrue((np.allclose(gm.weights_, expected_weights)))
        self.assertTrue((np.allclose(gm.means_, expected_means)))
        self.assertTrue((np.allclose(gm.covariances_, expected_cov)))
        self.assertTrue((np.allclose(gm.precisions_cholesky_, expected_pc)))
Beispiel #3
0
 def test_not_converged_warning(self):
     """ Tests GaussianMixture warns when not converged """
     with self.assertWarns(ConvergenceWarning):
         x, _ = load_iris(return_X_y=True)
         x_ds = ds.array(x, (75, 4))
         gm = GaussianMixture(max_iter=1)
         gm.fit(x_ds)
Beispiel #4
0
 def test_means_init_and_weights_init(self):
     """ Tests GaussianMixture means_init and weights_init parameters """
     x, _ = load_iris(return_X_y=True)
     x_ds = ds.array(x, (75, 4))
     weights_init = [1 / 3, 1 / 3, 1 / 3]
     means_init = np.array([[5, 3, 2, 0],
                            [6, 3, 4, 1],
                            [7, 3, 6, 2]])
     gm = GaussianMixture(random_state=0, n_components=3,
                          weights_init=weights_init, means_init=means_init)
     gm.fit(x_ds)
     self.assertTrue(gm.converged_)
Beispiel #5
0
    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])
Beispiel #6
0
    def test_precisions_init_spherical(self):
        """ Tests GaussianMixture with precisions_init='spherical' """
        x, _ = load_iris(return_X_y=True)
        x_ds = ds.array(x, (75, 4))
        weights_init = [1 / 3, 1 / 3, 1 / 3]
        means_init = np.array([[5, 3, 2, 0],
                               [6, 3, 4, 1],
                               [7, 3, 6, 2]])
        np.random.seed(0)
        precisions_init = np.random.rand(3) * 2

        gm = GaussianMixture(covariance_type='spherical', random_state=0,
                             n_components=3, weights_init=weights_init,
                             means_init=means_init,
                             precisions_init=precisions_init)
        gm.fit(x_ds)
        self.assertTrue(gm.converged_)
Beispiel #7
0
    def test_verbose(self):
        """ Tests GaussianMixture verbose mode prints text """
        x = ds.array([[0, 0], [0, 1], [1, 0]], (3, 2))
        gm = GaussianMixture(verbose=True, max_iter=2)

        saved_stdout = sys.stdout
        try:
            sys.stdout = io.StringIO()

            # Call code that has to print
            gm.fit(x)

            captured_output = sys.stdout.getvalue()
        finally:
            sys.stdout = saved_stdout

        self.assertTrue(len(captured_output) > 0)
Beispiel #8
0
    def test_precisions_init_tied(self):
        """ Tests GaussianMixture with precisions_init='tied' """
        x, _ = load_iris(return_X_y=True)
        x_ds = ds.array(x, (75, 4))
        weights_init = [1 / 3, 1 / 3, 1 / 3]
        means_init = [[5, 3, 2, 0],
                      [6, 3, 4, 1],
                      [7, 3, 6, 2]]
        np.random.seed(0)
        rand_matrix = np.random.rand(4, 4)
        precisions_init = np.matmul(rand_matrix, rand_matrix.T)

        gm = GaussianMixture(covariance_type='tied', random_state=0,
                             n_components=3, weights_init=weights_init,
                             means_init=means_init,
                             precisions_init=precisions_init)
        gm.fit(x_ds)
        self.assertTrue(gm.converged_)
Beispiel #9
0
    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_))
Beispiel #10
0
 def test_check_initial_parameters(self):
     """Tests GaussianMixture initial parameters validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(3, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(weights_init=[1, 2])
         gm.fit(x)
     with self.assertRaises(ValueError):
         gm = GaussianMixture(means_init=[1, 2])
         gm.fit(x)
     with self.assertRaises(ValueError):
         gm = GaussianMixture(precisions_init=[1, 2],
                              covariance_type='full')
         gm.fit(x)
     with self.assertRaises(ValueError):
         gm = GaussianMixture(precisions_init=[1, 2],
                              covariance_type='tied')
         gm.fit(x)
     with self.assertRaises(ValueError):
         gm = GaussianMixture(precisions_init=[1, 2],
                              covariance_type='diag')
         gm.fit(x)
     with self.assertRaises(ValueError):
         gm = GaussianMixture(precisions_init=[1, 2],
                              covariance_type='spherical')
         gm.fit(x)
     with self.assertRaises(ValueError):
         gm = GaussianMixture(means_init=[[1, 2, 3]],
                              precisions_init=[[1, 2], [3, 4]],
                              covariance_type='tied')
         gm.fit(x)
Beispiel #11
0
 def test_check_init_params(self):
     """Tests GaussianMixture init_params validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(3, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(init_params='')
         gm.fit(x)
Beispiel #12
0
 def test_check_covariance_type(self):
     """Tests GaussianMixture covariance_type validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(3, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(covariance_type='')
         gm.fit(x)
Beispiel #13
0
 def test_check_reg_covar(self):
     """Tests GaussianMixture reg_covar validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(3, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(reg_covar=-0.1)
         gm.fit(x)
Beispiel #14
0
 def test_check_max_iter(self):
     """Tests GaussianMixture max_iter validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(3, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(max_iter=0)
         gm.fit(x)
Beispiel #15
0
 def test_check_n_components(self):
     """Tests GaussianMixture n_components validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(3, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(n_components=0)
         gm.fit(x)
Beispiel #16
0
 def test_check_tol(self):
     """Tests GaussianMixture tol validation"""
     x = ds.array([[0, 0], [0, 1], [1, 0]], block_size=(10, 2))
     with self.assertRaises(ValueError):
         gm = GaussianMixture(tol=-0.1)
         gm.fit(x)