Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mostafatalaat770 committed May 5, 2021
0 parents commit b48c3f2
Show file tree
Hide file tree
Showing 491 changed files with 2,665 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/venv
/__pycache__
32 changes: 32 additions & 0 deletions KNN.py
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]
49 changes: 49 additions & 0 deletions LDA.py
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)
43 changes: 43 additions & 0 deletions PCA.py
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 added Report.pdf
Binary file not shown.
39 changes: 39 additions & 0 deletions att_faces/README
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]
4 changes: 4 additions & 0 deletions att_faces/s1/1.pgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
P5
92 112
255
01-/19'*515<L[c_PKB6/12+.5=FTi��n^Qk_P97BVPJAG>T4JGC@XDGKB9=>4/2:<@B9.6BPPDGW@MBSM:.)+873886-4'.8-'/0('2,EFCH;343045@KQUbnk�meP]^L:QNSNHM<J=KOJ@]CEF532;73///==>/,@KN:BRQVIFD9/&''35423-2*33-(0,%$%,14+83/,.=HMOUdkoz�v{qoUPPSE>IdSVPILCEPWOUDH87/+-1+49@G;7/->PLPP[X^RF?64/)!130521.//2/*-(,==1A>=6CM=579@D>GapYq�qgQ^JIKK@K_`][POIDSRVVCK:5178557:A9:502>ZWY_\XZ[TOH9.'#++23../00,+,<62EHB?>0A96';JE@MTRXa\Z{_SDMEGHGM_ei\OXORVR\TNI:8=DLQU]S<.1/08=`VUZWRSFQQH@+."&)50/-03,#)18>8GDAC2$7+,6CGGGVZ]FCPRTtKQEQDOMSbpsXQfbTeXc^VL<AJ]\]^chZKA3;9<UTP@QHLEAE<5/'"!'+35-0.2!%*3<=D=<8:)"$'?UQ>:ELSHE9EUNZ^VEIPO\Xqyj`_sd\fYo[TF>J_ce]_a\aUGDG;?MWMDOH7:BDB5-.$# .50/1/7(64A<G;,+5$'+GQF?4<XSVKC9C\ZKb`VTVTeXrfcjpde]ju[QMEJUhshe]YSSVTNF;H]THG@;<A;1565*!%"310029%3>.07@2)$+"%.MaTBDV[SG\MDNIkVSgVaYfbikumpvnpa_pledZXQT`eehc[\\VPOG?DTFBC?>8>1<4<4@,$'-15.27)9.;G4<,,)%#(9U\GLNTOONRl_`agc][h[_efkprs��vvnq||wrh_VNR\ecc]SX[XIC?AQOH=8-85/2@<33=0'234017'.8Y17-3$#),66FH?FBOVbcbiqzzrnnbfmkpy�}~����}|����ykj`\Xd`m[UU`YTNRLQSLI@=6,-68:0/.1'-2216.$29((<26+))*3M\QLN^dgny�|{����yx{�����������������}{xxmja`dQPY]_RSMUVOK>DA.%+16=/(0$4,5/:%-01463>88+35=W`UU`nt}����������������������������������~xtjdTRRabPSKONMDJ;8=+/9&4C1.#.041;'1/,G63:8<>:GOe[Zkr|����������������������������������������{vZW]_d]OJHTJC<=4<9@=1*<63")27/@*10259=;9>FLVhf]tw~�������������������������������������������qZgd_]THXJD>>15>8N@5*:*3,0.53/;-3(@OB7=MYeqjq}{���������������������������������������������ii_\QXNYBDI6201BR;:)4( +*/188JII-'+850GOS_rz�}������������������������������������ü���������kYT^_bNQUEHE3DEBAG(%'--/3EA@+Q9+1264:NWk}~�������������������������������������������������}_QXea]PQA8DE>?=7@&&"-102>25.N??3:6BUhr���������������������������������������÷�����������wfmlmn^WK=DD:D*8C+!',236.40*O:A@@4Mhw��������������������������������������������������������~{{xjYXGJ^3'-*H;/&.00643.(=4BGBIGf{�����������������������������������������������������������}yi\QMEN*&%1;1,210>51'**59EBJKg������������������������������������������������������������zi]LDFB6'(#-722/0<6(#&%9;MHNXl��������������������������������������������������������������uZVDDH<+0)!7/231D1/"#038GFYXk|�������������������������������������������������������������yaYCLO>362$,#,5-1<-)*6>8@RT[m��������������������������������������������������������������ylZHIHA2;5*"'-4,2;) &:2A9M[]bp��������������������������������������������������������������yafJH@:9/0'9(*,/0=*$',-CAF^Zcq��������������������������������������������������������������te[QCVA;.*$A1$3-.12/)$/3BPQTcp��������������������������������������������������������������{c^GHMVO5''(/-,0/32@#)-FBJR]fo�������������������������������������������������������������~gZD>IPF08&%!./2/.22#85JLJSX_m}��������������������������������������������������������������h\INBE3-01,/-/&3&*=6CCUX`bs��������������������������������������������������������������ymaSMM<*,*)$"/1-/*8!+22;DV^cl����������������������������������������������������������������mXRJB5)*&(&#*20076053??RXgj}��������������������������������������������������������������zl_QVJ7).$)&#220.<;0+);ICY^jn����������������������������������������������������������������j_VLBH3&**"%2//2C7*)%38J_hmv����������������������������������������������������������������v^ZN555%8(*'22/-E/&%-*BLXkn����������������������������������������������������������������}_^UM;1'*'*#620.=2"+F)5@Pjt������������������������������¿��������������������������������w`XFj9C)&&$.01-9?>R%2FXkt������������������������������žÿ�������������������������������eQRM663($&"/1.,.?-J 9MJeq������������������������������ÿſ��������������������������������gF_@1,+-,'.-1../7&!=*6TJ`�������������������������������������������������������������������mNW92%.'2-,5-2124))43LD\�������������������������������������������������������������������nTZ73),-/0-4+0/57%2E4@8\�������������������������������������������������������������������laG/44%5-/11*1117.CNA6@S�������������������������������������������������������������������xO>-%2,45/1714034+!0_C)GK���������������������������������������������xxzy�y����������������\0&&2D,06235.1525+_='LL����������������~}���������������������������ulkpwqw}��������������d0$#>C:2@412044/7!*X9-LP��������������~yuty|���������������������������~u|������������������bH#-?676.M-11362:&%D@,IT��������������������������������������������������������������������mV415828.D8,/1641<#H=TS���������������������������������������������������������������������P<A7617VVB)/16QdK%K.!RU�������������������}}������������������������~qallAVCIOWv������������]5JA1C}���B-+q��Q@-+Qd��������ļ����y\cYMJ`^l~��������������������tsqz��V{�)I-vgT~���������k5E<H{�����,-�����LAGJl�����������jwYY?�UbG�xdhr�������������������ko����UQ_3sC��cNq��������xH@Oq������_#������YkLy���������ke��~JQ6Z[M��w]vy�����������������z�t����sbfG����|id|��������KKc�������� �������M���������}S�����aN\Zk���y�a�����������������txsx�����l[�����|wu��������X\}��������P��������T�����������z����gcam��}�y}p����������������}����������}|�����|w��������|{����������y��������������������h����|������������������������~uy�����������������������������y�������p���������������������������������������������������s}�����������������������������w�����z�r������ƕ������������������������������������°������~x�����������������������������������|��������Ė�������������õ�������������������������������~��������������������������x�������|�����þ����������������������������������������������������������������������������PZ}�����}������yq^�������������������������������������������������������������������������~Uzpv����������{t�i��������������������������������������������������������������������������R��r}��������vu����������������������������������������������������������������������������p��{{��������yy������������������������������������������������������������������������������p�~���������y}�}����������������������������������������������������������������������������l�z����������{�u���������������������������������������������������������������������������~n�w�����b������m������������������������������������������������������������������������������������<���ū��o�������������������������������������������������������������������������~��������w(�������}����������������������������������������������������������������������������������9-z����������������������������������������������������������������������������������������^$.���������������������������������������������������������������������������������������{%/0'?�������������������������������������������ͷ����������������������������������������r9(..,#>�����������������������������������������������������������������������������������R:(,.,13$&r��������������������������������ü����Ǵ���������������������������������������{>)0.+,,5,/-'m���Ď����������������������������w����������z^������������������������������vT<-,--++//10++'?z�������������������������������������������y���������������������������H;9/-))/+-*+/0(.+0(+=H_��������������������������������������������������������������������s(,*-)+*-++--*+-+++-(-, ��������������������������������������������������������������������U +,*,-+,--*.--)-*.,+.+���������ÿÿ�������������������������������������������������������<$(+,,,(*,+,/0*,,+,-+/(L������������½����������������������������������������������������z&'-*&*/'.*,*,.,++,(,+(/-&������������������������������������������������������������������X#'+*-'-'.&*.+1++.-+)+)-.������������������������������������������������������������������:$+**).%(,)++&./-+,&+++*0j����������������������������������������������������������������})",+(-+%))/'.)/.-,)+'.,*,#H����������������������������������������������������������������` &)'()+*%*-,,)2,++,*,.+*0,�����������¾���������������������������������������������������B#($*%%+(((%+0(/,0/+(,+--).z��������������������������������������������������������������}, &#,''(($()),)11/,0(--(*,. D��������������������������������������������������������������h'(*%$'*#**'(,*/1/,+.*0&,*--��������������������������������������������������������������]$&&,$+(*$')&,(120./,,+(--+-a�����������ÿ�������{ysk_bqponu~wxrrsqw}���������������������X (%*$-%%,$'&',*1.20)/+-,++-/+�����������ĸ���tnuupihg`geb^UU]VTUQX^\[glr{�����������������T(''&*((''&')*,-251.0,).-)+.(e����������ó����������zx|vtrjpmmcg`cjddlrw�����������������U!&'%((($+')&'++.3/3(3),*/()0,'����������ĵ���������������y{��{xvu|w{}��������������������V!%%((($'&*$)()(212./0+0-,-+.))[�����������������������������������������������������������\!()&.('(-'((',15/0300//+*,)-(,�����������������������������������������������������������\$(''0%.+)-#-(,24//52-/0)-.)*-*������������Ƽ���������������������������������������������`#)"*%(+)+)),*-23.1-6-0//-,0+*.�������������¼��������������������������������������������c '&&)%'*)%)'2&/00/8+8.20021+-&/����������������þ�����������������������������������������k&$)')&((*)),).212225012,022-./q��������ü������������������������������������������}�����h###$&'$*+&-',1*042040342-13//1-r��������Ż������������������������������������������������j"'"&&'&.',)-3+14411514..5/13.,m���������������������������������������������������{������i#&%'((-'(.(.1(040433/2./2/+01.i��������������������������������������������������z�������e&('+'(*&+(*..01414.6-1131//3/3d��������������������������������������������������~�������i&$)(-(-&',+//-02127,52304/05.5h����������������������������������������������������������e%%%&+))*,)(0.-150332700630904*l����������������������������������������������������������h%!*%('&.**-+0/34105/4.0024505 r����������������������������������������������������������d$&'$%.#+'.+0+,014/21322005/7({��������������������������������������������{�������������c&%$&&()'++/0-/-1/2215-1/4010 ���������������������������������������������{�������������^'&%#'&+)..0/)4.62/30341062.,�������������������������������������������{���������������b'"('&&**/-.,01/1/4203/133'&������������������������������������������z����������������d!)$%%*&--*2)01-4+40134./3+&%�����������������������������������������}�����������������c*&$#'++/*0-0-202.2/320.4#'���������ó������������������������������������������������Y#)#()')..,-..-61.22/.2/-"'`��������ü������������������������������������������������J.%%)'*+2-.//333-42/0.4"#'/'���������¾�����������������������������������������������9*"',()1*,/..
Expand Down
Loading

0 comments on commit b48c3f2

Please sign in to comment.