A Python implementation of Robust PCA using Principal Component Pursuit by alternating directions (ADMM). The theory and algorithm are described in: Robust Principal Component Analysis? (Candès et al., 2011)
Robust PCA decomposes a data matrix D into two components:
D = L + S
where:
- L is a low-rank matrix (captures the underlying structure)
- S is a sparse matrix (captures outliers/corruptions)
This is useful when your data has:
- Underlying patterns shared across observations (low-rank structure)
- Sparse corruptions, outliers, or anomalies
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ │ │ │ │ · · │
│ Corrupted │ = │ Low-rank │ + │ · ·│
│ Data │ │ Structure │ │ Sparse │
│ D │ │ L │ │ Outliers S │
└─────────────┘ └─────────────┘ └─────────────┘
from r_pca import RobustPCA, np
# Create data: 3 groups with constant values
data = np.hstack([
np.ones((100, 40)) * 10, # Group 1: all 10s
np.ones((100, 40)) * 20, # Group 2: all 20s
np.ones((100, 40)) * 30 # Group 3: all 30s
])
# Corrupt 5% of entries
mask = np.random.rand(*data.shape) < 0.05
data[mask] = 0
# Decompose into low-rank + sparse
rpca = RobustPCA(data)
L, S = rpca.fit(max_iter=500)fit() runs silently by default. To see iteration/error progress, pass
verbose=True:
L, S = rpca.fit(max_iter=500, verbose=True, iter_print=100)Once fit() has been called, you can run PCA (via scikit-learn's
PCA) on the recovered low-rank matrix L. This is useful because L
has had sparse outliers/corruptions removed, so the resulting components
are less sensitive to those corruptions than a PCA run directly on D.
rpca = RobustPCA(data)
rpca.fit(max_iter=500)
result = rpca.pca()
result['scores'] # (n_samples, k) projected coordinates
result['loadings'] # (n_features, k) component directions
result['explained_variance'] # (k,) variance explained per component
result['explained_variance_ratio'] # (k,) fraction of total variance per component
result['n_components'] # k, the number of retained components
result['pca_model'] # fitted sklearn PCA instanceBy default, pca() standardizes the columns of L (zero mean, unit
variance, via StandardScaler) before running PCA. This matters whenever
the columns of your data represent heterogeneous variables on different
scales or units (e.g. social/survey indicators mixing counts, percentages,
income, etc.) — without standardization, PCA is dominated by whichever
columns happen to have the largest numeric variance, regardless of their
real importance.
If instead your columns share the same scale and unit (e.g. pixel intensities in a video or image), you can disable standardization and fall back to classical, covariance-based PCA (centering only):
result = rpca.pca(standardize=False)You can also fix the number of retained components explicitly:
result = rpca.pca(n_components=3)Note: the RPCA decomposition itself (
fit()) does not standardizeD— it follows the ADMM algorithm from the paper exactly, which makes no assumption about column scale. If your columns are heterogeneous, consider standardizingDyourself before constructingRobustPCA(D)if you want the low-rank/sparse split itself to treat all columns comparably; this is a methodological choice on top of the paper, not something the paper prescribes.
Full working version of the usage example above, with visualization:
python -m examples.readme_example| Metric | Value |
|---|---|
| ‖D - (L+S)‖_F | ≈ 0.0001 |
| ‖D - (L+S)‖_F / ‖D‖_F | ≈ 10⁻⁸ |
RPCA can separate sparse corruptions (text/watermarks) from the underlying low-rank image structure.
python -m examples.image_watermark_removal| Metric | Value |
|---|---|
| ‖D - (L+S)‖_F | ≈ 0.00001 |
| ‖D - (L+S)‖_F / ‖D‖_F | ≈ 10⁻⁷ |
How it works:
- Natural images have low-rank structure (smooth gradients, repeated patterns)
- Text/watermarks are sparse (only affect a small fraction of pixels)
- L recovers the clean image, S extracts the watermark
Real photographs from the Columbia Object Image Library. 72 views of a rotating object create natural low-rank structure.
python -m examples.coil20_recovery| Metric | Value |
|---|---|
| ‖D - (L+S)‖_F / ‖D‖_F | ≈ 10⁻⁶ |
| ‖L - D_clean‖_F / ‖D_clean‖_F | ≈ 0.13 |
How it works:
- 72 images of the same object from different angles → low-rank matrix
- 5% of pixels corrupted (set to 0) → sparse component
- RPCA recovers the clean images by exploiting multi-view redundancy
Parameters:
D: Input data matrix (numpy array)mu: Augmented Lagrangian parameter (default: auto-computed)lmbda: Sparsity regularization (default:1/sqrt(max(n,m)))
Run the ADMM algorithm to decompose D = L + S.
Parameters:
tol: Convergence tolerance. Defaults to1e-7 * ||D||_F.max_iter: Maximum number of ADMM iterations.verbose: IfTrue, prints iteration/error progress. Defaults toFalse(silent).iter_print: Print everyiter_printiterations, only whenverbose=True.
Returns: (L, S) - the low-rank and sparse components
Compute PCA (via scikit-learn's PCA) on the low-rank component L
obtained from fit(). Must be called after fit().
Parameters:
n_components: Passed directly tosklearn.decomposition.PCA. IfNone, sklearn keepsmin(n_samples, n_features)components.standardize: IfTrue(default), standardizesL's columns (zero mean, unit variance) viaStandardScalerbefore PCA — recommended for heterogeneous variables. IfFalse, only centers the data (classical PCA on the covariance matrix) — appropriate when all columns share the same scale/unit.
Returns: a dict with the following keys:
| Key | Shape | Description |
|---|---|---|
scores |
(n_samples, k) |
Projected coordinates of the samples |
loadings |
(n_features, k) |
Component directions |
explained_variance |
(k,) |
Variance explained by each retained component |
explained_variance_ratio |
(k,) |
Fraction of total variance explained per component |
n_components |
int |
Number of retained components (k) |
pca_model |
sklearnPCA |
The fitted scikit-learn PCA instance |
Raises: RuntimeError if called before fit().
This implementation follows Algorithm 1 (Principal Component Pursuit by Alternating Directions) from the published paper (Candès et al., 2011).
Note: The arXiv preprint (v1, 2009) contains a different formulation of the algorithm. This code implements the version from the final ACM publication, which has corrected signs in the ADMM updates.
while not converged:
L = svd_threshold(D - S + μ⁻¹Y, μ⁻¹) # Singular value thresholding
S = shrink(D - L + μ⁻¹Y, λμ⁻¹) # Element-wise soft thresholding
Y = Y + μ(D - L - S) # Dual variable updateThis solves the convex optimization problem:
minimize ||L||_* + λ||S||_1
subject to D = L + S
where ||L||_* is the nuclear norm (sum of singular values) and ||S||_1 is the element-wise L1 norm.
The ADMM decomposition itself is performed on D exactly as given, with
no standardization — this follows the paper, which makes no assumption
about column scale. After convergence, pca() performs PCA on L to
extract the principal directions of the recovered, outlier-free
structure; standardizing at this stage (the default) is a separate,
practical choice for data with heterogeneous columns, not something
mandated by the paper itself.
μ = (n × m) / (4 × ||D||_1)— controls convergence rateλ = 1 / √(max(n, m))— balances low-rank vs sparse (from the paper's theoretical results)
python -m pytest test_r_pca.py -v- [ ]


