Maximization (M-step) for Gaussian Mixture.
(self)
| 96 | self.responsibilities = weighted_likelihoods |
| 97 | |
| 98 | def _M_step(self): |
| 99 | """Maximization (M-step) for Gaussian Mixture.""" |
| 100 | weights = self.responsibilities.sum(axis=0) |
| 101 | for assignment in range(self.K): |
| 102 | resp = self.responsibilities[:, assignment][:, np.newaxis] |
| 103 | self.means[assignment] = (resp * self.X).sum(axis=0) / resp.sum() |
| 104 | self.covs[assignment] = (self.X - self.means[assignment]).T.dot( |
| 105 | (self.X - self.means[assignment]) * resp |
| 106 | ) / weights[assignment] |
| 107 | self.weights = weights / weights.sum() |
| 108 | |
| 109 | def _is_converged(self): |
| 110 | """Check if the difference of the latest two likelihood is less than the tolerance.""" |