PCP#
This module implements Principal Component Pursuit (PCP) for robustly decomposing a data matrix into low-rank and sparse components.
API Reference#
Implementation of Principal Component Pursuit (PCP).
- class PCP(alpha=None, mu=None, max_iter=100, tol=1e-07)[source]#
Bases:
TransformerMixin
,BaseEstimator
Principal Component Pursuit (PCP) for matrix decomposition.
PCP decomposes an input matrix into low-rank and sparse components using augmented Lagrange multiplier method.
- Parameters:
alpha (float, default=None) – Regularization parameter for the sparse component. If None, it’s set automatically as 1/sqrt(max(n_features, n_samples)).
mu (float, default=None) – Augmented Lagrange multiplier parameter. If None, it’s set automatically as n_features * n_samples / (4 * L1_norm(X)).
max_iter (int, default=100) – Maximum number of iterations for the optimization algorithm.
tol (float, default=1e-7) – Tolerance for convergence. Algorithm stops when the relative error is below this threshold.
- Variables:
low_rank (ndarray of shape (n_samples, n_features)) – The low-rank component from the last decomposition.
sparse (ndarray of shape (n_samples, n_features)) – The sparse component from the last decomposition.
n_iter (int) – Number of iterations run in the fit.
- fit(X, y=None)[source]#
Fit the PCP model to the input matrix.
This method stores the input matrix dimensions and validates parameters, but does not perform the actual decomposition until transform is called.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input matrix to fit the model to.
y (array-like, default=None) – Target values (ignored). This parameter exists for sklearn compatibility.
- Returns:
self – Fitted estimator instance.
- Return type:
- transform(X)[source]#
Transform input matrix by returning the low-rank component.
Only supports the exact same data used in fit.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input matrix to transform.
- Returns:
x_transformed – Low-rank component of the input matrix.
- Return type:
ndarray of shape (n_samples, n_features)
- frobenius_norm(X)[source]#
Compute the Frobenius norm of a matrix.
\[\|X\|_F = \sqrt{\sum_{i,j} X_{ij}^2}\]- Parameters:
X (np.ndarray) – Input matrix.
- Returns:
Frobenius norm of the matrix.
- Return type:
- l1_norm(X)[source]#
Compute the L1 norm of a matrix.
\[\|X\|_1 = \sum_{i,j} |X_{ij}|\]- Parameters:
X (np.ndarray) – Input matrix.
- Returns:
L1 norm of the matrix.
- Return type:
- pcp(X, alpha=None, mu=None, max_iter=100, tol=1e-07)[source]#
Apply Principal Component Pursuit (PCP) to decompose a matrix into low-rank and sparse components.
- Parameters:
X (np.ndarray) – Input matrix.
alpha (Optional[float]) – Regularization parameter for the sparse component.
mu (Optional[float]) – Augmented Lagrange multiplier parameter. If None, it’s set automatically.
max_iter (int, optional) – Maximum number of iterations, by default 100.
tol (float, optional) – Tolerance for convergence, by default 1e-7.
- Returns:
Low-rank matrix (L), sparse matrix (S), number of iterations (n_iter_).
- Return type:
- shrinkage_operator(X, tau)[source]#
Apply Shrinkage operator to a matrix X with threshold tau.
\[S_{\tau}(X) = \text{sign}(X) \cdot \max(|X| - \tau, 0)\]where :math: text{sign}(X) is the element-wise sign function, and :math: max(|X| - tau, 0) is the element-wise maximum between :math: |X| - tau and :math: 0.
- Parameters:
X (np.ndarray) – Input matrix.
tau (float) – Threshold value.
- Returns:
Shrunk matrix.
- Return type:
np.ndarray
- svd_operator(X, tau)[source]#
Apply Singular Value Thresholding operator to a matrix X with threshold tau.
$$D_{\tau}(X) = U \cdot \text{shrinkage_operator}(S, \tau) \cdot V^T $$
where \(X = U S V^T\) is the Singular Value Decomposition (SVD) of \(X\).
- Parameters:
X (np.ndarray) – Input matrix.
tau (float) – Threshold value.
- Returns:
Shrunk SVD.
- Return type:
np.ndarray