-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwpca.py
248 lines (197 loc) · 8.51 KB
/
wpca.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import numpy as np
from scipy import linalg
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import check_array
from utils_wpca import check_array_with_weights, weighted_mean
class WPCA(BaseEstimator, TransformerMixin):
"""Weighted Principal Component Analysis
Parameters
----------
n_components : int (optional)
Number of components to keep. If not specified, all components are kept
xi : float (optional)
Degree of weight enhancement.
regularization : float (optional)
Control the strength of ridge regularization used to compute the
transform.
copy_data : boolean, optional, default True
If True, X and weights will be copied; else, they may be overwritten.
Attributes
----------
components_ : array, [n_components, n_features]
Principal axes in feature space, representing the directions of
maximum variance in the data.
explained_variance_ : array, [n_components]
The amount of variance explained by each of the selected components.
explained_variance_ratio_ : array, [n_components]
Percentage of variance explained by each of the selected components.
mean_ : array, [n_features]
Per-feature empirical mean, estimated from the training set.
See Also
--------
- PCA
- sklearn.decomposition.PCA
"""
def __init__(self, n_components=None, xi=0, regularization=None,
copy_data=True, mean_centering=True):
self.n_components = n_components
self.xi = xi
self.regularization = regularization
self.copy_data = copy_data
self.mean_centering = mean_centering
def _center_and_weight(self, X, weights, fit_mean=False):
"""Compute centered and weighted version of X.
If fit_mean is True, then also save the mean to self.mean_
"""
X, weights = check_array_with_weights(X, weights, dtype=float,
copy=self.copy_data)
if fit_mean:
self.mean_ = weighted_mean(X, weights, axis=0)
# now let X <- (X - mean) * weights
if self.mean_centering:
X -= self.mean_
if weights is not None:
X *= weights
else:
weights = np.ones_like(X)
return X.astype(np.float64), weights.astype(np.float64)
def fit(self, X, y=None, weights=None):
"""Compute principal components for X
Parameters
----------
X: array-like, shape (n_samples, n_features)
Training data, where n_samples in the number of samples
and n_features is the number of features.
weights: array-like, shape (n_samples, n_features)
Non-negative weights encoding the reliability of each measurement.
Equivalent to the inverse of the Gaussian errorbar.
Returns
-------
self : object
Returns the instance itself.
"""
# let X <- (X - mean) * weights
X, weights = self._center_and_weight(X, weights, fit_mean=True)
self._fit_precentered(X, weights)
return self
def _fit_precentered(self, X, weights):
"""fit pre-centered data"""
if self.n_components is None:
n_components = X.shape[1]
else:
n_components = self.n_components
# TODO: filter NaN warnings
covar = np.dot(X.T, X)
wscale = np.dot(weights.T, weights)
covar /= np.dot(weights.T, weights)
covar[np.isnan(covar)] = 0
# enhance weights if desired
if self.xi != 0:
Ws = weights.sum(0)
covar *= np.outer(Ws, Ws) ** self.xi
eigvals = (X.shape[1] - n_components, X.shape[1] - 1)
evals, evecs = linalg.eigh(covar, eigvals=eigvals)
'''
evals, evecs = np.linalg.eigh(covar)
evals = evals[(X.shape[1] - n_components):X.shape[1]]; evecs = evecs[(X.shape[1] - n_components):X.shape[1]]
'''
self.components_ = evecs[:, ::-1].T
self.explained_variance_ = evals[::-1]
if covar.trace() == 0:
self.explained_variance_ratio_ = evals[::-1]
raise ZeroDivisionError('It is likely that the covar matrix is all 0s thus the trace is {} because after mean-centering everything is 0'.format(covar.trace))
else:
self.explained_variance_ratio_ = evals[::-1] / covar.trace()
def transform(self, X, weights=None):
"""Apply dimensionality reduction on X.
X is projected on the first principal components previous extracted
from a training set.
Parameters
----------
X : array-like, shape (n_samples, n_features)
New data, where n_samples in the number of samples
and n_features is the number of features.
weights: array-like, shape (n_samples, n_features)
Non-negative weights encoding the reliability of each measurement.
Equivalent to the inverse of the Gaussian errorbar.
Returns
-------
X_new : array-like, shape (n_samples, n_components)
"""
X, weights = self._center_and_weight(X, weights, fit_mean=False)
return self._transform_precentered(X, weights)
def _transform_precentered(self, X, weights):
"""transform pre-centered data"""
# TODO: parallelize this?
Y = np.zeros((X.shape[0], self.components_.shape[0]))
for i in range(X.shape[0]):
cW = self.components_ * weights[i]
cWX = np.dot(cW, X[i])
cWc = np.dot(cW, cW.T)
if self.regularization is not None:
cWc += np.diag(self.regularization / self.explained_variance_)
Y[i] = np.linalg.solve(cWc, cWX)
return Y
def fit_transform(self, X, y=None, weights=None):
"""Fit the model with X and apply the dimensionality reduction on X.
Parameters
----------
X : array-like, shape (n_samples, n_features)
New data, where n_samples in the number of samples
and n_features is the number of features.
weights: array-like, shape (n_samples, n_features)
Non-negative weights encoding the reliability of each measurement.
Equivalent to the inverse of the Gaussian errorbar.
Returns
-------
X_new : array-like, shape (n_samples, n_components)
"""
X, weights = self._center_and_weight(X, weights, fit_mean=True)
self._fit_precentered(X, weights)
return self._transform_precentered(X, weights)
def inverse_transform(self, X):
"""Transform data back to its original space.
Returns an array X_original whose transform would be X.
Parameters
----------
X : array-like, shape (n_samples, n_components)
Data in transformed representation.
Returns
-------
X_original : array-like, shape (n_samples, n_features)
"""
X = check_array(X)
return self.mean_ + np.dot(X, self.components_)
def reconstruct(self, X, weights=None):
"""Reconstruct the data using the PCA model
This is equivalent to calling transform followed by inverse_transform.
Parameters
----------
X : array-like, shape (n_samples, n_components)
Data in transformed representation.
weights: array-like, shape (n_samples, n_features)
Non-negative weights encoding the reliability of each measurement.
Equivalent to the inverse of the Gaussian errorbar.
Returns
-------
X_reconstructed : ndarray, shape (n_samples, n_components)
Reconstructed version of X
"""
return self.inverse_transform(self.transform(X, weights=weights))
def fit_reconstruct(self, X, weights=None):
"""Fit the model and reconstruct the data using the PCA model
This is equivalent to calling fit_transform()
followed by inverse_transform().
Parameters
----------
X : array-like, shape (n_samples, n_components)
Data in transformed representation.
weights: array-like, shape (n_samples, n_features)
Non-negative weights encoding the reliability of each measurement.
Equivalent to the inverse of the Gaussian errorbar.
Returns
-------
X_reconstructed : ndarray, shape (n_samples, n_components)
Reconstructed version of X
"""
return self.inverse_transform(self.fit_transform(X, weights=weights))