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

In [2]:
import kmeans as kmeans_

kmeans = kmeans_.load_kmeans('kmeans-10.dat')
visualize.plot_means(kmeans)


Number of means: 10

In [3]:
# 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)

In [4]:
plt.imshow(train_data[0], cmap='gray')


Out[4]:
<matplotlib.image.AxesImage at 0x7f613211eac8>

In [5]:
# Reshape data
train_data = np.reshape(train_data, (train_data.shape[0], 784))
test_data = np.reshape(test_data, (test_data.shape[0], 784))

Bernoulli Mixture Model


In [6]:
# Making it binary
train_data_binary = np.where(train_data > 0.5, 1, 0)
test_data_binary = np.where(test_data > 0.5, 1, 0)
plt.imshow(np.reshape(train_data_binary[0], (28, 28)), cmap='gray')


Out[6]:
<matplotlib.image.AxesImage at 0x7f61321104e0>

In [7]:
model = bmm(10, n_iter=20, verbose=True)

In [8]:
import visualize
model.fit(train_data_binary, means_init_heuristic='random')
visualize.plot_means(model.means)


using random heuristic to initialize the means
[00] likelihood = -568.7447954425697 (elapsed 0:00:00.000011)
[01] likelihood = -190.9893873861033 (elapsed 0:00:03.238385)
[02] likelihood = -177.76898929594628 (elapsed 0:00:06.331602)
[03] likelihood = -171.8370297940123 (elapsed 0:00:09.431821)
[04] likelihood = -169.3375409522614 (elapsed 0:00:12.530521)
[05] likelihood = -168.22701444138704 (elapsed 0:00:15.638988)
[06] likelihood = -167.49335611707937 (elapsed 0:00:18.746143)
[07] likelihood = -166.95695924390253 (elapsed 0:00:21.852179)
[08] likelihood = -166.59166626430394 (elapsed 0:00:24.957374)
[09] likelihood = -166.3484840425897 (elapsed 0:00:28.064057)
[10] likelihood = -166.19972436218973 (elapsed 0:00:31.171494)
[11] likelihood = -166.09848847875028 (elapsed 0:00:34.280970)
[12] likelihood = -166.02241763953984 (elapsed 0:00:37.387654)
[13] likelihood = -165.9734568000099 (elapsed 0:00:40.497298)
[14] likelihood = -165.93390782770945 (elapsed 0:00:43.603479)
[15] likelihood = -165.89686457524402 (elapsed 0:00:46.713598)
[16] likelihood = -165.86651530587977 (elapsed 0:00:49.824044)
[17] likelihood = -165.84345845129477 (elapsed 0:00:52.956869)
[18] likelihood = -165.82200848065656 (elapsed 0:00:56.064609)
[19] likelihood = -165.80091595169043 (elapsed 0:00:59.176806)
[20] likelihood = -165.77693626733603 (elapsed 0:01:02.283341)
converged in 21 iterations in 0:01:05.392259
Number of means: 10

In [9]:
model2 = bmm(10, verbose=True)
model2.fit(train_data_binary, means_init_heuristic='data_classes_mean', labels=train_labels)
visualize.plot_means(model2.means)


using data_classes_mean heuristic to initialize the means
[00] likelihood = -170.0721754394032 (elapsed 0:00:00.000025)
[01] likelihood = -168.48184456985467 (elapsed 0:00:03.109455)
[02] likelihood = -167.47609486286774 (elapsed 0:00:06.253904)
[03] likelihood = -166.86314656637327 (elapsed 0:00:09.357631)
[04] likelihood = -166.50318153203176 (elapsed 0:00:12.465661)
[05] likelihood = -166.2674235380959 (elapsed 0:00:15.627504)
[06] likelihood = -166.10039120351965 (elapsed 0:00:18.790285)
[07] likelihood = -165.99419530199302 (elapsed 0:00:21.898102)
[08] likelihood = -165.9117273097042 (elapsed 0:00:25.010853)
[09] likelihood = -165.8468430112527 (elapsed 0:00:28.173921)
[10] likelihood = -165.79296046410198 (elapsed 0:00:31.337134)
[11] likelihood = -165.75368606492944 (elapsed 0:00:34.449491)
[12] likelihood = -165.72760330768065 (elapsed 0:00:37.559824)
[13] likelihood = -165.70292977875587 (elapsed 0:00:40.726269)
[14] likelihood = -165.68259518731233 (elapsed 0:00:43.895982)
[15] likelihood = -165.66250757475953 (elapsed 0:00:46.998792)
[16] likelihood = -165.64405853649433 (elapsed 0:00:50.113952)
[17] likelihood = -165.62619425865446 (elapsed 0:00:53.272761)
[18] likelihood = -165.60456479965075 (elapsed 0:00:56.425272)
[19] likelihood = -165.5815199390327 (elapsed 0:00:59.523869)
[20] likelihood = -165.55859948485264 (elapsed 0:01:02.638777)
[21] likelihood = -165.53749856008477 (elapsed 0:01:05.797373)
[22] likelihood = -165.52023210312487 (elapsed 0:01:08.949242)
[23] likelihood = -165.50826958980312 (elapsed 0:01:12.052251)
[24] likelihood = -165.497675319114 (elapsed 0:01:15.164123)
[25] likelihood = -165.4873399132042 (elapsed 0:01:18.323915)
[26] likelihood = -165.4802188366528 (elapsed 0:01:21.475305)
[27] likelihood = -165.4744587411926 (elapsed 0:01:24.576846)
[28] likelihood = -165.46736634905403 (elapsed 0:01:27.689159)
[29] likelihood = -165.4631581437666 (elapsed 0:01:30.845465)
[30] likelihood = -165.46001165372704 (elapsed 0:01:33.998547)
[31] likelihood = -165.45734951960443 (elapsed 0:01:37.099546)
[32] likelihood = -165.4553231709111 (elapsed 0:01:40.210803)
[33] likelihood = -165.45305519445324 (elapsed 0:01:43.370612)
[34] likelihood = -165.4508507704601 (elapsed 0:01:46.521094)
[35] likelihood = -165.44960198073093 (elapsed 0:01:49.621306)
[36] likelihood = -165.44839763560267 (elapsed 0:01:52.735854)
[37] likelihood = -165.44737825106847 (elapsed 0:01:55.893701)
[38] likelihood = -165.4468681668938 (elapsed 0:01:59.044434)
converged in 38 iterations in 0:02:01.946911
Number of means: 10

Classification using BMM


In [10]:
from classifier import classifier

# number of components for each BMM
k = 10

bayesian_classifier = classifier(k, means_init_heuristic='kmeans',
                                 means=kmeans, model_type='bmm')

bayesian_classifier.fit(train_data_binary, train_labels)


training label 0 (5923 samples)
converged in 66 iterations in 0:00:21.450448
training label 1 (6742 samples)
converged in 41 iterations in 0:00:14.867834
training label 2 (5958 samples)
converged in 53 iterations in 0:00:17.440692
training label 3 (6131 samples)
converged in 58 iterations in 0:00:19.954195
training label 4 (5842 samples)
converged in 43 iterations in 0:00:14.199735
training label 5 (5421 samples)
converged in 62 iterations in 0:00:18.706564
training label 6 (5918 samples)
converged in 53 iterations in 0:00:17.437920
training label 7 (6265 samples)
converged in 101 iterations in 0:00:34.547184
training label 8 (5851 samples)
converged in 81 iterations in 0:00:26.399630
training label 9 (5949 samples)
converged in 60 iterations in 0:00:19.577842

In [11]:
visualize.plot_means(bayesian_classifier.models[3].means)
visualize.plot_means(bayesian_classifier.models[7].means)


Number of means: 10
Number of means: 10

In [12]:
label_set = set(train_labels)
predicted_labels = bayesian_classifier.predict(test_data_binary, label_set)

print('Accuracy: {}'.format(np.mean(predicted_labels == test_labels)))


Accuracy: 0.9248