()
| 70 | |
| 71 | |
| 72 | def plot(): |
| 73 | fig, axes = plt.subplots(4, 4) |
| 74 | fig.set_size_inches(10, 10) |
| 75 | for i, ax in enumerate(axes.flatten()): |
| 76 | n_ex = 150 |
| 77 | n_in = 2 |
| 78 | n_classes = np.random.randint(2, 4) |
| 79 | X, y = make_blobs( |
| 80 | n_samples=n_ex, centers=n_classes, n_features=n_in, random_state=i |
| 81 | ) |
| 82 | X -= X.mean(axis=0) |
| 83 | |
| 84 | # take best fit over 10 runs |
| 85 | best_elbo = -np.inf |
| 86 | for k in range(10): |
| 87 | _G = GMM(C=n_classes, seed=k * 3) |
| 88 | ret = _G.fit(X, max_iter=100, verbose=False) |
| 89 | while ret != 0: |
| 90 | print("Components collapsed; Refitting") |
| 91 | ret = _G.fit(X, max_iter=100, verbose=False) |
| 92 | |
| 93 | if _G.best_elbo > best_elbo: |
| 94 | best_elbo = _G.best_elbo |
| 95 | G = _G |
| 96 | |
| 97 | ax = plot_clusters(G, X, ax) |
| 98 | ax.xaxis.set_ticklabels([]) |
| 99 | ax.yaxis.set_ticklabels([]) |
| 100 | ax.set_title("# Classes: {}; Final VLB: {:.2f}".format(n_classes, G.best_elbo)) |
| 101 | |
| 102 | plt.tight_layout() |
| 103 | plt.savefig("img/plot.png", dpi=300) |
| 104 | plt.close("all") |
nothing calls this directly
no test coverage detected