-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj1_3_PCA.py
77 lines (58 loc) · 1.88 KB
/
proj1_3_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 25 21:51:44 2021
@author: changai
"""
from proj1_1_load_data import *
import matplotlib.pyplot as plt
from scipy.linalg import svd
Y1 = X1 - np.ones((N,1)) * X1.mean(axis=0)
#print(X1.mean(axis=0)) #obtain mean value along column, will get 10 numbers, array(1, M)
#print(Y1)
#normalizing matrix
Y1 = Y1*(1/np.std(Y1.astype(float),axis=0))
# Y1 = Y1*(1/np.std(Y1.astype(float),axis=0))
# PCA by computing SVD of Y
U1,S1,V1 = svd(Y1.astype(np.float),full_matrices=False)
# Compute variance explained by principal
#print(S)
rho1 = (S1*S1) / (S1*S1).sum()
#print(rho)
threshold = 0.9
# Plot variance explained
plt.figure()
plt.plot(range(1,len(rho1)+1),rho1,'x-')
plt.plot(range(1,len(rho1)+1),np.cumsum(rho1),'o-')
plt.plot([1,len(rho1)],[threshold, threshold],'k--')
plt.title('Variance explained by principal components for Bejaia');
plt.xlabel('Principal component');
plt.ylabel('Variance explained');
plt.legend(['Individual','Cumulative','Threshold'])
plt.grid()
plt.show()
print('run Bjaia')
Y2 = X2 - np.ones((N,1)) * X2.mean(axis=0)
#print(X1.mean(axis=0)) #obtain mean value along column, will get 10 numbers, array(1, M)
#print(Y2)
#normalizing matrix
Y2 = Y2*(1/np.std(Y2.astype(float),axis=0))
# Y2 = Y2*(1/np.std(Y2.astype(float),axis=0))
# PCA by computing SVD of Y
U2,S2,V2 = svd(Y2.astype(np.float),full_matrices=False)
# Compute variance explained by principal
#print(S)
rho2 = (S2*S2) / (S2*S2).sum()
#print(rho)
threshold = 0.9
# Plot variance explained
plt.figure()
plt.plot(range(1,len(rho2)+1),rho2,'x-')
plt.plot(range(1,len(rho2)+1),np.cumsum(rho2),'o-')
plt.plot([1,len(rho2)],[threshold, threshold],'k--')
plt.title('Variance explained by principal components for Sidi');
plt.xlabel('Principal component');
plt.ylabel('Variance explained');
plt.legend(['Individual','Cumulative','Threshold'])
plt.grid()
plt.show()
print('run Sidi')