-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathssc_mps.py
102 lines (73 loc) · 2.47 KB
/
ssc_mps.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
import numpy as np
from sklearn.linear_model import OrthogonalMatchingPursuit
from sklearn.cluster import SpectralClustering
from sklearn.datasets import fetch_olivetti_faces
from sklearn.preprocessing import normalize
from matchingpursuit import MatchingPursuit
def ssc_mps(X,smax,L,tol=None,alg_name='OMP',pmax=None):
"""
Implements Sparse Subspace Clustering-Orthogonal Matching Pursuit (SSC-OMP) and
SSC-Matching Pursuit (SSC-MP)
Parameters
----------
X: array, shape (n_features, n_samples)
data matrix
smax: int
Maximum number of OMP/MP iterations
L: int
Number of clusters
tol: float, optional
Threshold on approximation quality
alg_name: string, optional
'OMP' (default) or 'MP'
pmax:
Maximum sparsity level for MP
Note:
- Stopping behavior:
SSC-OMP: Stop after smax iterations if tol=None, stop when approximation quality
specified by tol is attained otherwise
SSC-MP: Stop after smax iterations, or when approximation quality specified by tol
is attained, or when the sparsity level of the coefficient vector is pmax
- See https://arxiv.org/abs/1612.03450 for a discussion of the stopping criteria
"""
XX = np.array(X)
assert(len(XX.shape) == 2)
m = XX.shape[0]
N = XX.shape[1]
alg = None
if alg_name == 'MP':
alg = MatchingPursuit(smax, pmax, tol)
else:
alg = OrthogonalMatchingPursuit(
n_nonzero_coefs=smax,
tol=tol,
fit_intercept=False,
normalize=False)
C = np.zeros((N,N))
for i in range(N):
data_idx = [j for j in range(i)]
data_idx.extend([j for j in range(i+1,N)])
alg.fit(XX[:,data_idx],np.squeeze(XX[:,i]))
c = np.zeros(N)
c[data_idx] = alg.coef_
C[:,i] = c
sc = SpectralClustering(n_clusters=L, affinity='precomputed', n_init=50, n_jobs=-1)
sc.fit(np.abs(C) + np.abs(C.T))
return sc.labels_
"""
Example: Cluster face images of 3 randomly selected individuals (10 images each)
from the Olivetti face data base.
"""
if __name__ == '__main__':
ids = np.random.choice([i for i in range(40)], size=3, replace=False)
faces = fetch_olivetti_faces()
X = np.hstack((
faces.data[faces.target==ids[0],:].T,
faces.data[faces.target==ids[1],:].T,
faces.data[faces.target==ids[2],:].T))
Xn = normalize(X - np.outer(np.mean(X, axis=1), np.ones(X.shape[1])), axis=0)
# L=3, smax=3
labels_omp = ssc_mps(Xn,3,3)
labels_mp = ssc_mps(Xn,3,3,alg_name='MP')
print('Image labels (SSC-OMP): %s\nImage labels (SSC-MP) : %s'
% (str(labels_omp), str(labels_mp)))