using Colab

from keras.datasets import cifar10 

(Xtr, Ytr), (Xte, Yte) = cifar10.load_data()

# flatten out all images to be one-dimensional
Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3) # Xtr_rows becomes 50000 x 3072
Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3) # Xte_rows becomes 10000 x 3072
Using TensorFlow backend.
Downloading data from <https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz>
170500096/170498071 [==============================] - 11s 0us/step

1-Neareset Neighbor(L1 distance)

import numpy as np

class NearestNeighbor(object):
  def __init__(self):
    pass

  def train(self, X, y):
    """ X is N x D where each row is an example. Y is 1-dimension of size N """
    # the nearest neighbor classifier simply remembers all the training data
    self.Xtr = X
    self.ytr = y

  def predict(self, X):
    """ X is N x D where each row is an example we wish to predict label for """
    num_test = X.shape[0]
    # lets make sure that the output type matches the input type
    Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
    # loop over all test rows
    for i in range(num_test):
      # find the nearest training image to the i'th test image
      # using the L1 distance (sum of absolute value differences)
      distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
      min_index = np.argmin(distances) # get the index with smallest distance
      Ypred[i] = self.ytr[min_index] # predict the label of the nearest example

    return Ypred

nn = NearestNeighbor() # create a Nearest Neighbor classifier class
nn.train(Xtr_rows, Ytr) # train the classifier on the training images and labels
Yte_predict = nn.predict(Xte_rows) # predict labels on the test images

count = 0
for i in range(len(Yte_predict)):
  if Yte_predict[i] == Yte[i]:
    count +=1

# and now print the classification accuracy, which is the average number
# of examples that are correctly predicted (i.e. label matches)
print('accuracy: %f' %(count/len(Yte_predict)))

→ accuracy: 0.249200

1-Neareset Neighbor(L2 distance)

import numpy as np

class NearestNeighbor(object):
  def __init__(self):
    pass

  def train(self, X, y):
    """ X is N x D where each row is an example. Y is 1-dimension of size N """
    # the nearest neighbor classifier simply remembers all the training data
    self.Xtr = X
    self.ytr = y

  def predict(self, X):
    """ X is N x D where each row is an example we wish to predict label for """
    num_test = X.shape[0]
    # lets make sure that the output type matches the input type
    Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
    # loop over all test rows
    for i in range(num_test):
      # find the nearest training image to the i'th test image
      # using the L1 distance (sum of absolute value differences)
      distances = np.sqrt(np.sum(np.square(self.Xtr - X[i,:]), axis = 1))
      min_index = np.argmin(distances) # get the index with smallest distance
      Ypred[i] = self.ytr[min_index] # predict the label of the nearest example

    return Ypred

nn = NearestNeighbor() # create a Nearest Neighbor classifier class
nn.train(Xtr_rows, Ytr) # train the classifier on the training images and labels
Yte_predict = nn.predict(Xte_rows) # predict labels on the test images

count = 0
for i in range(len(Yte_predict)):
  if Yte_predict[i] == Yte[i]:
    count +=1

# and now print the classification accuracy, which is the average number
# of examples that are correctly predicted (i.e. label matches)
print('accuracy: %f' %(count/len(Yte_predict)))

→ accuracy : 0.2535

K-Neareset Neighbor(L2 distance)

import numpy as np

class K_NearestNeighbor(object):
  def __init__(self):
    pass

  def train(self, X, y):
    """ X is N x D where each row is an example. Y is 1-dimension of size N """
    # the nearest neighbor classifier simply remembers all the training data
    self.Xtr = X
    self.ytr = y

  def predict(self, X, K):
    """ X is N x D where each row is an example we wish to predict label for """
    num_test = X.shape[0]
    # lets make sure that the output type matches the input type
    Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
    # loop over all test rows
    for i in range(num_test):
      # find the nearest training image to the i'th test image
      # using the L1 distance (sum of absolute value differences)
      distances = np.sqrt(np.sum(np.square(self.Xtr - X[i,:]), axis = 1))
      min_pred = []
      for j in range(K):
        min_pred.append(self.ytr[np.argmin(distances)]) # get the index with smallest distance
        np.delete(distances, np.argmin(distances))
      Ypred[i] = majorityElement(min_pred) # predict the label of the nearest example

    return Ypred

def majorityElement(num):
        candidate = None
        count = 0
        for n in num:
            if candidate == None:
                candidate = n
                count += 1
            elif n == candidate:
                count += 1
            elif n != candidate and count == 1:
                candidate = None
                count -= 1
            elif n != candidate:
                count -= 1
        return candidate

k = 5 # find the top k closest images
knn = K_NearestNeighbor() # create a K - Nearest Neighbor classifier class
knn.train(Xtr_rows, Ytr) # train the classifier on the training images and labels
Yte_predict = knn.predict(Xte_rows, k) # predict labels on the test images

count = 0
for i in range(len(Yte_predict)):
  if Yte_predict[i] == Yte[i]:
    count +=1

# and now print the classification accuracy, which is the average number
# of examples that are correctly predicted (i.e. label matches)
print('accuracy: %f' %(count/len(Yte_predict)))