-
Notifications
You must be signed in to change notification settings - Fork 0
/
pca.py
31 lines (28 loc) · 931 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
def sort_key(x):
return x[0]
def pca(data):
data = data.astype(np.float)
components = 2
rows, cols = data.shape
#substract the mean
normal_data = data
for i in range(cols):
mean = np.mean(data[:,i])
for j in range(rows):
normal_data[j][i] -= mean
#calculate the covariance matrix
cov_mat = np.cov(np.transpose(normal_data))
#calculate the eigenvectors and eigenvalues
eigenval, eigenvec = np.linalg.eig(cov_mat)
#sorting eigenvectors by correlating eigenvalue
tmp_pairs = [(np.abs(eigenval[i]), eigenvec[:,i]) for i in range(cols)]
tmp_pairs.sort(key=sort_key, reverse=True)
#forming a feature vector
feature = np.array([ele[1] for ele in tmp_pairs[:components]])
feature = np.transpose(feature)
#new dataset
result = normal_data @ feature
return result