Set the initial weights, means and covs (with full covariance matrix). weights: the prior of the clusters (what percentage of data does a cluster have) means: the mean points of the clusters covs: the covariance matrix of the clusters
(self)
| 60 | break |
| 61 | |
| 62 | def _initialize(self): |
| 63 | """Set the initial weights, means and covs (with full covariance matrix). |
| 64 | |
| 65 | weights: the prior of the clusters (what percentage of data does a cluster have) |
| 66 | means: the mean points of the clusters |
| 67 | covs: the covariance matrix of the clusters |
| 68 | """ |
| 69 | self.weights = np.ones(self.K) |
| 70 | if self.init == "random": |
| 71 | self.means = [ |
| 72 | self.X[x] for x in random.sample(range(self.n_samples), self.K) |
| 73 | ] |
| 74 | self.covs = [np.cov(self.X.T) for _ in range(self.K)] |
| 75 | |
| 76 | elif self.init == "kmeans": |
| 77 | kmeans = KMeans(K=self.K, max_iters=self.max_iters // 3, init="++") |
| 78 | kmeans.fit(self.X) |
| 79 | self.assignments = kmeans.predict() |
| 80 | self.means = kmeans.centroids |
| 81 | self.covs = [] |
| 82 | for i in np.unique(self.assignments): |
| 83 | self.weights[int(i)] = (self.assignments == i).sum() |
| 84 | self.covs.append(np.cov(self.X[self.assignments == i].T)) |
| 85 | else: |
| 86 | raise ValueError("Unknown type of init parameter") |
| 87 | self.weights /= self.weights.sum() |
| 88 | |
| 89 | def _E_step(self): |
| 90 | """Expectation(E-step) for Gaussian Mixture.""" |