-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA.py
More file actions
120 lines (103 loc) · 3.94 KB
/
Copy pathPCA.py
File metadata and controls
120 lines (103 loc) · 3.94 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from ExtractData import *
import scipy.linalg as linalg
import matplotlib.pyplot as plt
# convertion of array of objects to array of floats
Xf = X.astype(float)
N,M = Xf.shape
# =============================================================================
# =============================================================================
# !CAUTION!
# plots each pair of the attributes
# better leave commented
# =============================================================================
# =============================================================================
# for i in range(0,M):
# for j in range(0,M):
# plt.plot(Xf[:, i], Xf[:, j], 'o', alpha=0.1)
# plt.xlabel(attributeNames[i])
# plt.ylabel(attributeNames[j])
# plt.show()
# =============================================================================
# =============================================================================
# variance explained by PCA
# =============================================================================
# =============================================================================
# normalization
Xf = Xf - np.ones((N,1))*Xf.mean(axis=0)
Xf = Xf*(1/np.std(Xf,0))
U,S,V = linalg.svd(Xf,full_matrices=False)
rho = (S*S) / (S*S).sum()
threshold = 0.9
plt.figure()
plt.plot(range(1,len(rho)+1),rho,'o-')
plt.title('Variance explained by principal components');
plt.xlabel('Principal component');
plt.ylabel('Variance explained');
plt.legend(['Individual'])
plt.grid()
plt.show()
plt.figure()
plt.plot(range(1,len(rho)+1),np.cumsum(rho),'o-')
plt.plot([1,len(rho)],[threshold, threshold],'k--')
plt.title('Variance explained by principal components');
plt.xlabel('Principal component');
plt.ylabel('Variance explained');
plt.legend(['Cumulative','Threshold'])
plt.grid()
plt.show()
# =============================================================================
# =============================================================================
# the amount of variation explained as the number of PCA components included
# =============================================================================
# =============================================================================
pcs = [[0,1,2],[3,4,5]]
for pcs_triple in pcs:
legendStrs = ['PC'+str(e+1) for e in pcs_triple]
c = ['r','g','b']
bw = .2
r = np.arange(1,M+1)
for i in pcs_triple:
plt.bar(r+i*bw, V[:,i], width=bw)
plt.xticks(r+bw, r)
# with attributes name (its unreadable):
# plt.xticks(r+bw, attributeNames[:])
plt.xlabel('Attribute index')
plt.ylabel('Component coefficients')
plt.legend(legendStrs)
plt.grid()
plt.title('PCA Component Coefficients')
plt.show()
# =============================================================================
# =============================================================================
# plot of PCA's against each other
# =============================================================================
# =============================================================================
V = V.T
Z = Xf @ V
# choice of principal components
i = 1
j = 2
f = plt.figure()
plt.title('Data projected into PCs (6-classification)')
for c in range(0,C):
class_mask = y_classification[:,c]==1
plt.plot(Z[class_mask,i], Z[class_mask,j], 'o', alpha=.5)
plt.xlabel('PC{0}'.format(i+1))
plt.ylabel('PC{0}'.format(j+1))
plt.legend(classNames)
plt.show()
f = plt.figure()
plt.title('Data projected into PCs (3-classification)')
for c in range(0,C-1,2):
print(c)
class1_mask = y_classification[:,c]==1
class2_mask = y_classification[:,c+1]==1
class_mask = class1_mask | class2_mask
if c == 4:
class3_mask = y_classification[:,c+2]==1
class_mask = class_mask | class3_mask
plt.plot(Z[class_mask,i], Z[class_mask,j], 'o', alpha=.5)
plt.xlabel('PC{0}'.format(i+1))
plt.ylabel('PC{0}'.format(j+1))
plt.legend(['Insufficient/Normal_Weight', 'Obesities', "Overweights"])
plt.show()