forked from SleepyBag/Statistical-Learning-Methods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.py
47 lines (41 loc) · 1.25 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
import numpy as np
import sys
import os
from pathlib import Path
sys.path.append(str(Path(os.path.abspath(__file__)).parent.parent))
from utils import *
sys.path.append(str(Path(os.path.abspath(__file__)).parent.parent / '15.SVD'))
from SVD import svd
def pca(X, k=5):
"""
given a normlized matrix X, each of whose column is a sample
the dimension of the principle component, k
return the principle component matrix
"""
m, n = X.shape
X_trans = 1 / sqrt(n - 1) * X.T
_, _, V = svd(X_trans)
V = V[:, :k]
return V.T @ X
if __name__ == '__main__':
def demonstrate(X, k, desc):
print(desc)
X -= X.mean(axis=-1, keepdims=True)
X_trans = pca(X, k=k)
print(X_trans)
X = np.array([[1, 1],
[2, 2],
[0, 0]]).astype(float)
demonstrate(X, 1, 'Example 1')
X = np.array([[1, 0, 0, 0],
[0, 0, 0, 4],
[0, 3, 0, 0],
[0, 0, 0, 0],
[2, 0, 0, 0]]).astype(float)
demonstrate(X, 1, 'Example 2')
X = np.array([[3, 1],
[2, 1]]).astype(float)
demonstrate(X, 1, 'Example 3')
X = np.array([[0, 0],
[-1, 1]]).astype(float)
demonstrate(X, 1, 'Example 3')