Skip to content
Snippets Groups Projects
Commit 00d6cf8f authored by pierre-cau's avatar pierre-cau
Browse files

evaluate_knn

parent 32c1ecfe
No related branches found
No related tags found
No related merge requests found
......@@ -15,3 +15,11 @@ if __name__ == "__main__":
print(f" - Training data shape: {data_train.shape}, Training labels shape: {labels_train.shape}")
print(f" - Testing data shape: {data_test.shape}, Testing labels shape: {labels_test.shape}")
# We flatten the images
data_train = data_train.reshape(data_train.shape[0], -1)
data_test = data_test.reshape(data_test.shape[0], -1)
# Evaluate the k-NN algorithm
k = 3
accuracy = evaluate_knn(data_train, labels_train, data_test, labels_test, k)
\ No newline at end of file
......@@ -4,6 +4,7 @@ ROOT = r"../../.."
from .read_cifar import *
from .split_data import *
from .knn import *
No preview for this file type
File added
......@@ -7,9 +7,9 @@ def distance_matrix(matrix1, matrix2):
Parameters
----------
matrix1 : np.ndarray
First matrix of shape (n1, d).
First matrix of shape (n1, d). [must be the test set]
matrix2 : np.ndarray
Second matrix of shape (n2, d).
Second matrix of shape (n2, d). [must be the train set]
Returns
-------
......@@ -67,6 +67,39 @@ def knn_predict(dists, labels_train, k):
return labels_pred
def evaluate_knn(data_train, labels_train, data_test, labels_test, k):
"""
Evaluate the k-nearest neighbors algorithm on the given dataset.
Parameters
----------
data_train : np.ndarray
Training data of shape (n_train, d).
labels_train : np.ndarray
Training labels of shape (n_train,).
data_test : np.ndarray
Testing data of shape (n_test, d).
labels_test : np.ndarray
Testing labels of shape (n_test,).
k : int
Number of neighbors to consider.
Returns
-------
accuracy : float
Classification rate (accuracy) of the k-NN algorithm on the test set.
"""
# Compute the distance matrix between the testing and training data
dists = distance_matrix(data_test, data_train)
# Predict the labels for the test set
labels_pred = knn_predict(dists, labels_train, k)
# Compute the accuracy
accuracy = np.mean(labels_pred == labels_test)
return accuracy
if __name__ == "__main__":
# Example data and labels
data_train = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
......@@ -78,10 +111,16 @@ if __name__ == "__main__":
print("Test data:", data_test)
# Compute the distance matrix
dists = distance_matrix(data_train, data_test)
dists = distance_matrix(data_test,data_train)
# Predict the labels for the test set
k = 3
k = 2
labels_pred = knn_predict(dists, labels_train, k)
print("Predicted labels:", labels_pred)
# Evaluate the k-NN algorithm
labels_test = np.array([0, 1])
accuracy = evaluate_knn(data_train, labels_train, data_test, labels_test, k)
print(f"Accuracy: {accuracy}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment