<jupyter_start><jupyter_text>Classification of MNIST data set with Ada Boost Algorithm<jupyter_code># Import the required libraries.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
# Load MNIST data set from sci-kit Learn Library.
mnist_data = datasets.load_digits() 
mnist_data
# Show the first image from the data set.
plt.subplot()
plt.imshow(mnist_data.images[0])
# Show the first image gray-scale.
plt.subplot()
plt.imshow(mnist_data.images[0], cmap=plt.cm.gray_r)
# Identify the target value for the first image.
mnist_data.target[0]
# Show the shape of mnist_data.images as a numpy.ndarray.
np.shape(mnist_data.images)
# The data set of mnist_data.images has 1797 samples of 8*8 images. For classification purposes,
# we have to reshape this 8*8 images to 64-element lists.
# To put it in another way, we must transform an 8*8 matrix to a single list (with 64 elements).
X = mnist_data.images.reshape(len(mnist_data.images), -1)
# Now we have 1797 samples, which each sample has 64 features.
np.shape(X)
# The content of this data set.
X
# Specify the mnist_data targets.
y = mnist_data.target
type(y), y, len(y)
# Import Ada Boost classifier from sci-kit library.
# Import SVM Classifier from sci-kit library.
# SVM is used as a base estimator.
# Note that Ada Boost is an 'Ensemble' algorithm.
from sklearn.ensemble import AdaBoostClassifier
from sklearn.svm import SVC
# Create an Ada Boost classifier.
ada_boost_classifier = AdaBoostClassifier(base_estimator=SVC(probability=True, kernel='rbf'), n_estimators=100)
# Learn from the data.
ada_boost_classifier.fit(X[:1000], y[:1000])
# Now let's evaluate our model with specific metrics.
from sklearn import metrics
# We must give our test data set to metrics.
# With metrics.classification_report, the accuracy, precision and recall of our model are found out.
predicted_values = ada_boost_classifier.predict(X[1000:])
expected_values = y[1000:]
print(metrics.classification_report(expected_values, predicted_values))
# Show the confusion matrix of our model.
print(metrics.confusion_matrix(expected_values, predicted_values))<jupyter_output>[[77  0  0  0  1  0  1  0  0  0]
 [ 0 76  0  0  0  0  0  0  4  0]
 [ 1  0 72  4  0  0  0  0  0  0]
 [ 0  4  0 72  0  1  0  0  2  0]
 [ 0  2  0  0 80  0  1  0  0  0]
 [ 0  2  0  1  2 73  1  0  0  3]
 [ 0  1  0  0  0  0 79  0  0  0]
 [ 0 10  0  0 12  0  0 58  0  0]
 [ 0 15  0  7  2  1  0  0 51  0]
 [ 1 10  0  8  2  2  0  0  0 58]]
{'fraction_non_alphanumeric': 0.08011272141706924, 'fraction_numerical': 0.06320450885668277, 'mean_word_length': 3.706439393939394, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '258288', 'license': 'MIT', 'n_tokens_mistral': 976, 'n_tokens_neox': 879, 'n_words': 349, 'path': 'Classification-AdaBoost-MNIST.ipynb', 'repo_name': 'farazattar/Python', 'size': '2484'}