# ------------------------------------------------------------------------------
# Original Author:  Gael Varoquaux <gael dot varoquaux at normalesup dot org>
# License:          BSD 3 clause
# Co-Author:        Tologon Eshimkanov (https://github.com/tologon)
# Course:           COMP 3770-01 - Introduction to Artificial Intelligence
# School:           Wentworth Institute of Technology
# Project:          Simplified Digit Recognition
# Description:
#   This file is storing a machine learning algorithm (i.e a classifier
#   to be applied on training and test data), plotting function, and
#   MNIST database of digits. This file primarily displays training examples,
#   processes MNIST data and trains ML classifier on such data.
#
#   Note: the cross-validation process happens in the pipeline.py file.
# ------------------------------------------------------------------------------

# required package(s)
import matplotlib.pyplot as plt
from sklearn import datasets, svm

# The digits dataset
digits = datasets.load_digits()

# The data that we are interested in is made of 8x8 images of digits, let's
# have a look at the first 3 images, stored in the `images` attribute of the
# dataset.  If we were working from image files, we could load them using
# pylab.imread.  Note that each image must have the same size. For these
# images, we know which digit they represent: it is given in the 'target' of
# the dataset.
images_and_labels = list(zip(digits.images, digits.target))
for index, (image, label) in enumerate(images_and_labels[:4]):
    plt.subplot(2, 4, index + 1)
    plt.axis('off')
    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
    plt.title('Training: %i' % label)

# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))

# Create a classifier: a linear support vector classifier
classifier = svm.LinearSVC()

# We learn the digits on the entire database of the MNIST digits
classifier.fit(data, digits.target)
