Last active
April 13, 2021 08:53
-
-
Save DFoly/5fd08799bd340ae30858ca303559c3d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def _e_step(self, X, pi, mu, sigma): | |
| """Performs E-step on GMM model | |
| Parameters: | |
| ------------ | |
| X: (N x d), data points, m: no of features | |
| pi: (C), weights of mixture components | |
| mu: (C x d), mixture component means | |
| sigma: (C x d x d), mixture component covariance matrices | |
| Returns: | |
| ---------- | |
| gamma: (N x C), probabilities of clusters for objects | |
| """ | |
| N = X.shape[0] | |
| self.gamma = np.zeros((N, self.C)) | |
| const_c = np.zeros(self.C) | |
| self.mu = self.mu if self._initial_means is None else self._initial_means | |
| self.pi = self.pi if self._initial_pi is None else self._initial_pi | |
| self.sigma = self.sigma if self._initial_cov is None else self._initial_cov | |
| for c in range(self.C): | |
| # Posterior Distribution using Bayes Rule | |
| self.gamma[:,c] = self.pi[c] * mvn.pdf(X, self.mu[c,:], self.sigma[c]) | |
| # normalize across columns to make a valid probability | |
| gamma_norm = np.sum(self.gamma, axis=1)[:,np.newaxis] | |
| self.gamma /= gamma_norm | |
| return self.gamma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment