```
In [1]:
```%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mnist import load_mnist
from gmm import gmm
import visualize

```
In [2]:
```# Load MNIST dataset
data_path = '../MNIST_data/decompressed'
train_data, train_labels = load_mnist(dataset='training', path=data_path)
test_data, test_labels = load_mnist(dataset='testing', path=data_path)
# reshape
train_data = np.reshape(train_data, (train_data.shape[0], 784))
test_data = np.reshape(test_data, (test_data.shape[0], 784))
# get the set of different labels
label_set = set(train_labels)

```
In [3]:
```# load pre-computed kmeans centers
import kmeans as kmeans_
kmeans = kmeans_.load_kmeans('kmeans-10.dat')
visualize.plot_means(kmeans)

```
```

```
In [4]:
```from sklearn.decomposition import PCA
# Reduce 784 dims to `d `dims
d = 40
# Using PCA to reduce data dims
reducer = PCA(n_components=d)
reducer.fit(train_data)
train_data_reduced = reducer.transform(train_data)
test_data_reduced = reducer.transform(test_data)
kmeans_reduced = reducer.transform(kmeans)

```
In [5]:
```# number of gaussians
k = 10
model = gmm(k, verbose=True)
model.fit(train_data_reduced, means_init_heuristic='kmeans', means=kmeans_reduced)
means_projected = reducer.inverse_transform(model.means)
visualize.plot_means(means_projected)

```
```

```
In [6]:
```from classifier import classifier
bayesian_classifier = classifier(k, model_type='gmm',
means_init_heuristic='kmeans',
means=kmeans_reduced,
covariance_type='diag')
bayesian_classifier.fit(train_data_reduced, train_labels)
means_projected = reducer.inverse_transform(bayesian_classifier.models[4].means)
visualize.plot_means(means_projected)

```
```

```
In [7]:
```predicted_labels = bayesian_classifier.predict(test_data_reduced, label_set)
print('Accuracy: {}'.format(np.mean(predicted_labels == test_labels)))

```
```

```
In [8]:
```bayesian_classifier = classifier(k, model_type='gmm',
means_init_heuristic='kmeans',
means=kmeans_reduced,
covariance_type='full')
bayesian_classifier.fit(train_data_reduced, train_labels)
means_projected = reducer.inverse_transform(bayesian_classifier.models[4].means)
visualize.plot_means(means_projected)

```
```