Ejemplo n.º 1
0
 def test__initialize_covariances_case4(self):
     """ diagonal covariance, multi-init.
     """
     random_state = np.random.RandomState(12)
     n_init = 3
     means_init_true = random_state.rand(n_init, self.td.K, self.td.P)
     covariances_init_true = random_state.rand(n_init, self.td.K, self.td.P)
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=means_init_true,
                           covariances_init=covariances_init_true,
                           covariance_type='diag',
                           n_init=n_init,
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     # init means twice to cycle covariances
     _ = gmm._initialize_means()
     means_init = gmm._initialize_means()
     covariances_init_test = gmm._initialize_covariances(means_init)
     self.assertArrayEqual(covariances_init_test, covariances_init_true[1])
Ejemplo n.º 2
0
 def test__initialize_weights_case3(self):
     """ multi-init
     """
     random_state = np.random.RandomState(12)
     n_init = 3
     means_init_true = random_state.rand(n_init, self.td.K, self.td.P)
     weights_init_true = random_state.rand(n_init, self.td.K)
     weights_init_true /= weights_init_true.sum(axis=1)[:, np.newaxis]
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=means_init_true,
                           weights_init=weights_init_true,
                           n_init=n_init,
                           covariance_type='diag',
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     # init means twice to cycle covariances
     _ = gmm._initialize_means()
     means_init = gmm._initialize_means()
     weights_init_test = gmm._initialize_weights(means_init)
     self.assertArrayEqual(weights_init_test, weights_init_true[1])
Ejemplo n.º 3
0
 def test__initialize_means_case2(self):
     """ means_init is a 3D array.
     """
     random_state = np.random.RandomState(12)
     n_init = 3
     means_init_true = random_state.rand(n_init, self.td.K, self.td.P)
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=means_init_true,
                           n_init=n_init,
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     # first one is discarded for this test
     _ = gmm._initialize_means()
     # this should recover the second one
     means_init_test = gmm._initialize_means()
     self.assertArrayEqual(means_init_test, means_init_true[1])
Ejemplo n.º 4
0
 def test__initialize_means_case1(self):
     """ means_init is a 2D array.
     """
     random_state = np.random.RandomState(12)
     means_init_true = random_state.rand(self.td.K, self.td.P)
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=means_init_true,
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     means_init_test = gmm._initialize_means()
     self.assertArrayEqual(means_init_test, means_init_true)
Ejemplo n.º 5
0
 def test__initialize_means_case4(self):
     """ means_init is None, init_params is 'random'.
     Only checks that the initialized means are of the correct shape.
     """
     random_state = np.random.RandomState(12)
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=None,
                           init_params='random',
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     means_init_shape_test = gmm._initialize_means().shape
     means_init_shape_true = np.array([self.td.K, self.td.P])
     self.assertArrayEqual(means_init_shape_test, means_init_shape_true)
Ejemplo n.º 6
0
 def test__initialize_covariances_case3(self):
     """ No covariances given, just check shape.
     """
     random_state = np.random.RandomState(12)
     means_init_true = random_state.rand(self.td.K, self.td.P)
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=means_init_true,
                           covariances_init=None,
                           covariance_type='diag',
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     means_init = gmm._initialize_means()
     covariances_init_test = gmm._initialize_covariances(means_init)
     true_shape = np.array((self.td.K, self.td.P))
     self.assertArrayEqual(covariances_init_test.shape, true_shape)
Ejemplo n.º 7
0
 def test__initialize_covariances_case1(self):
     """ spherical covariance, 1 init.
     """
     random_state = np.random.RandomState(12)
     means_init_true = random_state.rand(self.td.K, self.td.P)
     covariances_init_true = random_state.rand(self.td.K)
     gmm = GaussianMixture(n_components=self.td.K,
                           num_feat_full=self.td.P,
                           num_feat_comp=self.td.Q,
                           num_feat_shared=self.td.Qs,
                           num_samp=self.td.N,
                           transform=self.td.transform,
                           D_indices=self.td.D_indices,
                           mask=self.td.mask,
                           means_init=means_init_true,
                           covariances_init=covariances_init_true,
                           covariance_type='spherical',
                           random_state=random_state)
     gmm.fit_sparsifier(X=self.td.X)
     means_init = gmm._initialize_means()
     covariances_init_test = gmm._initialize_covariances(means_init)
     self.assertArrayEqual(covariances_init_test, covariances_init_true)