def __init__( self, X, prior, alpha, K, assignments="rand", covariance_type="full" ): self.alpha = alpha N, D = X.shape # Initial component assignments if assignments == "rand": assignments = np.random.randint(0, K, N) # Make sure we have consequetive values for k in xrange(assignments.max()): while len(np.nonzero(assignments == k)[0]) == 0: assignments[np.where(assignments > k)] -= 1 if assignments.max() == k: break elif assignments == "each-in-own": assignments = np.arange(N) else: # assignments is a vector pass if covariance_type == "full": self.components = GaussianComponents(X, prior, assignments, K_max=K) elif covariance_type == "diag": self.components = GaussianComponentsDiag(X, prior, assignments, K_max=K) elif covariance_type == "fixed": self.components = GaussianComponentsFixedVar(X, prior, assignments, K_max=K) else: assert False, "Invalid covariance type."
def setup_components(self, K, assignments="rand", X=None): """ Setup the `components` attribute. See parameters of `FBGMM` for parameters not described below. This function is also useful for resetting the `components`, e.g. if you want to change the maximum number of possible components. Parameters ---------- X : NxD matrix or None The data matrix. If None, then it is assumed that the `components` attribute has already been initialized and that this function is called to reset the `components`; in this case the data is taken from the previous initialization. """ if X is None: assert hasattr(self, "components") X = self.components.X N, D = X.shape # Initial component assignments if isinstance(assignments, basestring) and assignments == "rand": assignments = np.random.randint(0, K, N) elif isinstance(assignments, basestring) and assignments == "each-in-own": assignments = np.arange(N) else: # `assignments` is a vector pass # Make sure we have consequetive values for k in xrange(assignments.max()): while len(np.nonzero(assignments == k)[0]) == 0: assignments[np.where(assignments > k)] -= 1 if assignments.max() == k: break if self.covariance_type == "full": self.components = GaussianComponents(X, self.prior, assignments, K_max=K) elif self.covariance_type == "diag": self.components = GaussianComponentsDiag(X, self.prior, assignments, K_max=K) elif self.covariance_type == "fixed": self.components = GaussianComponentsFixedVar(X, self.prior, assignments, K_max=K) else: assert False, "Invalid covariance type."