Skip to content

Instantly share code, notes, and snippets.

@DFoly
Last active April 13, 2021 08:53
Show Gist options
  • Select an option

  • Save DFoly/5fd08799bd340ae30858ca303559c3d4 to your computer and use it in GitHub Desktop.

Select an option

Save DFoly/5fd08799bd340ae30858ca303559c3d4 to your computer and use it in GitHub Desktop.
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