forked from scikit-learn/scikit-learn
/
lpp.py
383 lines (308 loc) · 13.5 KB
/
lpp.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'''
Locality Preserving Projection (LPP)
References
----------
[1] "X. He and P. Niyogi. Locality preserving projections. Advances in Neural
Information Processing Systems 16 (NIPS 2003), 2003. Vancouver, Canada."
Created on 2012/11/27
@author: du
'''
import numpy as np
from scipy.sparse import csr_matrix, isspmatrix
from ..base import BaseEstimator, TransformerMixin
from ..neighbors import kneighbors_graph
from ..utils.extmath import safe_sparse_dot
from ..utils import check_random_state, array2d, check_arrays
from scipy.linalg import eigh
from ..utils.arpack import eigsh
from ..decomposition import PCA
def adjacency_matrix(X, n_neighbors, mode='distance'):
"""Compute weighted adjacency matrix by k-nearest search
Parameters
----------
X : array-like, shape: (n_samples, n_features)
Input data, each column is an edge of a graph
n_neighbors : int
number of neighbors to define whether two edges are connected or not
mode : string
mode of kneighbors_graph calculation
use_ext : bool
whether use cython extension or not
Returns
-------
adjacency matrix : LIL sparse matrix, shape: (n_samples, n_samples)
(i, j) component of it is defined in the section 2.2 of [1]
"""
X = np.asanyarray(X)
G = kneighbors_graph(X, n_neighbors, mode)
G = G.tolil()
nzx, nzy = G.nonzero()
try:
from .lpp_util import to_symmetrix_matrix
G = to_symmetrix_matrix(G, nzx, nzy)
except ImportError:
for xx, yy in zip(nzx, nzy):
if G[yy, xx] == 0:
G[yy, xx] = G[xx, yy]
return G
def affinity_matrix(adj_mat, kernel_func="heat", kernel_param="auto"):
"""Compute the affinity matrix from adjacency matrix
Parameters
----------
adj_mat : array-like, shape(n_samples, n_samples)
the adjacency matrix
kernel_func : string or callable
kernel function to compute affinity matrix
kernel_param : float
parameter for heat kernel
Returns
-------
affinity matrix : CSR sparse matrix, shape(n_samples, n_samples)
(i, j) component W(i,j) is defined in [1] section 2.2 2 (a)
"""
W = csr_matrix(adj_mat)
if kernel_func == "heat":
W.data **= 2
# Estimate kernel_param by the heuristics that it must correspond to
# the variance of Gaussian distribution.
if kernel_param == "auto":
kernel_param = np.median(W.data)
np.exp(-W.data / kernel_param, W.data)
else:
W.data = kernel_func(W.data)
return W
def laplacian_matrix(afn_mat):
"""Compute the affinity matrix from adjacency matrix
Parameters
----------
afn_mat : CSR sparse matrix, shape(n_samples, n_samples)
the affinity matrix
Returns
-------
Laplacian matrix : CSR sparse matrix, shape(n_samples, n_samples)
(i, j) component L(i,j) is defined in [1] section 2.2 3
col_sum : ndarray, shape(n_samples)
diagonal matrix whose entries are column sums of the affinity matrix
"""
col_sum = np.asarray(afn_mat.sum(0)).flatten()
lap_mat = (-afn_mat).tolil()
lap_mat.setdiag(col_sum)
return lap_mat.tocsr(), col_sum
def auto_dsygv(M, N, k, k_skip=0, eigen_solver='auto', tol=1E-6,
max_iter=100, random_state=None):
"""
Helper function for solving Generalized Symmetric Eigen Problem
M * x = a * N * x
Parameters
----------
M : {array, matrix, sparse matrix, LinearOperator}
Left hand input matrix: should be symmetric positive semi-definite
N : {array, matrix, sparse matrix, LinearOperator}
Right hand input matrix: should be symmetric positive semi-definite
k : integer
Number of eigenvalues/vectors to return
k_skip : integer, optional
Number of low eigenvalues to skip.
eigen_solver : string, {'auto', 'arpack', 'dense'}
auto : algorithm will attempt to choose the best method for input data
arpack : use arnoldi iteration in shift-invert mode.
For this method, M may be a dense matrix, sparse matrix,
or general linear operator.
Warning: ARPACK can be unstable for some problems. It is
best to try several random seeds in order to check results.
dense : use standard dense matrix operations for the eigenvalue
decomposition. For this method, M must be an array
or matrix type. This method should be avoided for
large problems.
tol : float, optional
Tolerance for 'arpack' method.
Not used if eigen_solver=='dense'.
max_iter : maximum number of iterations for 'arpack' method
not used if eigen_solver=='dense'
random_state: numpy.RandomState or int, optional
The generator or seed used to determine the starting vector for arpack
iterations. Defaults to numpy.random.
Returns
-------
eigen vectors : ndarray, shape(n_features, k)
eigen values : ndarray, shape(k)
"""
if eigen_solver == 'auto':
if M.shape[0] > 200 and k + k_skip < 10:
eigen_solver = 'arpack'
else:
eigen_solver = 'dense'
if eigen_solver == 'arpack':
random_state = check_random_state(random_state)
v0 = random_state.rand(M.shape[0])
try:
eigen_values, eigen_vectors = eigsh(M, k + k_skip, N, sigma=0.0,
tol=tol, maxiter=max_iter,
v0=v0)
except RuntimeError as msg:
raise ValueError("Error in solving eigen problem with ARPACK. "
"Error message: '%s'. "
"Note that method='arpack' can fail when the "
"weight matrix is singular or otherwise "
"ill-behaved. method='dense' is recommended. "
"See online documentation for more information."
% msg)
return eigen_vectors[:, k_skip:], eigen_values[k_skip:]
elif eigen_solver == 'dense':
if hasattr(M, 'toarray'):
M = M.toarray()
if hasattr(N, 'toarray'):
N = N.toarray()
eigen_values, eigen_vectors = eigh(
M, N, eigvals=(k_skip, k + k_skip - 1), overwrite_a=True)
# index = np.argsort(np.abs(eigen_values))
# return eigen_vectors[:, index], eigen_values
return eigen_vectors, eigen_values
else:
raise ValueError("Unrecognized eigen_solver '%s'" % eigen_solver)
def locality_preserving_projection(X, n_neighbors, mode="distance",
kernel_func="heat", kernel_param="auto", k=2, eigen_solver='auto',
tol=1E-6, max_iter=100, random_state=None):
"""Perform Locality Linear Projection
Parameters
----------
X : array-like, shape: (n_samples, n_features)
Input data, each column is an edge of a graph
n_neighbors : int
number of neighbors to define whether two edges are connected or not
mode : string
mode of kneighbors_graph calculation
kernel_func : string or callable
kernel function to compute affinity matrix
kernel_param : float
parameter for heat kernel
k : integer
Number of eigenvalues/vectors to return
eigen_solver : string, {'auto', 'arpack', 'dense'}
auto : algorithm will attempt to choose the best method for input data
arpack : use arnoldi iteration in shift-invert mode.
For this method, M may be a dense matrix, sparse matrix,
or general linear operator.
Warning: ARPACK can be unstable for some problems. It is
best to try several random seeds in order to check results.
dense : use standard dense matrix operations for the eigenvalue
decomposition. For this method, M must be an array
or matrix type. This method should be avoided for
large problems.
tol : float, optional
Tolerance for 'arpack' method.
Not used if eigen_solver=='dense'.
max_iter : maximum number of iterations for 'arpack' method
not used if eigen_solver=='dense'
random_state: numpy.RandomState or int, optional
The generator or seed used to determine the starting vector for arpack
iterations. Defaults to numpy.random.
Returns
-------
eigen vectors : ndarray, shape(n_features, k)
eigen values : ndarray, shape(k)
"""
# making adjacency matrix
W = adjacency_matrix(X, n_neighbors, mode)
# making affinity matrix
W = affinity_matrix(W, kernel_func, kernel_param)
# making laplacian matrix
L, D = laplacian_matrix(W)
L = safe_sparse_dot(X.T, safe_sparse_dot(L, X))
D = np.dot(X.T, D[:, np.newaxis] * X)
return auto_dsygv(L, D, k, 0, eigen_solver, tol, max_iter, random_state)
class LocalityPreservingProjection(BaseEstimator, TransformerMixin):
"""Locality Preserving Projection (LPP) find the optimal linear embedding
for Graph Laplacian Matrix. LPP can be considered as a linear approximation
to the Laplacian Eigen Mapping (LEM). While lower dimensional mapping of
LEM can only be defined to training data set, LPP can also project test
data set to the lower dimensional space.
Parameters
----------
n_neighbors : int
number of neighbors to define whether two edges are connected or not
mode : string
mode of kneighbors_graph calculation
kernel_func : string or callable
kernel function to compute affinity matrix
kernel_param : float
parameter for heat kernel
k : integer
Number of eigenvalues/vectors to return
eigen_solver : string, {'auto', 'arpack', 'dense'}
auto : algorithm will attempt to choose the best method for input data
arpack : use arnoldi iteration in shift-invert mode.
For this method, M may be a dense matrix, sparse matrix,
or general linear operator.
Warning: ARPACK can be unstable for some problems. It is
best to try several random seeds in order to check results.
dense : use standard dense matrix operations for the eigenvalue
decomposition. For this method, M must be an array
or matrix type. This method should be avoided for
large problems.
tol : float, optional
Tolerance for 'arpack' method.
Not used if eigen_solver=='dense'.
max_iter : maximum number of iterations for 'arpack' method
not used if eigen_solver=='dense'
random_state: numpy.RandomState or int, optional
The generator or seed used to determine the starting vector for arpack
iterations. Defaults to numpy.random.
use_ext : bool
whether use cython extension or not for constructing adjacency matrix
pca_preprocess : int, float
n_components parameter of PCA class for preprocessing to reduce
dimension in advance to avoid singularity.
If `None` is passed, preprocessing will be passed.
"""
def __init__(self, n_neighbors=None, n_components=2, mode="distance",
kernel_func="heat", kernel_param="auto", eigen_solver='auto',
tol=1E-6, max_iter=100, random_state=None,
pca_preprocess=0.9):
self.n_neighbors = n_neighbors
self.n_components = n_components
self.mode = mode
self.kernel_func = kernel_func
self.kernel_param = kernel_param
self.eigen_solver = eigen_solver
self.tol = tol
self.max_iter = max_iter
self.random_state = random_state
self.pca_preprocess = pca_preprocess
def fit(self, X, y=None):
# print X
# X = array2d(X)
# print X
X = array2d(check_arrays(X, copy=self.copy, sparse_format='dense',
dtype=np.float)[0])
n_samples, n_features = X.shape
did_pca_preprocess = False
if self.pca_preprocess:
# PCA preprocess for removing singularity
# check preprocess param
if isinstance(self.pca_preprocess, float)\
and (0.0 < self.pca_preprocess < 1):
pca_dim = self.pca_preprocess
else:
pca_dim = 0.9
if int(pca_dim * n_features) > self.n_components:
_pca = PCA(n_components=pca_dim)
X = _pca.fit_transform(X)
did_pca_preprocess = True
if self.kernel_func == "heat" and self.kernel_param is None:
self.kernel_param = "auto"
if self.n_neighbors is None:
self.n_neighbors = max(int(X.shape[0] / 10), 1)
self.components_, _ = locality_preserving_projection(X,
self.n_neighbors, self.mode,
self.kernel_func, self.kernel_param,
self.n_components, self.eigen_solver,
self.tol, self.max_iter,
self.random_state)
if did_pca_preprocess:
self.components_ = safe_sparse_dot(_pca.components_.T,
self.components_)
self.components_ = self.components_.T
return self
def transform(self, X):
return safe_sparse_dot(X, self.components_.T)