-
Notifications
You must be signed in to change notification settings - Fork 0
/
pca.py
50 lines (40 loc) · 1.95 KB
/
pca.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
43
44
45
46
47
48
49
50
from typing import Tuple
import numpy as np
def hello_world():
print("Hello world")
def train_PCA(data: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Run PCA on the data.
Input:
data: A numpy array of shape [N, d], where N is the number of data
points and d is the dimension of each data point.
We assume the data has full rank.
Returns: A tuple of (U, eigenvalues)
U: The U matrix, whose column vectors are principal components
(i.e., eigenvectors) in the order of decreasing variance.
eigenvalues:
An array (or list) of all eigenvalues sorted in a decreasing order.
"""
if len(data.shape) != 2:
raise ValueError("Invalid shape of data; did you forget flattening?")
N, d = data.shape
#######################################################
### START OF YOUR CODE ###
#######################################################
centered_data = data - np.mean(data, axis=0, keepdims=True)
assert np.allclose(centered_data.mean(axis=0), np.zeros(d))
# Important: bias=False is default in np.cov, which divides by "N - 1".
# The correct implementation is to divide by "N", where bias=True should
# be used.
# In the future (2024), guide students to not use np.cov but use the
# definition directly. Or explicitly give a hint that bias=True is needed.
# To future GSIs: please make the 1 / (N-1) one incorrect (e.g., -0.5 pts)
# by improving the test cases, specifically detecting this failure mode.
cov = np.cov(centered_data.T, bias=True)
eigen_vals, eigen_vecs = np.linalg.eig(cov)
indices = np.argsort(-eigen_vals)
U = eigen_vecs[:, indices]
eigenvalues = eigen_vals[indices]
#######################################################
### END OF YOUR CODE ###
#######################################################
return U, eigenvalues