-
Notifications
You must be signed in to change notification settings - Fork 21
/
pca.py
206 lines (168 loc) · 5.95 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
import metrics
class PCA:
def __init__(self, n_components, whiten=False, method='', visualize=False):
'''
Parameters
----------
n_components : Number of components to keep
whiten : Whitening
method : SVD or not
visualize : Plot scatter if n_components equals 2
'''
self.__n_components = n_components
self.__whiten = whiten
self.__method = method
self.__visualize = visualize
def fit_transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
Returns
-------
X : shape (n_samples, n_components)
The data of dimensionality reduction
'''
n_samples = X.shape[0]
self.__mean = np.mean(X, axis=0)
X_sub_mean = X - self.__mean
if self.__method == 'svd':
u, s, vh = np.linalg.svd(X_sub_mean)
self.__eig_values = (s ** 2)[:self.__n_components]
self.__eig_vectors = vh.T[:, :self.__n_components]
else:
conv = X_sub_mean.T.dot(X_sub_mean)
eig_values, eig_vectors = np.linalg.eigh(conv)
self.__eig_values = eig_values[::-1][:self.__n_components]
self.__eig_vectors = eig_vectors[:, ::-1][:, :self.__n_components]
if self.__whiten:
self.__std = np.sqrt(self.__eig_values.reshape((1, -1)) / (n_samples - 1))
return self.transform(X)
def transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
X : shape (n_samples, n_components)
The data of dimensionality reduction
'''
X_sub_mean = X - self.__mean
pc = X_sub_mean.dot(self.__eig_vectors)
if self.__whiten:
pc /= self.__std
if self.__n_components == 2 and self.__visualize:
metrics.scatter_feature(pc)
return pc
class KernelPCA:
def __init__(self, n_components, kernel_func, sigma=1, visualize=False):
'''
Parameters
----------
n_components : Number of components to keep
kernel_func : kernel algorithm see also kernel.py
sigma : Parameter for rbf kernel
visualize : Plot scatter if n_components equals 2
'''
self.__n_components = n_components
self.__kernel_func = kernel_func
self.__sigma = sigma
self.__visualize = visualize
def fit_transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
Returns
-------
X : shape (n_samples, n_components)
The data of dimensionality reduction
'''
self.__X = X
n_samples = self.__X.shape[0]
K = self.__kernel_func(self.__X, self.__X, self.__sigma)
self.__K_row_mean = np.mean(K, axis=0)
self.__K_mean = np.mean(self.__K_row_mean)
I = np.full((n_samples, n_samples), 1 / n_samples)
K_hat = K - I.dot(K) - K.dot(I) + I.dot(K).dot(I)
eig_values, eig_vectors = np.linalg.eigh(K_hat)
self.__eig_values = eig_values[::-1][:self.__n_components]
self.__eig_vectors = eig_vectors[:, ::-1][:, :self.__n_components]
#pc = self.__eig_vectors * np.sqrt(self.__eig_values)
return self.transform(X)
def __kernel_centeralization(self, kernel):
kernel -= self.__K_row_mean
K_pred_cols = (np.sum(kernel, axis=1) / self.__K_row_mean.shape[0]).reshape((-1, 1))
kernel -= K_pred_cols
kernel += self.__K_mean
return kernel
def transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
X : shape (n_samples, n_components)
The data of dimensionality reduction
'''
kernel = self.__kernel_func(self.__X, X, self.__sigma)
kernel = self.__kernel_centeralization(kernel)
pc = kernel.dot(self.__eig_vectors / np.sqrt(self.__eig_values))
if self.__n_components == 2 and self.__visualize:
metrics.scatter_feature(pc)
return pc
class ZCAWhiten:
def __init__(self, method=''):
'''
Parameters
----------
method : SVD or not
'''
self.__method = method
def fit_transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
Returns
-------
X : shape (n_samples, n_components)
The data whitened
'''
n_samples = X.shape[0]
self.__mean = np.mean(X, axis=0)
X_sub_mean = X - self.__mean
if self.__method == 'svd':
u, s, vh = np.linalg.svd(X_sub_mean)
self.__eig_values = s ** 2
self.__eig_vectors = vh.T
else:
conv = X_sub_mean.T.dot(X_sub_mean)
eig_values, eig_vectors = np.linalg.eigh(conv)
self.__eig_values = eig_values[::-1]
self.__eig_vectors = eig_vectors[:, ::-1]
self.__std = np.sqrt(self.__eig_values.reshape((1, -1)) / (n_samples - 1))
return self.transform(X)
def transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
X : shape (n_samples, n_components)
The data whitened
'''
X_sub_mean = X - self.__mean
pc = X_sub_mean.dot(self.__eig_vectors)
pc /= self.__std
return pc.dot(self.__eig_vectors.T)