forked from hal3/ciml
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdr.py
44 lines (32 loc) · 1.24 KB
/
dr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from numpy import *
from util import *
from pylab import *
def pca(X, K):
'''
X is an N*D matrix of data (N points in D dimensions)
K is the desired maximum target dimensionality (K <= min{N,D})
should return a tuple (P, Z, evals)
where P is the projected data (N*K) where
the first dimension is the higest variance,
the second dimension is the second higest variance, etc.
Z is the projection matrix (D*K) that projects the data into
the low dimensional space (i.e., P = X * Z).
and evals, a K dimensional array of eigenvalues (sorted)
'''
N,D = X.shape
# make sure we don't look for too many eigs!
if K > N:
K = N
if K > D:
K = D
# first, we need to center the data
### TODO: YOUR CODE HERE
util.raiseNotDefined()
# next, compute eigenvalues of the data variance
# hint 1: look at 'help(pylab.eig)'
# hint 2: you'll want to get rid of the imaginary portion of the eigenvalues; use: real(evals), real(evecs)
# hint 3: be sure to sort the eigen(vectors,values) by the eigenvalues: see 'argsort', and be sure to sort in the right direction!
#
### TODO: YOUR CODE HERE
util.raiseNotDefined()
return (P, Z, evals)