Skip to content
Snippets Groups Projects
Unverified Commit 6e2e532b authored by Jangberry (Nomad-Debian)'s avatar Jangberry (Nomad-Debian)
Browse files

Pep8

parent cab6108d
Branches
No related tags found
No related merge requests found
{
"python.analysis.autoImportCompletions": true,
"python.analysis.typeCheckingMode": "off"
}
\ No newline at end of file
...@@ -3,10 +3,22 @@ ...@@ -3,10 +3,22 @@
## Setup ## Setup
1. Download the [CIFAR dataset](https://www.cs.toronto.edu/~kriz/cifar.html) `cifar-10-batches-py` in the [data](./data/) folder. 1. Download the [CIFAR dataset](https://www.cs.toronto.edu/~kriz/cifar.html) `cifar-10-batches-py` in the [data](./data/) folder.
1. You might want to create a venv with `python -m venv .venv` and activate it with `source .venv/bin/activate`. 1. (*optionnal*) You might want to create a venv with `python -m venv .venv` and activate it with `source .venv/bin/activate`.
1. Install the requirements using `pip install -r requirements.txt`. 1. Install the requirements using `pip install -r requirements.txt`.
## Usage ## Usage
To test knn, simply run [knn.py](./knn.py) using `python knn.py`. To test knn, simply run [knn.py](./knn.py) using `python knn.py`.
Otherwise here is a test result: ![knn test result](./results/knn.png) Otherwise here is a test result: ![knn test result](./results/knn.png)
## Some proofs
### *1. Prove that $`\sigma' = \sigma \times (1-\sigma)`$*
To prove that, let's firt derive the sigmoid function:
$\sigma(x) = \frac{1}{1+e^{-x}}$
so $\sigma'(x)=\frac{e^{-x}}{(1+e^{-x})^2}$
$\sigma'(x)=\frac{1}{1+e^{-x}}\times\frac{e^{-x}}{1+e^{-x}}$
Here we can identify $\frac{1}{1+e^{-x}} = \sigma(x)$
And $\frac{e^{-x}}{1+e^{-x}} = 1 - \frac{1}{1+e^{x}}$
So $\sigma'(x) = \sigma(x) \times (1 - \sigma(x))$
\ No newline at end of file
import read_cifar
import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
import read_cifar
def distance_matrix(X: np.array, Y: np.array): def distance_matrix(X: np.array, Y: np.array):
"""Compute the L2 distance between two matricies """Compute the L2 distance between two matricies
...@@ -12,9 +14,12 @@ def distance_matrix(X: np.array, Y: np.array): ...@@ -12,9 +14,12 @@ def distance_matrix(X: np.array, Y: np.array):
dist -- distance matrix (shape: (X.shape[0], Y.shape[0])) => dist[i, j] = L2(X[i], Y[j]) dist -- distance matrix (shape: (X.shape[0], Y.shape[0])) => dist[i, j] = L2(X[i], Y[j])
""" """
return np.sqrt( return np.sqrt(
np.sum(np.square(X), axis=1).reshape((-1, 1))@np.ones((1, Y.shape[0])) + # X² # X² + Y² - 2XY
np.ones((X.shape[0], 1))@np.sum(np.square(Y), axis=1).reshape((1, -1)) # Y² np.sum(np.square(X), axis=1).reshape((-1, 1))@np.ones((1, Y.shape[0])) +
- 2*X@Y.T) # -2XY np.ones((X.shape[0], 1))@np.sum(np.square(Y),
axis=1).reshape((1, -1))
- 2*X@Y.T)
def knn_predict(dist: np.array, labels_train: np.array, k: int): def knn_predict(dist: np.array, labels_train: np.array, k: int):
"""Predict the labels of the test set using the k-nearest neighbors algorithm """Predict the labels of the test set using the k-nearest neighbors algorithm
...@@ -31,10 +36,12 @@ def knn_predict(dist: np.array, labels_train: np.array, k: int): ...@@ -31,10 +36,12 @@ def knn_predict(dist: np.array, labels_train: np.array, k: int):
# Get the labels of the k nearest neighbors # Get the labels of the k nearest neighbors
labels = labels_train[indices] labels = labels_train[indices]
# Get the most frequent label # Get the most frequent label
labels = np.apply_along_axis(lambda x: np.bincount(x).argmax(), axis=1, arr=labels) labels = np.apply_along_axis(
lambda x: np.bincount(x).argmax(), axis=1, arr=labels)
return labels return labels
def evaluate_knn(data_train: np.array, labels_train: np.array, data_test: np.array, labels_test: np.array, k: int, dist=None): def evaluate_knn(data_train: np.array, labels_train: np.array, data_test: np.array, labels_test: np.array, k: int, dist=None):
"""Evaluate the k-nearest neighbors algorithm """Evaluate the k-nearest neighbors algorithm
...@@ -62,7 +69,8 @@ if __name__ == "__main__": ...@@ -62,7 +69,8 @@ if __name__ == "__main__":
# Split the data into training and testing sets # Split the data into training and testing sets
print("Splitting sets") print("Splitting sets")
images_train, labels_train, images_test, labels_test = read_cifar.split_dataset(images, labels, split_factor) images_train, labels_train, images_test, labels_test = read_cifar.split_dataset(
images, labels, split_factor)
# Compute the distance matrix # Compute the distance matrix
print("Computing the distance matrix...") print("Computing the distance matrix...")
...@@ -72,7 +80,8 @@ if __name__ == "__main__": ...@@ -72,7 +80,8 @@ if __name__ == "__main__":
accuracies = [] # List of the accuracy accuracies = [] # List of the accuracy
ks = [] # List to make sure the plot starts at one and not 0 ks = [] # List to make sure the plot starts at one and not 0
for k in range(1, 21): for k in range(1, 21):
accuracy = evaluate_knn(images_train, labels_train, images_test, labels_test, k) accuracy = evaluate_knn(
images_train, labels_train, images_test, labels_test, k)
print(f"Accuracy for k = {k}: {accuracy}") print(f"Accuracy for k = {k}: {accuracy}")
accuracies.append(accuracy) accuracies.append(accuracy)
ks.append(k) ks.append(k)
......
numpy numpy
matplotlib
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment