Skip to content
Snippets Groups Projects
Commit b4689a40 authored by Saidi Aya's avatar Saidi Aya
Browse files

Update knn.py

parent 4b2d6c8e
No related branches found
No related tags found
No related merge requests found
#Libraries
import numpy as np
import torch
#Functions
def distance_matrix(Y , X):
#This function takes as parameters two matrices X and Y
......@@ -14,22 +13,38 @@ def distance_matrix(Y , X):
def knn_predict(dists, labels_train, k):
#This function takes as parameters: dists (from above), labels_train, and k the number of neighbors
labels_test_pred=torch.zeros(len(data_test), dtype=torch.int64)
for i in range(dists.shape[1]):
# Find index of k lowest values
x = torch.topk(dists[:,i], k, largest=False).indices
# Index the labels according to x
k_lowest_labels = labels_train[x]
# y_test_pred[i] = the most frequent occuring index
labels_test_pred[i] = torch.argmax(torch.bincount(k_lowest_labels))
return labels_test_pred
labels_pred=np.zeros(labels_train.shape[0])
for i in range(0,dists.shape[0]):
# Find index of k smallest distances
index_smallest_distance = np.argsort(dists[i,:])[0:k+1]
# Index the labels according to these distances
labels_distances = [labels_train[i] for i in index_smallest_distance]
#Predict the class / label
labels_pred[i]=max(labels_distances,key=labels_distances.count)
return labels_pred
def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
labels_test_pred=knn_predict(distance_matrix(data_train, data_test), labels_train, k)
#This function evaluates the knn classifier rate
labels_test__pred=knn_predict(distance_matrix(data_train, data_test), labels_train, k)
num_samples= data_test.shape[0]
num_correct= (labels_test == labels_test_pred).sum().item()
accuracy= 100 * num_correct / num_samples
accuracy= 100 * (num_correct / num_samples) #The accuracy is the percentage of the correctly predicted classes
return accuracy
def accuracy_graph(k,dirname,num_batch):
#This function is used to plot the variation of the accuracy as a function of k
# k -- the max number of neighbors
x=[] #axis x : k
y=[] #axis y : accuracy
dir_batch=str(dirname)+"\\data\\cifar-10-batches-py\\data_batch_"+str(num_batch)
dir_test = str(dirname)+"\\data\\cifar-10-batches-py\\test_batch"
(data_test, labels_test)=read_cifar_batch(dir_test)
(data_train, labels_train)=read_cifar_batch(dir_batch)
for i in range (1,k+1):
x.append(i) #axis (k from 1 to 20)
accuracy=evaluate_knn(data_train , labels_train , data_test , labels_test , i)
y.append(accuracy)
plt.plot(x,y)
plt.Show
plt.savefig(str(dirname)+"results")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment