Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
Homework2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
20200318093
Homework2
Commits
ab430625
Commit
ab430625
authored
Apr 30, 2020
by
20200318093
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update kernel func
parent
aee8dc20
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
6 deletions
+11
-6
face.rec.klda.homework.py
+11
-6
No files found.
face.rec.klda.homework.py
View file @
ab430625
...
...
@@ -66,13 +66,15 @@ class KernelDiscriminantAnalysis(BaseEstimator, ClassifierMixin,
self
.
N
=
0
# 用于存放 Kernel LDA 最优化公式中的 N
self
.
EigenVectors
=
None
# 用于存放 Kernel LDA 最优化公式中的 M 对应的广义特征向量, 每一列为一个特征向量, 按照对应特征值大小排序
def
Kernal
(
self
,
x
,
y
,
gamma
):
### Defining a kernel, can replace it with linear or others
def
Kernel
(
self
,
x
,
y
,
gamma
):
return
math
.
exp
(
-
(
x
-
y
)
.
T
.
dot
(
x
-
y
)
*
gamma
)
### Computting kernel matrix,
### input: X1(n_samples_1,n_feature) * X2(n_samples_2,n_feature)
### Output: (n_samples_1,n_samples_2)
def
Kernal_matrix
(
self
,
x1
,
x2
,
gamma
):
#print(x1.shape,x2.shape,gamma)
#print(self.Kernal([1,1],[2,2],gamma=gamma))
return
np
.
fromfunction
(
np
.
vectorize
(
lambda
i
,
j
:
self
.
Kernal
(
x1
[
int
(
i
),:],
x2
[
int
(
j
),:],
gamma
=
gamma
)),
(
x1
.
shape
[
0
],
x2
.
shape
[
0
]),
dtype
=
np
.
float32
)
return
np
.
fromfunction
(
np
.
vectorize
(
lambda
i
,
j
:
self
.
Kernel
(
x1
[
int
(
i
),:],
x2
[
int
(
j
),:],
gamma
=
gamma
)),
(
x1
.
shape
[
0
],
x2
.
shape
[
0
]),
dtype
=
np
.
float32
)
def
fit
(
self
,
X
,
y
):
"""Fit KDA model.
...
...
@@ -105,14 +107,17 @@ class KernelDiscriminantAnalysis(BaseEstimator, ClassifierMixin,
l
.
append
(
X1
.
shape
[
0
])
### Computing M, based on M = (M0-M1) @ (M1-M1).T, M is symmetric
### Alternatively, we can use sklearn.metrics.pairwise.rbf_kernel
### Reason of using this function is that we can replace the RBF kernel by other kernels easily
M0
=
np
.
mean
(
self
.
Kernal_matrix
(
X
,
X0
,
gamma
=
self
.
gamma
),
axis
=-
1
)
.
reshape
(
-
1
,
1
)
M1
=
np
.
mean
(
self
.
Kernal_matrix
(
X
,
X1
,
gamma
=
self
.
gamma
),
axis
=-
1
)
.
reshape
(
-
1
,
1
)
self
.
M
=
(
M0
-
M1
)
@
(
M0
-
M1
)
.
T
### Computing kernal matrix K0 and K1
### Alternatively, we can use sklearn.metrics.pairwise.rbf_kernel
self
.
K
=
[]
self
.
K
.
append
(
rbf_kernel
(
X
,
X0
,
gamma
=
self
.
gamma
))
self
.
K
.
append
(
rbf_kernel
(
X
,
X1
,
gamma
=
self
.
gamma
))
self
.
K
.
append
(
self
.
Kernal_matrix
(
X
,
X0
,
gamma
=
self
.
gamma
))
self
.
K
.
append
(
self
.
Kernal_matrix
(
X
,
X0
,
gamma
=
self
.
gamma
))
### Computing N, N is symmetric
for
i
in
range
(
1
):
self
.
N
+=
self
.
K
[
i
]
@
(
np
.
eye
(
l
[
i
])
-
np
.
full
((
l
[
i
],
l
[
i
]),
1.
/
l
[
i
]))
@
self
.
K
[
i
]
.
T
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment