assert gmm.pi_k.shape == ( n_cluster,), 'pi_k should be numpy vector of size'.format(n_cluster) assert iterations > 0 and type( iterations) == int, 'Number of updates should be positive integer' assert type(ll) == float, 'log-likelihood should be float' print('GMM for toy dataset with {} init converged in {} iteration. Final log-likelihood of data: {}'.format( i, iterations, ll)) np.savez('results/gmm_toy_{}.npz'.format(i), iterations=iterations, variances=gmm.variances, pi_k=gmm.pi_k, means=gmm.means, log_likelihood=ll, x=x, y=y) # plot fig = Figure() fig.ax.scatter(x[:, 0], x[:, 1], c=y) # fig.ax.scatter(gmm.means[:, 0], gmm.means[:, 1], c='red') for component in range(n_cluster): a, b, angle = compute_elipse_params(gmm.variances[component]) e = Ellipse(xy=gmm.means[component], width=a * 5, height=b * 5, angle=angle, alpha=gmm.pi_k[component]) fig.ax.add_artist(e) fig.savefig('plots/gmm_toy_dataset_{}.png'.format(i)) ################################################################################ # GMM on digits dataset # We fit a gaussian distribution on digits dataset and show generate samples from the distribution # Complete implementation of sample function for GMM class in gmm.py ################################################################################
import pickle from PIL import ImageFont, ImageDraw, Image from utils import Figure TEXT = "0123456789." fnt = ImageFont.truetype("FreeMonoBold", 100) FIGURES = {} for c in TEXT: bbox = fnt.getbbox(c, "1") shape = (bbox[0] + bbox[2], bbox[1] + bbox[3]) im = Image.new("1", shape) d = ImageDraw.Draw(im) d.text((0, 0), c, font=fnt, fill=(1,)) FIGURES[c] = Figure(shape, list(im.getdata())) with open("figures.dat", "wb") as f: pickle.dump(FIGURES, f) # im.show()