Commit d4507c2683cd0fcec7ee302ad1a4e430b61cd6cd
1 parent
4152e83df2
Exists in
master
We do not update a cluster if it is not associated to any data point.
Showing 1 changed file with 3 additions and 3 deletions Side-by-side Diff
volia/clustering_modules/kmeans_mahalanobis.py
| ... | ... | @@ -27,7 +27,6 @@ |
| 27 | 27 | for n in range(N): |
| 28 | 28 | for k in range(self.K): |
| 29 | 29 | distances[n][k] = self._dist(features[n], self.C[k], self.L[k]) |
| 30 | - print(distances) | |
| 31 | 30 | closest_cluster = np.argmin(distances, axis=1) |
| 32 | 31 | return closest_cluster |
| 33 | 32 | |
| ... | ... | @@ -119,7 +118,6 @@ |
| 119 | 118 | for n in range(N): |
| 120 | 119 | for k in range(self.K): |
| 121 | 120 | distances[n][k] = self._dist(X[n], self.C[k], self.L[k]) |
| 122 | - print(distances) | |
| 123 | 121 | closest_cluster = np.argmin(distances, axis=1) |
| 124 | 122 | if i % 1 == 0: |
| 125 | 123 | # -- Debug tool ---------------------- |
| ... | ... | @@ -140,6 +138,8 @@ |
| 140 | 138 | # Find subset of X with values closed to the centroid c_k. |
| 141 | 139 | X_sub = np.where(closest_cluster == k) |
| 142 | 140 | X_sub = np.take(X, X_sub[0], axis=0) |
| 141 | + if X_sub.shape[0] == 0: | |
| 142 | + continue | |
| 143 | 143 | np.mean(X_sub, axis=0) |
| 144 | 144 | C_new = np.mean(X_sub, axis=0) |
| 145 | 145 | |
| ... | ... | @@ -150,7 +150,7 @@ |
| 150 | 150 | c_tmp = np.reshape(C_new, (-1, 1)) |
| 151 | 151 | K_new = K_new + (x - c_tmp).dot((x - c_tmp).transpose()) |
| 152 | 152 | K_new = K_new / X_sub.shape[0] |
| 153 | - K_new = np.linalg.inv(K_new) | |
| 153 | + K_new = np.linalg.pinv(K_new) | |
| 154 | 154 | |
| 155 | 155 | if end_algo and (not (self.C[k] == C_new).all()): # If the same stop |
| 156 | 156 | end_algo = False |