In [9]:
%matplotlib inline

from sklearn.datasets import load_digits
from sklearn.mixture import GaussianMixture
from matplotlib import pyplot as plt
import numpy as np

In [10]:
digits = load_digits()
digits.data.shape


Out[10]:
(1797, 64)

In [11]:
# plot the first 100 digits

def plot_digits(data):
    fig, ax = plt.subplots(10, 10, figsize=(8, 8),
                           subplot_kw=dict(xticks=[], yticks=[]))
    fig.subplots_adjust(hspace=0.05, wspace=0.05)
    for i, axi in enumerate(ax.flat):
        im = axi.imshow(data[i].reshape(8, 8), cmap='binary')
        im.set_clim(0, 16)
plot_digits(digits.data)



In [16]:
from sklearn.decomposition import PCA
pca = PCA(0.98, whiten=True)
data = pca.fit_transform(digits.data)
data.shape


Out[16]:
(1797, 37)

In [17]:
n_components = np.arange(50, 210, 10)
models = [GaussianMixture(n, covariance_type='full', random_state=0)
          for n in n_components]
aics = [model.fit(data).aic(data) for model in models]
plt.plot(n_components, aics);



In [19]:
gmm = GaussianMixture(160, covariance_type='full', random_state=0)
gmm.fit(data)
print(gmm.converged_)


True

In [21]:
data_new = gmm.sample(100)
data_new[0].shape


Out[21]:
(100, 37)

In [23]:
digits_new = pca.inverse_transform(data_new[0])
plot_digits(digits_new)



In [ ]: