def mnist_digit_recognition(): train_set, test_set = load_mnist_dataset() n_labels = 10 # 1,2,3,4,5,6,7,9,0 n_features = 28 * 28 draw_ex_images(5, 4, train_set[0].shape[0], train_set[0]) mnist_model = GaussianNaiveBayes(n_labels, n_features) start = time.time() mnist_model.train(train_set[0], train_set[1]) end = time.time() print(end - start) mnist_model.save_model() mean, var, pi = mnist_model.get_parameters() print(f"Model parameters: mean {mean}, var {var}, pi {pi}") test_data, labels = test_set limit = 150 test_data, labels = test_data[:limit], labels[:limit] results = np.arange(limit, dtype=np.int) for n in range(limit): results[n] = mnist_model.classify(test_data[n]) print(f"{n} : predicted {results[n]}, correct {labels[n]}") print("recognition rate: ", (results == labels).mean())
def naive_bayes_test(): n_labels, n_features = 2, 2 nb = GaussianNaiveBayes(n_labels, n_features) # prepare sample training data data1 = np.random.multivariate_normal([1,4], [[2,0],[0,2]], size=100) data2 = np.random.multivariate_normal([5,7], [[3,0],[0,1]], size=100) data = np.concatenate((data1, data2), axis=0) # prepare training label data labels = np.concatenate((np.array([0]*100), np.array([1]*100)), axis=0) print "correct labels" print labels # nb.load() nb.train(data, labels) # nb.save() results = nb.classify(data) print "predicted labels" print results print "recognition rate: ", (results == labels).mean()
def mnist_digit_recognition(): train_set, valid_set, test_set = load_mnist_dataset("mnist.pkl.gz") n_labels = 10 # 1,2,3,4,5,6,7,9,0 n_features = 28*28 mnist_model = GaussianNaiveBayes(n_labels, n_features) mnist_model.train(train_set[0], train_set[1]) [mean, var], pi = mnist_model.get_parameters() # visualization of learned means create_2D_images_horizontal(mean, w=28, h=28) show() test_data, labels = test_set # slice #limit = len(test_data) limit = 50 test_data, labels = test_data[:limit], labels[:limit] results = np.arange(limit, dtype=np.int) for n in range(limit): results[n] = mnist_model.classify(test_data[n]) print "%d : predicted %s, correct %s" % (n, results[n], labels[n]) # results = mnist_model.classify(test_data) print "recognition rate: ", (results == labels).mean()
def mnist_digit_recognition(): train_set, valid_set, test_set = load_mnist_dataset("mnist.pkl.gz") n_labels = 10 # 1,2,3,4,5,6,7,9,0 n_features = 28 * 28 mnist_model = GaussianNaiveBayes(n_labels, n_features) mnist_model.train(train_set[0], train_set[1]) [mean, var], pi = mnist_model.get_parameters() # visualization of learned means create_2D_images_horizontal(mean, w=28, h=28) show() test_data, labels = test_set # slice #limit = len(test_data) limit = 50 test_data, labels = test_data[:limit], labels[:limit] results = np.arange(limit, dtype=np.int) for n in range(limit): results[n] = mnist_model.classify(test_data[n]) print "%d : predicted %s, correct %s" % (n, results[n], labels[n]) # results = mnist_model.classify(test_data) print "recognition rate: ", (results == labels).mean()