In [1]:
# Import Files
import os
import sys
import time
import random
import numpy as np
import sklearn as sk
import pandas as pd
import keras
import tensorflow as tf
import matplotlib
%matplotlib inline
In [2]:
# Setting the random seed, to make the results reproducible
np.random.seed(123)
In [3]:
# List of files
files = os.listdir("./cifar_data")
print(files)
In [4]:
train_data = np.load("./cifar_data/cifar-10-train.npz")
In [5]:
# Checking the files inside the data
train_data.files
Out[5]:
In [6]:
# Train data
cifar_labels = train_data.f.label_map
cifar_train_inputs = train_data.f.inputs
cifar_train_targets = train_data.f.targets
In [7]:
# Shape of training data inputs
print("Training Input Shape: ",end="")
print(cifar_train_inputs.shape)
# 3072 = 32 * 32 * 3, image dimensions
In [8]:
# Shape of training data targets
print("Training Target Shape: ",end="")
print(cifar_train_targets.shape)
In [9]:
# Labels CIFAR-10
cifar_labels
Out[9]:
In [10]:
# Loading test file
test_data_inp = np.load("./cifar_data/cifar-10-test-inputs.npz")
test_data_tar = np.load("./cifar_data/cifar-10-test-targets.npz")
In [11]:
# Getting the data
print(test_data_inp.files)
test_data_inputs = test_data_inp.f.inputs
print(test_data_tar.files)
test_data_targets = test_data_tar.f.targets
In [12]:
# Shape of data
print("Test Input Shape: ",end="")
print(test_data_inputs.shape)
print("Test Target Shape: ",end="")
print(test_data_targets.shape)
In [13]:
# Loading Validation files
val_data = np.load("./cifar_data/cifar-10-valid.npz")
In [14]:
# Checking Validation Files
val_data.files
Out[14]:
In [15]:
# Processing the data
val_data_labels = val_data.f.label_map
val_data_inputs = val_data.f.inputs
val_data_targets = val_data.f.targets
In [16]:
# Getting the shapes
print("Shape of Validation Inputs", end="")
print(val_data_inputs.shape)
print("Shape of Validation Targets", end="")
print(val_data_targets.shape)
print("Validation Data Labels")
print(val_data_labels)
In [55]:
from sklearn.linear_model import LogisticRegression
In [56]:
clf_logistic = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial').fit(cifar_train_inputs, cifar_train_targets)
In [57]:
val_predictions = clf_logistic.predict(val_data_inputs)
In [22]:
from sklearn.metrics import accuracy_score
In [65]:
logreg_acc = accuracy_score(val_data_targets, val_predictions)
In [73]:
print("Logistic Regression Validation Accuracy: ")
print(logreg_acc)
In [74]:
test_predictions = clf_logistic.predict(test_data_inputs)
In [75]:
logreg_acc = accuracy_score(test_data_targets, test_predictions)
print("Logistic Regression Test Accuracy: ")
print(logreg_acc)
In [69]:
from sklearn.svm import SVC
In [71]:
clf_svm = SVC(gamma='auto')
In [76]:
clf_svm.fit(cifar_train_inputs, cifar_train_targets)
Out[76]:
In [77]:
val_predictions = clf_svm.predict(val_data_inputs)
In [78]:
svm_acc = accuracy_score(val_data_targets, val_predictions)
In [79]:
print("SVM Validation Accuracy: ")
print(svm_acc)
In [80]:
test_predictions = clf_svm.predict(test_data_inputs)
In [81]:
svm_acc = accuracy_score(test_data_targets, test_predictions)
print("SVM Test Accuracy: ")
print(svm_acc)
In [82]:
from sklearn.ensemble import RandomForestClassifier
In [83]:
clf_randforest = RandomForestClassifier(n_estimators=100, max_depth=2,random_state=0)
In [84]:
clf_randforest.fit(cifar_train_inputs, cifar_train_targets)
Out[84]:
In [85]:
val_predictions = clf_randforest.predict(val_data_inputs)
In [86]:
randforest_acc = accuracy_score(val_data_targets, val_predictions)
In [88]:
print("Random Forest Validation Accuracy: ")
print(randforest_acc)
In [89]:
test_predictions = clf_randforest.predict(test_data_inputs)
In [90]:
randforest_acc = accuracy_score(test_data_targets, test_predictions)
print("Random Forest Test Accuracy: ")
print(randforest_acc)
In [91]:
from sklearn.neural_network import MLPClassifier
In [92]:
clf_mlp = MLPClassifier(alpha=1)
In [93]:
clf_mlp.fit(cifar_train_inputs, cifar_train_targets)
Out[93]:
In [94]:
val_predictions = clf_mlp.predict(val_data_inputs)
In [95]:
mlp_acc = accuracy_score(val_data_targets, val_predictions)
In [96]:
print("MLP Validation Accuracy: ")
print(mlp_acc)
In [97]:
test_predictions = clf_mlp.predict(test_data_inputs)
In [98]:
mlp_acc = accuracy_score(test_data_targets, test_predictions)
print("Random Forest Test Accuracy: ")
print(mlp_acc)
In [17]:
from sklearn.neighbors import KNeighborsClassifier
In [18]:
clf_knn = KNeighborsClassifier(3)
In [19]:
clf_knn.fit(cifar_train_inputs, cifar_train_targets)
Out[19]:
In [20]:
val_predictions = clf_knn.predict(val_data_inputs)
In [23]:
knn_acc = accuracy_score(val_data_targets, val_predictions)
In [24]:
print("KNN Validation Accuracy: ")
print(knn_acc)
In [25]:
test_predictions = clf_knn.predict(test_data_inputs)
In [26]:
knn_acc = accuracy_score(test_data_targets, test_predictions)
print("KNN Forest Test Accuracy: ")
print(knn_acc)
In [17]:
from sklearn.ensemble import AdaBoostClassifier
In [18]:
clf_ada = AdaBoostClassifier()
In [19]:
clf_ada.fit(cifar_train_inputs, cifar_train_targets)
Out[19]:
In [20]:
val_predictions = clf_ada.predict(val_data_inputs)
In [23]:
ada_acc = accuracy_score(val_data_targets, val_predictions)
In [24]:
print("ADA Validation Accuracy: ")
print(ada_acc)
In [25]:
test_predictions = clf_ada.predict(test_data_inputs)
In [26]:
ada_acc = accuracy_score(test_data_targets, test_predictions)
print("ADA Forest Test Accuracy: ")
print(ada_acc)
In [27]:
from sklearn.naive_bayes import GaussianNB
In [28]:
clf_nb = GaussianNB()
In [29]:
clf_nb.fit(cifar_train_inputs, cifar_train_targets)
Out[29]:
In [30]:
val_predictions = clf_nb.predict(val_data_inputs)
In [31]:
nb_acc = accuracy_score(val_data_targets, val_predictions)
In [32]:
print("NB Validation Accuracy: ")
print(nb_acc)
In [33]:
test_predictions = clf_nb.predict(test_data_inputs)
In [34]:
nb_acc = accuracy_score(test_data_targets, test_predictions)
print("NB Forest Test Accuracy: ")
print(nb_acc)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: