-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathToolPca.py
More file actions
24 lines (17 loc) · 620 Bytes
/
ToolPca.py
File metadata and controls
24 lines (17 loc) · 620 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding: utf-8 -*-
import numpy as np
## helper function: principal component analysis (pca)
#
# @param V: features for all train observations (dimension iNumFeatures x iNumObservations)
#
# @return U_pc: transformed features 'score' (dimension see V)
# @return T: transformation matrix 'loading' (dimension iNumFeatures x iNumFeatures )
# @return eigenvalues: 'latent' (length iNumFeatures)
def ToolPca(V):
# covariance
cov_VV = np.cov(V)
# svd
[U, eigenvalues, T] = np.linalg.svd(cov_VV)
# features to components
U_pc = np.matmul(T.T, V)
return U_pc, T, eigenvalues