-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b48c3f2
Showing
491 changed files
with
2,665 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.idea | ||
/venv | ||
/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numpy as np | ||
from collections import Counter | ||
|
||
|
||
def euclidean_distance(x1, x2): | ||
return np.sqrt(np.sum((x1 - x2) ** 2)) | ||
|
||
|
||
class KNN: | ||
|
||
def __init__(self, k=3): | ||
self.k = k | ||
|
||
def fit(self, X, y): | ||
self.X_train = X | ||
self.y_train = y | ||
|
||
def predict(self, X): | ||
y_pred = [self._predict(x) for x in X] | ||
return np.array(y_pred) | ||
|
||
def _predict(self, x): | ||
# Compute distances between x and all examples in the training set | ||
distances = [euclidean_distance(x, x_train) | ||
for x_train in self.X_train] | ||
# Sort by distance and return indices of the first k neighbors | ||
k_idx = np.argsort(distances)[:self.k] | ||
# Extract the labels of the k nearest neighbor training samples | ||
k_neighbor_labels = [self.y_train[i] for i in k_idx] | ||
# return the most common class label | ||
most_common = Counter(k_neighbor_labels).most_common(1) | ||
return most_common[0][0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import numpy as np | ||
|
||
|
||
class LDA: | ||
|
||
def __init__(self, n_components): | ||
self.n_components = n_components | ||
self.linear_discriminants = None | ||
|
||
def fit(self, X, y): | ||
n_features = X.shape[1] | ||
class_labels = np.unique(y) | ||
|
||
# Within class scatter matrix: | ||
# SW = sum((X_c - mean_X_c)^2 ) | ||
|
||
# Between class scatter: | ||
# SB = sum( n_c * (mean_X_c - mean_overall)^2 ) | ||
|
||
mean_overall = np.mean(X, axis=0) | ||
SW = np.zeros((n_features, n_features)) | ||
SB = np.zeros((n_features, n_features)) | ||
for c in class_labels: | ||
X_c = X[y == c] | ||
mean_c = np.mean(X_c, axis=0) | ||
SW += (X_c - mean_c).T.dot((X_c - mean_c)) | ||
|
||
n_c = X_c.shape[0] | ||
mean_diff = (mean_c - mean_overall).reshape(n_features, 1) | ||
SB += n_c * (mean_diff).dot(mean_diff.T) | ||
|
||
# Determine SW^-1 * SB | ||
A = np.linalg.pinv(SW).dot(SB) | ||
# Get eigenvalues and eigenvectors of SW^-1 * SB | ||
eigenvalues, eigenvectors = np.linalg.eigh(A) | ||
# -> eigenvector v = [:,i] column vector, transpose for easier calculations | ||
# sort eigenvalues high to low | ||
eigenvectors = eigenvectors.T | ||
idxs = np.argsort(abs(eigenvalues))[::-1] | ||
eigenvalues = eigenvalues[idxs] | ||
eigenvectors = eigenvectors[idxs] | ||
# store first n eigenvectors | ||
self.linear_discriminants = eigenvectors[0:self.n_components] | ||
return self.linear_discriminants | ||
|
||
def transform(self, X): | ||
# project data | ||
X = X - np.mean(X, axis=0) | ||
return np.dot(X, self.linear_discriminants.T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
|
||
|
||
class PCA: | ||
|
||
def __init__(self, alpha): | ||
self.alpha = alpha | ||
self.components = None | ||
self.mean = None | ||
self.n_components = None | ||
self.eigenvalues = None | ||
self.eigenvectors = None | ||
|
||
def fit(self, X): | ||
# Mean centering | ||
self.mean = np.mean(X, axis=0) | ||
X = X - self.mean | ||
# covariance, function needs samples as columns | ||
cov = np.cov(X.T) | ||
# eigenvalues, eigenvectors | ||
if not self.eigenvalues: | ||
self.eigenvalues, self.eigenvectors = np.linalg.eigh(cov) | ||
# -> eigenvector v = [:,i] column vector, transpose for easier calculations | ||
# sort eigenvectors | ||
self.eigenvectors = self.eigenvectors.T | ||
idxs = np.argsort(self.eigenvalues)[::-1] | ||
self.eigenvalues = self.eigenvalues[idxs] | ||
self.eigenvectors = self.eigenvectors[idxs] | ||
# find number of n_components satisfying alpha | ||
self.n_components = 1 | ||
while (self.n_components): | ||
f = sum(self.eigenvalues[:self.n_components]) / sum(self.eigenvalues) | ||
if f < self.alpha: | ||
self.n_components += 1 | ||
else: | ||
break | ||
# store first n eigenvectors (projection matrix) | ||
self.components = self.eigenvectors[0:self.n_components] | ||
|
||
def transform(self, X): | ||
# project data | ||
X = X - np.mean(X, axis=0) | ||
return np.dot(X, self.components.T) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
The ORL face database | ||
--------------------- | ||
|
||
This directory contains a set of faces taken between April 1992 and | ||
April 1994 at the Olivetti Research Laboratory in Cambridge, UK. | ||
|
||
There are 10 different images of 40 distinct subjects. For some of the | ||
subjects, the images were taken at different times, varying lighting | ||
slightly, facial expressions (open/closed eyes, smiling/non-smiling) | ||
and facial details (glasses/no-glasses). All the images are taken | ||
against a dark homogeneous background and the subjects are in | ||
up-right, frontal position (with tolerance for some side movement). | ||
|
||
The files are in PGM format and can be conveniently viewed using the 'xv' | ||
program. The size of each image is 92x112, 8-bit grey levels. The images | ||
are organised in 40 directories (one for each subject) named as: | ||
|
||
sX | ||
|
||
where X indicates the subject number (between 1 and 40). In each directory | ||
there are 10 different images of the selected subject named as: | ||
|
||
Y.pgm | ||
|
||
where Y indicates which image for the specific subject (between 1 and 10). | ||
|
||
When using these images, please give credit to Olivetti Research Laboratory. | ||
A convenient reference is the face recognition work which uses some of | ||
these images: | ||
|
||
F. Samaria and A. Harter | ||
"Parameterisation of a stochastic model for human face identification" | ||
2nd IEEE Workshop on Applications of Computer Vision | ||
December 1994, Sarasota (Florida). | ||
|
||
The paper is available via anonymous ftp from quince.cam-orl.co.uk and is | ||
stored in pub/users/fs/IEEE_workshop.ps.Z | ||
|
||
If you have any question, please email Ferdinando Samaria: [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.