<jupyter_start><jupyter_text>Classification of the fashion mnist dataset using SVMsThe 2d image arrays are flattened to a 1d array which is then fed to the SVM  Step 1: Import all required libraries<jupyter_code>pip install cython
from __future__ import absolute_import, division, print_function, unicode_literals

# TensorFlow and tf.keras: Ease our import of the dataset
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Step 2: Import the Dataset<jupyter_code>#Using Keras we import the dataset and store it.
fashion_mnist = keras.datasets.fashion_mnist
#The data is already split into Train and test sets
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()<jupyter_output><empty_output><jupyter_text>Split the dataset further we dont need 50000 for SVMs so we can split the train images into a further training set.<jupyter_code>#We split it even further SVMs dont need a lot of data to train
from sklearn.model_selection import train_test_split

#Ensure to stratify so were able to have all classes in one set
X_train, X_test, y_train, y_test = train_test_split(train_images, train_labels,
                                                   test_size = 0.98,
                                                   random_state = 0,
                                                   stratify = train_labels)

#Define our ClassNames
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

print(X_train.shape)
print(len(y_train)) #We have 12 thousand samples<jupyter_output>(1200, 28, 28)
1200<jupyter_text>Do A bit of exploring<jupyter_code>plt.figure()
plt.imshow(X_train[0], cmap ="Blues")
plt.colorbar()
plt.grid(False)
plt.show()<jupyter_output><empty_output><jupyter_text>Scale the values from 0 to 1, this aids in convergence speed<jupyter_code>X_train = X_train / 255.0

X_test = X_test / 255.0<jupyter_output><empty_output><jupyter_text>Plot a few of them and see how they look<jupyter_code>plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(X_train[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[y_train[i]])
plt.show()<jupyter_output><empty_output><jupyter_text>Good so far we nowFlatten the 2D Arrays to 1D<jupyter_code># Creates a list containing 5 lists, each of 8 items, all set to 0
h, w = len(X_train), 784;
X_trainF = [[0 for x in range(w)] for y in range(h)]
X_trainF = np.asarray(X_trainF)
for i in range(len(X_trainF)):
  X_trainF[i] = X_train[i].flatten()<jupyter_output><empty_output><jupyter_text>Step 3 Train the SVM Model<jupyter_code># Fitting Kernel SVM to the Training set

from sklearn.svm import SVC
classifier = SVC(kernel = 'rbf', random_state = 0)
classifier.fit(X_trainF, y_train)<jupyter_output>/usr/local/lib/python3.6/dist-packages/sklearn/svm/base.py:193: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)<jupyter_text>Test the ModelFlatten the test set first<jupyter_code># Creates a list containing 5 lists, each of 8 items, all set to 0
h, w = len(X_test), 784;
X_testF = [[0 for x in range(w)] for y in range(h)]

X_testF = np.asarray(X_testF)
for i in range(len(X_trainF)):
  X_testF[i] = X_test[i].flatten()<jupyter_output><empty_output><jupyter_text>Perform predictions<jupyter_code># Predicting the Test set results
y_pred = classifier.predict(X_testF[1:100])
y_pred.shape
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test[1:100], y_pred)
def accuracy(confusion_matrix):
    diagonal_sum = confusion_matrix.trace()
    sum_of_all_elements = confusion_matrix.sum()
    return diagonal_sum / sum_of_all_elements<jupyter_output><empty_output><jupyter_text>Perform Single Predictions<jupyter_code>accuracy(cm)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09672727272727273, 'fraction_numerical': 0.019636363636363636, 'mean_word_length': 4.819464033850494, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '348639', 'license': 'Apache-2.0', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1402, 'n_words': 403, 'path': 'Mnist_SVM.ipynb', 'repo_name': 'SamNdirangu/Image-Classification-Mnist', 'size': '4125'}