-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca.py
39 lines (28 loc) · 950 Bytes
/
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
import numpy as np
class PCA:
"""
Principal Component Analysis (PCA) for dimensionality reduction.
"""
def __init__(self, n_components=None):
self.n_components = n_components
# Training parameters
self.evals = None
self.evecs = None
def fit(self, X):
if self.n_components is None:
self.n_components = min(X.shape)
# Center data
X = X - np.mean(X, axis=0)
# Covariance matrix
self.cov = np.cov(X.T)
# Compute eigenvalues and eigenvectors
evals, evecs = np.linalg.eig(self.cov)
# Select top n_components eigenvectors
idx = evals.argsort()[::-1]
self.evals = evals[idx][:self.n_components]
self.evecs = evecs[:, idx][:, :self.n_components]
def transform(self, X):
return np.dot(X, self.evecs)
def fit_transform(self, X):
self.fit(X)
return self.transform(X)