In [45]:
import numpy as np
import copy
import bayes_logistic as bl
from sklearn import datasets
from sklearn import linear_model
In [46]:
# The digit we want to classify
DIGIT = 1
In [47]:
# Prep the MNIST image classification data. The data is divided into training and test sets
TRAINING_PERCENTAGE = 0.8
TEST_PERCENTAGE = 0.2
digits = datasets.load_digits()
n_samples, n_features = digits.data.shape
# Add bias column to the feature matrix
X = np.ones((n_samples, n_features + 1))
X[:, 1:] = digits.data
# Change the vector to binary data
y = digits.target
for idx in range(0, y.shape[0]):
y[idx] = 1.0 if y[idx] == DIGIT else 0.0
training_cnt = int(n_samples*TRAINING_PERCENTAGE)
training_X = X[:training_cnt,:]
training_y = y[:training_cnt]
test_X = X[training_cnt:,:]
test_y = y[training_cnt:]
In [48]:
# Train the model
logistic = linear_model.LogisticRegression()
model = logistic.fit(training_X, training_y)
In [49]:
# Perform Test
model.score(test_X, test_y)
Out[49]: