During the time of this project, I had absolutely no experience withneural networks or with TensorFlow or any of that.
So this is my orignial take on image classification using a multinomial naive bayes model. This was one of the first models I was introduced to, so that is why I chose that particular model.
I am pretty proud of this one, as I did not have the benefit of using any tutorials to help guide me. This was me trying to figure everything out as I went along and I ended up getting a decent result at the end. If you read my previous post, you’ll know that the CNN got an accuracy of about 90% (with little to no tuning of the model). Unsurprisingly, the neural network performs much better than the naive bayes model does.
Similarly to the last post, I am not going to be writing my thoughts in between each code block because each code block is pretty well commented and documented (I think so anyways).
Please enjoy my first self guided attempt at image classification!
import pandas as pd
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from google.colab import files
import matplotlib.pyplot as plt
import csv
import seaborn as sns
files.upload()
files.upload()
# READ THROUGH EACH LINE IN THE TRAINING CSV
train_image_list = []
train_label_list = []
with open("fashion-mnist_train.csv", "r") as train_file:
# CREATE THE READER THAT READS EACH LINE
reader = csv.reader(train_file, delimiter = ",")
# THIS IS HOW YOU SKIP OVER A LINE
# THE LINE BEING SKIPPED IS THE HEADER (COLUMN NAMES)
next(reader)
for row in reader:
# THE LABEL IS THE FIRST ELEMENT IN EACH ROW
# APPEND EACH LABEL TO ITS OWN ROW
label = row[0]
train_label_list.append(label)
# DELETE THE LABEL FROM EACH LIST
row.pop(0)
# WHATS LEFT IN THE LIST IS JUST THE PIXELS FOR THE IMAGES (NO LABELS)
train_image_list.append(row)
# READ THROUGH EACH LINE IN THE TEST CSV
test_image_list = []
test_label_list = []
with open("fashion-mnist_test.csv", "r") as test_file:
# CREATE THE READER THAT READS EACH LINE
reader = csv.reader(test_file, delimiter = ",")
# THIS IS HOW YOU SKIP OVER A LINE
# THE LINE BEING SKIPPED IS THE HEADER (COLUMN NAMES)
next(reader)
for row in reader:
# THE LABEL IS THE FIRST ELEMENT IN EACH ROW
# APPEND EACH LABEL TO ITS OWN ROW
label = row[0]
test_label_list.append(label)
# DELETE THE LABEL FROM EACH LIST
row.pop(0)
# WHATS LEFT IN THE LIST IS JUST THE PIXELS FOR THE IMAGES (NO LABELS)
test_image_list.append(row)
# CONVERT THE ELEMENTS IN THE LIST FROM STR --> INT
train_labels = []
for elem in train_label_list:
elem = int(elem)
train_labels.append(elem)
# CONVERT THE ELEMENTS IN EACH LIST FROM STR --> INT
train_images = []
for item in train_image_list:
for i in range(len(item)):
item[i] = int(item[i])
train_data_with_label = pd.read_csv("fashion-mnist_train.csv")
train_data_with_label.head()
<bound method NDFrame.head of label pixel1 pixel2 pixel3 ... pixel781 pixel782 pixel783 pixel784
0 2 0 0 0 ... 0 0 0 0
1 9 0 0 0 ... 0 0 0 0
2 6 0 0 0 ... 0 0 0 0
3 0 0 0 0 ... 0 0 0 0
4 3 0 0 0 ... 0 0 0 0
... ... ... ... ... ... ... ... ... ...
59995 9 0 0 0 ... 0 0 0 0
59996 1 0 0 0 ... 0 0 0 0
59997 8 0 0 0 ... 0 0 0 0
59998 8 0 0 0 ... 0 0 0 0
59999 7 0 0 0 ... 0 0 0 0
[60000 rows x 785 columns]>
test_data_with_label = pd.read_csv("fashion-mnist_test.csv")
test_data_with_label.head()
# SHOW AN EXAMPLE IMAGE OF EACH TYPE OF CLOTHING IN THE DATASET
# CREATE 10 SUBPLOTS, 2 ROWS AND 5 COLUMNS LONG WITH THE SAME X AND Y AXIS
fig, ax = plt.subplots(nrows = 2, ncols = 5, sharex = True, sharey = True)
ax = ax.flatten()
for i in range(0,10):
# FIND THE INDEX VALUE OF EVERY DIFFERENT LABEL (0-9)
# only finds the first occurance in this case
# THIS INDEX MATCHES UP WITH A PICTURE IN THE IMAGES LIST
image = train_image_list[train_labels.index(i)]
# CONVERT THE LIST OF PIXELS INTO A NUMPY ARRAY
# a list cannot be reshaped, must be an array
# go from (1, 754) to (28,28)
# RESHAPE THE ARRAY TO THE SIZE OF THE PICTURE (28,28) IN THIS CASE
image = np.asarray(image).reshape(28,28)
# EACH DIFFERENT AXIS WILL BE A DIFFERENT PIECE OF CLOTHING
ax[i].imshow(image, cmap = "Greys", interpolation = "nearest")
# GET RID OF THE X AND Y TICKS
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.show()
# CREATE A PLOT WITH THE COUNTS OF EACH LABEL
counts = sns.countplot(x = "label", data = train_data_with_label)
# CHANGE THE TITLE OF THE CHART
counts.set_title("Counts for each label")
# SET THE POSITION OF THE TICKS
# should be the same length of the number of bars
counts.set_xticks([0,1,2,3,4,5,6,7,8,9])
# CHANGE THE LABELS OF THE TICKS
# ROTATE THE LABELS SO THEY CAN BE READ AND DO NOT OVERLAP EACH OTHER
counts.set_xticklabels(["T-Shirt", "Trousers", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle Boot"],
rotation = 45)
training_labels = train_data_with_label["label"]
training_labels.reset_index(inplace = True, drop = True)
training_data = train_data_with_label.drop("label", axis = 1, inplace = False)
training_data.reset_index(inplace = True, drop = True)
print("Train label shape: ", training_labels.shape)
print("Train data shape: ", training_data.shape)
Train label shape: (60000,)
Train data shape: (60000, 784)
testing_labels = test_data_with_label["label"]
testing_labels.reset_index(inplace = True, drop = True)
testing_data = test_data_with_label.drop("label", axis = 1, inplace = False)
testing_labels.reset_index(inplace = True, drop = True)
print("Test label shape: ", testing_labels.shape)
print("Test data shape: ", testing_data.shape)
Test label shape: (10000,)
Test data shape: (10000, 784)
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
nb = MultinomialNB()
nb_model = nb.fit(training_data, training_labels)
nb_pred = nb_model.predict(testing_data)
nb_confusion = confusion_matrix(testing_labels, nb_pred)
nb_accuracy = accuracy_score(testing_labels, nb_pred)
print(nb_confusion)
print("\nNaive Bayes Accuracy: %", nb_accuracy * 100)
[[773 3 6 123 6 0 63 0 26 0]
[ 14 902 16 55 3 0 10 0 0 0]
[ 6 0 606 13 269 0 87 0 19 0]
[ 28 18 1 899 12 0 40 0 2 0]
[ 0 3 194 140 621 0 32 0 10 0]
[ 1 0 2 1 0 163 13 508 11 301]
[225 0 139 90 361 0 148 0 37 0]
[ 0 0 0 0 0 26 0 903 0 71]
[ 1 0 2 44 58 3 42 11 838 1]
[ 0 0 2 0 1 39 19 116 2 821]]
Naive Bayes Accuracy: % 66.74