<jupyter_start><jupyter_text>Importing necessary libraries<jupyter_code>import torch
from torchvision import datasets, transforms
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score<jupyter_output><empty_output><jupyter_text>Downloading CIFAR-10 dataset<jupyter_code>transform = transforms.Compose([transforms.Resize((32,32)),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
                               ])
train_set = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) # Data augmentation is only 
                                                                                            #done on training images
test_set = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')<jupyter_output><empty_output><jupyter_text>Reading and plotting images from dataset<jupyter_code>print('Training set samples:', len(train_set))
print('Test set samples:', len(test_set))
def im_convert(tensor):  
  image = tensor.cpu().clone().detach().numpy() # This process will happen in normal cpu.
  image = image.transpose(1, 2, 0)
  image = image * np.array((0.5, 0.5, 0.5)) + np.array((0.5, 0.5, 0.5))
  image = image.clip(0, 1)
  return image
train_loader = torch.utils.data.DataLoader(train_set, batch_size = 20)
test_loader = torch.utils.data.DataLoader(test_set, batch_size = 20 )
print(len(train_loader))
print(len(test_loader))
dataiter = iter(train_loader)
images, labels = dataiter.next()
print(images[2])
print(type(images[2]))
print(images[2].shape)<jupyter_output><class 'torch.Tensor'>
torch.Size([3, 32, 32])<jupyter_text>Plotting 20 images<jupyter_code>dataiter = iter(train_loader)
images, labels = dataiter.next()
fig = plt.figure(figsize=(25, 4)) 

# We plot 20 images from our train_dataset
for idx in np.arange(20):
  ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[]) 
  plt.imshow(im_convert(images[idx])) #converting to numpy array as plt needs it.
  ax.set_title(classes[labels[idx].item()])<jupyter_output><empty_output><jupyter_text>Plotting a single image from trainset<jupyter_code>dataiter = iter(train_loader)
images, labels = dataiter.next()
plt.imshow(im_convert(images[7]))
plt.show()<jupyter_output><empty_output><jupyter_text>Plotting a single image from test set<jupyter_code>dataiter = iter(test_loader)
images, labels = dataiter.next()
plt.imshow(im_convert(images[9]))
plt.show()<jupyter_output><empty_output><jupyter_text>Converting dataset tensor to Numpy<jupyter_code># Separating trainset/testset data/label
Xtr  = train_set.data
Xte  = test_set.data
Ytr  = np.array(train_set.targets)
Yte   = np.array(test_set.targets)

# flatten out all images to be one-dimensional
Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3) # converting Xtr to Xtr_rows as 50000 x 3072
Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3) # converting Xte to Xte_rows as 10000 x 3072
print(type(Ytr))
Ytr
Xtr_rows[1]
print(type(Xtr))
print(type(Xte))
print(Xtr.shape)
print(Xtr_rows.shape)
print(Xtr_rows[1].shape)
print(Xtr[2])
print(type(Xtr[2]))<jupyter_output>[[[255 255 255]
  [253 253 253]
  [253 253 253]
  ...
  [253 253 253]
  [253 253 253]
  [253 253 253]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [254 254 254]
  [254 254 254]
  ...
  [254 254 254]
  [254 254 254]
  [254 254 254]]

 ...

 [[113 120 112]
  [111 118 111]
  [105 112 106]
  ...
  [ 72  81  80]
  [ 72  80  79]
  [ 72  80  79]]

 [[111 118 110]
  [104 111 104]
  [ 99 106  98]
  ...
  [ 68  75  73]
  [ 70  76  75]
  [ 78  84  82]]

 [[106 113 105]
  [ 99 106  98]
  [ 95 102  94]
  ...
  [ 78  85  83]
  [ 79  85  83]
  [ 80  86  84]]]
<class 'numpy.ndarray'><jupyter_text>Nearest Neighbor Algorithm<jupyter_code>class NearestNeighbor(object):
  def __init__(self):
    pass

  def train(self, X, y):
    self.Xtr = X
    self.ytr = y

  def predict(self, X):
    num_test = X.shape[0]

    Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
    
    
    for i in range(num_test):
      distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
      min_index = np.argmin(distances) 
      Ypred[i] = self.ytr[min_index] 
    
    return Ypred  
classifier = NearestNeighbor()
classifier.train(Xtr_rows, Ytr)                 #Training images from trainset where Ytr true value
prediction = classifier.predict(Xte_rows)       #Testing images from test set  
print(prediction)
print(len(prediction))<jupyter_output>[8 8 1 ... 5 4 2]
10000<jupyter_text>Printing Accuracy, confusion matrix and F-1 scores for Nearest Neighbor Algorithm<jupyter_code>y_actual =  Yte.tolist()

nn_predicted = prediction.astype('int32').tolist()
 
data = {'y_actual':  y_actual,
        'nn_predicted': nn_predicted
        }
from sklearn.metrics import confusion_matrix
confusion_matrix(y_actual, nn_predicted)

ac = accuracy_score(y_actual, nn_predicted)
print('Accuracy is: ', ac )
df = pd.DataFrame(data, columns=['y_actual','nn_predicted'])
confusion_matrix = pd.crosstab(df['y_actual'], df['nn_predicted'], rownames=['Actual'], colnames=['Predicted'])
print ('Confusion Matrix is: \n\n',confusion_matrix)
f1 = f1_score(y_actual, nn_predicted, average=None)
print('F-1 Score is: ', f1)<jupyter_output>F-1 Score is:  [0.31652069 0.18876404 0.23489681 0.1824     0.23473282 0.25313808
 0.20072993 0.19791667 0.36886503 0.15041322]<jupyter_text>K Nearest Neighbor Algorithm (KNN)<jupyter_code>print(Xtr_rows.shape)
print(Ytr.shape)
print(Xte_rows.shape)
class KNearestNeighbor(object):
  def __init__(self):
    pass

  def train(self, X, y):
    self.Xtr = X
    self.ytr = y

  def predict(self, X, k):
    num_test = X.shape[0]

    Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
    
    for i in range(num_test):
      distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
      sorted_distance = np.argsort(distances)[:k]
        
      if(np.size(np.where(np.bincount(self.ytr[sorted_distance]) > 1)[0])==0):
         Ypred[i] = self.ytr[sorted_distance[0]];
      else:
         Ypred[i] = np.bincount(self.ytr[sorted_distance]).argmax();
          
    return Ypred  


knn_predict = np.zeros(shape=(2,Xte_rows.shape[0]));

index = 0;
for k in [3,5]:
    knn_classifier = KNearestNeighbor()
    knn_classifier.train(Xtr_rows, Ytr)
    knn_predict[index] = knn_classifier.predict(Xte_rows,k) 
    index = index + 1<jupyter_output><empty_output><jupyter_text>Printing accuracy, confusion matrix and F-1 score for K- Nearest Neighbor Algorithm<jupyter_code>K = [3,5]
for i in range(knn_predict.shape[0]):
    knn_predicted = knn_predict[i].astype('int32').tolist() #KNN_y_Predicted = knn_predicted
    print("For K = ",K[i])
    print()
    data = {'y_actual':  y_actual,
            'knn_predicted': knn_predicted
            }

    df = pd.DataFrame(data, columns=['y_actual','knn_predicted'])
    confusion_matrix = pd.crosstab(df['y_actual'], df['knn_predicted'], rownames=['Actual'], colnames=['Predicted'])
    print (confusion_matrix)
    print("\n\nAccuracy: ", accuracy_score(y_actual, knn_predicted))
    print()
    print ("F1 Score: ")
    print(f1_score(y_actual, knn_predicted, average=None))
    print()<jupyter_output>For K =  3

Predicted    0    1    2    3    4    5    6    7    8   9
Actual                                                    
0          577   18  123   21   31   13   37   11  162   7
1          206  121   90   61   87   64   51   29  250  41
2          273    2  322   71  133   57   39   17   76  10
3          217   16  149  158  103  150   89   24   85   9
4          189    6  273   58  233   75   49   14  100   3
5          213    6  139  130   93  231   54   37   91   6
6          134    6  243   98  145  120  163   10   78   3
7          201   14  163   93  158   64   63  133   97  14
8          283   32   47   24   34   28   17   16  511   8
9          223   80   68   66   61   68   46   46  260  82


Accuracy:  0.2531

F1 Score: 
[0.32821388 0.18601076 0.2460833  0.17752809 0.22425409 0.24705882
 0.20273632 0.19895288 0.37712177 0.1386306 ]

For K =  5

Predicted    0    1    2    3    4    5    6    7    8   9
Actual                                                    
0   [...]
{'fraction_non_alphanumeric': 0.12621474283005452, 'fraction_numerical': 0.10322351268073003, 'mean_word_length': 2.9806603773584905, 'pattern_counts': {'":': 0, '<': 42, '<?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': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '485152', 'license': 'MIT', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3143, 'n_words': 754, 'path': 'Slutions/Assignmen_01.ipynb', 'repo_name': 'taareek/neural_netwok', 'size': '8438'}