-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpca_pcp.py
110 lines (92 loc) · 3.24 KB
/
rpca_pcp.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
"""
An implementation of the Principal Component Pursuit algorithm for robust PCA
as described in `Candes, Li, Ma, & Wright <http://arxiv.org/abs/0912.3599>`_.
An alternative Python implementation using non-standard dependencies and
different hyperparameter choices is available at:
http://blog.shriphani.com/2013/12/18/
robust-principal-component-pursuit-background-matrix-recovery/
"""
from __future__ import division, print_function
__all__ = ["pcp"]
import time
import fbpca
import logging
import numpy as np
from scipy.sparse.linalg import svds
def pcp(M, delta=1e-6, mu=None, maxiter=500, verbose=False, missing_data=True,
svd_method="approximate", **svd_args):
# Check the SVD method.
allowed_methods = ["approximate", "exact", "sparse"]
if svd_method not in allowed_methods:
raise ValueError("'svd_method' must be one of: {0}"
.format(allowed_methods))
# Check for missing data.
shape = M.shape
if missing_data:
missing = ~(np.isfinite(M))
if np.any(missing):
M = np.array(M)
M[missing] = 0.0
else:
missing = np.zeros_like(M, dtype=bool)
if not np.all(np.isfinite(M)):
logging.warn("The matrix has non-finite entries. "
"SVD will probably fail.")
# Initialize the tuning parameters.
lam = 1.0 / np.sqrt(np.max(shape))
if mu is None:
mu = 0.25 * np.prod(shape) / np.sum(np.abs(M))
if verbose:
print("mu = {0}".format(mu))
# Convergence criterion.
norm = np.sum(M ** 2)
# Iterate.
i = 0
rank = np.min(shape)
S = np.zeros(shape)
Y = np.zeros(shape)
while i < max(maxiter, 1):
# SVD step.
strt = time.time()
u, s, v = _svd(svd_method, M - S + Y / mu, rank+1, 1./mu, **svd_args)
svd_time = time.time() - strt
s = shrink(s, 1./mu)
rank = np.sum(s > 0.0)
u, s, v = u[:, :rank], s[:rank], v[:rank, :]
L = np.dot(u, np.dot(np.diag(s), v))
# Shrinkage step.
S = shrink(M - L + Y / mu, lam / mu)
# Lagrange step.
step = M - L - S
step[missing] = 0.0
Y += mu * step
# Check for convergence.
err = np.sqrt(np.sum(step ** 2) / norm)
if verbose:
print(("Iteration {0}: error={1:.3e}, rank={2:d}, nnz={3:d}, "
"time={4:.3e}")
.format(i, err, np.sum(s > 0), np.sum(S > 0), svd_time))
if err < delta:
break
i += 1
if i >= maxiter:
logging.warn("convergence not reached in pcp")
return L, S, (u, s, v)
def shrink(M, tau):
sgn = np.sign(M)
S = np.abs(M) - tau
S[S < 0.0] = 0.0
return sgn * S
def _svd(method, X, rank, tol, **args):
rank = min(rank, np.min(X.shape))
if method == "approximate":
return fbpca.pca(X, k=rank, raw=True, **args)
elif method == "exact":
return np.linalg.svd(X, full_matrices=False, **args)
elif method == "sparse":
if rank >= np.min(X.shape):
return np.linalg.svd(X, full_matrices=False)
u, s, v = svds(X, k=rank, tol=tol)
u, s, v = u[:, ::-1], s[::-1], v[::-1, :]
return u, s, v
raise ValueError("invalid SVD method")