cca_zoo.sparse¶
Sparse linear CCA methods. No optional dependency required. Two mechanism families live here: EY-loss coordinate descent, and Alternating Least Squares (ALS).
EY-loss coordinate descent¶
ElasticNetCCA ¶
ElasticNetCCA(
n_components: int = 1,
*,
center: bool = True,
alpha: float | list[float] = 1.0,
l1_ratio: float | list[float] = 0.5,
max_iter: int = 1000,
tol: float = 1e-06,
random_state: int | None = None,
positive: bool = False,
)
Bases: BaseModel
Sparse multiview CCA by coordinate descent on the elastic-net EY loss.
Minimises, over \(Z_i = X_i W_i\),
with \(\rho\) = l1_ratio, by cyclic coordinate descent as in
:class:~sklearn.linear_model.ElasticNet; each coordinate's quartic
restriction is minimised exactly. The loss is not jointly convex, so the
result can depend on random_state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
alpha
|
float | list[float]
|
Penalty strength. Per-view. Default is 1.0. |
1.0
|
l1_ratio
|
float | list[float]
|
L1 share of the penalty in |
0.5
|
max_iter
|
int
|
Maximum coordinate-descent sweeps at each stage of the penalty path. Default is 1000, as sklearn's ElasticNet. |
1000
|
tol
|
float
|
Tolerance on the change in the objective. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the initial weights. Default is None. |
None
|
positive
|
bool
|
Whether to constrain the weights to be non-negative. Default is False. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Coordinate-descent sweeps at the full penalty. |
References
Chapman, J., Wang, H.-T., Wells, L., & Wiesner, J. (2021). CCA-Zoo: A collection of Regularized, Deep Learning based, Kernel, and Probabilistic CCA methods in a scikit-learn style framework. Journal of Open Source Software, 6(68), 3823.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import ElasticNetCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((200, 20))
>>> X2 = rng.standard_normal((200, 15))
>>> model = ElasticNetCCA(n_components=2, alpha=[0.1, 0.5]).fit([X1, X2])
Source code in cca_zoo/sparse/_elasticnetcca.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
ElasticNetCCA
|
self. |
Source code in cca_zoo/sparse/_elasticnetcca.py
MultiTaskElasticNetCCA ¶
MultiTaskElasticNetCCA(
n_components: int = 1,
*,
center: bool = True,
alpha: float | list[float] = 1.0,
l1_ratio: float | list[float] = 0.5,
max_iter: int = 1000,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Row-sparse multiview CCA with a multi-task elastic-net penalty on the EY loss.
As :class:~cca_zoo.sparse.ElasticNetCCA with
:class:~sklearn.linear_model.MultiTaskElasticNet's penalty,
so each feature is used by every component or by none. Each row is updated by a proximal-gradient step with backtracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
alpha
|
float | list[float]
|
Penalty strength. Per-view. Default is 1.0. |
1.0
|
l1_ratio
|
float | list[float]
|
Row-group share of the penalty in |
0.5
|
max_iter
|
int
|
Maximum coordinate-descent sweeps at each stage of the penalty path. Default is 1000, as sklearn's ElasticNet. |
1000
|
tol
|
float
|
Tolerance on the change in the objective. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the initial weights. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Proximal sweeps at the full penalty. |
References
Chapman, J., Wang, H.-T., Wells, L., & Wiesner, J. (2021). CCA-Zoo: A collection of Regularized, Deep Learning based, Kernel, and Probabilistic CCA methods in a scikit-learn style framework. Journal of Open Source Software, 6(68), 3823.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import MultiTaskElasticNetCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((200, 20))
>>> X2 = rng.standard_normal((200, 15))
>>> model = MultiTaskElasticNetCCA(n_components=2, alpha=0.1).fit([X1, X2])
Source code in cca_zoo/sparse/_multitaskelasticnetcca.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
MultiTaskElasticNetCCA
|
self. |
Source code in cca_zoo/sparse/_multitaskelasticnetcca.py
OrthogonalMatchingPursuitCCA ¶
OrthogonalMatchingPursuitCCA(
n_components: int = 1,
*,
center: bool = True,
n_nonzero_coefs: int | list[int] | None = None,
max_iter: int = 10,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Sparse multiview CCA with a fixed number of active features per view.
The EY analogue of :class:~sklearn.linear_model.OrthogonalMatchingPursuit:
each view's active set grows one feature at a time, choosing the feature
with the largest EY gradient norm, and the active weights are refitted by
coordinate descent after each addition. Once the active sets settle, the
weights are the exact EY optimum on them, as OMP's coefficients are the
least-squares fit on its support. Features outside the active set are zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
n_nonzero_coefs
|
int | list[int] | None
|
Active features, capped at the view's width. The
components share the active set, so it needs at least
|
None
|
max_iter
|
int
|
Maximum rounds of regrowing every view's active set; the fit has converged when the sets repeat. Default is 10. |
10
|
tol
|
float
|
Tolerance on the change in the EY loss while refitting an active set. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the dense warm start. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Rounds of regrowing the active sets. |
References
Chapman, J., Wang, H.-T., Wells, L., & Wiesner, J. (2021). CCA-Zoo: A collection of Regularized, Deep Learning based, Kernel, and Probabilistic CCA methods in a scikit-learn style framework. Journal of Open Source Software, 6(68), 3823.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import OrthogonalMatchingPursuitCCA
>>> rng = np.random.default_rng(0)
>>> X1 = rng.standard_normal((200, 20))
>>> X2 = rng.standard_normal((200, 15))
>>> model = OrthogonalMatchingPursuitCCA(
... n_components=2, n_nonzero_coefs=5
... ).fit([X1, X2])
Source code in cca_zoo/sparse/_ompcca.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
OrthogonalMatchingPursuitCCA
|
self. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in cca_zoo/sparse/_ompcca.py
Alternating Least Squares¶
PMDCCA ¶
PMDCCA(
n_components: int = 1,
*,
center: bool = True,
l1_bound: float | list[float] = 1.0,
max_iter: int = 500,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Sparse CCA by penalized matrix decomposition.
Maximises cross-view covariance subject to L1 and L2 constraints on each weight vector:
Each update soft-thresholds with the level found by bisection.
One of three sparse PLS power iterations, with :class:SpanCCA and
:class:ParkhomenkoCCA: each multiplies by the other views' summed score,
then thresholds, and they differ only in the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
l1_bound
|
float | list[float]
|
Bound \(b_i\) on the L1 norm, as a fraction of
|
1.0
|
max_iter
|
int
|
Maximum iterations per latent dimension. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance on the change in weights. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the start. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Most iterations run by any component. |
References
Witten, D. M., Tibshirani, R., & Hastie, T. (2009). A penalized matrix decomposition, with applications to sparse principal components and canonical correlation analysis. Biostatistics, 10(3), 515-534.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import PMDCCA
>>> rng = np.random.default_rng(0)
>>> X1, X2 = rng.standard_normal((50, 10)), rng.standard_normal((50, 8))
>>> model = PMDCCA(l1_bound=0.5, random_state=0).fit([X1, X2])
Source code in cca_zoo/sparse/_pmd.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
PMDCCA
|
self. |
Source code in cca_zoo/sparse/_pmd.py
ADMMCCA ¶
ADMMCCA(
n_components: int = 1,
*,
center: bool = True,
alpha: float | list[float] = 0.1,
rho: float = 1.0,
max_iter: int = 500,
admm_iter: int = 50,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Sparse CCA by linearised ADMM.
For view \(i\), with the other views' weights fixed, solves
the paper's problem on \(X_i / \sqrt{n}\), so that the constraint is unit
variance and alpha means the same at any sample size. It is solved
by linearised ADMM on the split \(\mathbf{z}_i = X_i \mathbf{w}_i / \sqrt{n}\),
so each step is a soft-threshold and a projection onto the unit ball,
with step size \(n / (\rho \|X_i\|_{\mathrm{op}}^2)\). Further components
use deflation rather than the paper's orthogonality constraint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
alpha
|
float | list[float]
|
L1 penalty. Per-view. Default is 0.1. |
0.1
|
rho
|
float
|
ADMM augmented-Lagrangian penalty. Default is 1.0. |
1.0
|
max_iter
|
int
|
Maximum outer iterations per latent dimension. Default is 500. |
500
|
admm_iter
|
int
|
Maximum ADMM iterations per view update. Default is 50. |
50
|
tol
|
float
|
Convergence tolerance of both loops. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the start. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Most iterations run by any component. |
References
Suo, X., Minden, V., Nelson, B., Tibshirani, R., & Saunders, M. (2017). Sparse canonical correlation analysis. arXiv:1705.10865.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import ADMMCCA
>>> rng = np.random.default_rng(0)
>>> X1, X2 = rng.standard_normal((50, 10)), rng.standard_normal((50, 8))
>>> model = ADMMCCA(alpha=0.1, random_state=0).fit([X1, X2])
Source code in cca_zoo/sparse/_admm.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
ADMMCCA
|
self. |
Source code in cca_zoo/sparse/_admm.py
IPLSCCA ¶
IPLSCCA(
n_components: int = 1,
*,
center: bool = True,
alpha: float | list[float] = 0.0,
l1_ratio: float | list[float] = 1.0,
max_iter: int = 500,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Sparse CCA by iterative penalised least squares.
Each view's weights solve an elastic-net regression onto the other views' summed score, then are rescaled to a unit-variance score, as in Waaijenborg et al.'s elastic-net CCA and Mai and Zhang's lasso version:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
alpha
|
float | list[float]
|
Penalty strength. Per-view. Default is 0. |
0.0
|
l1_ratio
|
float | list[float]
|
Share of the penalty that is L1: 1 is Mai and Zhang's lasso, and Waaijenborg et al. use an elastic net. Per-view. Default is 1. |
1.0
|
max_iter
|
int
|
Maximum iterations per latent dimension. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance on the change in weights. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the start and the lasso's coordinate order. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Most iterations run by any component. |
References
Waaijenborg, S., de Witt Hamer, P. C. V., & Zwinderman, A. H. (2008). Quantifying the association between gene expressions and DNA-markers by penalized canonical correlation analysis. Statistical Applications in Genetics and Molecular Biology, 7(1).
Mai, Q., & Zhang, X. (2019). An iterative penalized least squares approach to sparse canonical correlation analysis. Biometrics, 75(3), 734-744.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import IPLSCCA
>>> rng = np.random.default_rng(0)
>>> X1, X2 = rng.standard_normal((50, 10)), rng.standard_normal((50, 8))
>>> model = IPLSCCA(alpha=0.1, random_state=0).fit([X1, X2])
Source code in cca_zoo/sparse/_ipls.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
IPLSCCA
|
self. |
Source code in cca_zoo/sparse/_ipls.py
SpanCCA ¶
SpanCCA(
n_components: int = 1,
*,
center: bool = True,
span: int | list[int] | None = None,
max_iter: int = 500,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Sparse CCA by hard-thresholded alternating updates.
Each update keeps the span largest-magnitude weights. This is an
alternating heuristic in the spirit of Asteris et al.'s SpanCCA, not their
randomised low-rank search.
One of three sparse PLS power iterations, with :class:PMDCCA and
:class:ParkhomenkoCCA: each multiplies by the other views' summed score,
then thresholds, and they differ only in the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
span
|
int | list[int] | None
|
Number of nonzero weights kept; |
None
|
max_iter
|
int
|
Maximum iterations per latent dimension. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance on the change in weights. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the start. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Most iterations run by any component. |
References
Asteris, M., Kyrillidis, A., Koyejo, O., & Poldrack, R. (2016). A simple and provable algorithm for sparse diagonal CCA. ICML.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import SpanCCA
>>> rng = np.random.default_rng(0)
>>> X1, X2 = rng.standard_normal((50, 10)), rng.standard_normal((50, 8))
>>> model = SpanCCA(span=5, random_state=0).fit([X1, X2])
Source code in cca_zoo/sparse/_span.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
SpanCCA
|
self. |
Source code in cca_zoo/sparse/_span.py
ParkhomenkoCCA ¶
ParkhomenkoCCA(
n_components: int = 1,
*,
center: bool = True,
alpha: float | list[float] = 0.1,
max_iter: int = 500,
tol: float = 1e-06,
random_state: int | None = None,
)
Bases: BaseModel
Sparse CCA by soft-thresholded power iteration on standardised views.
Uses the paper's diagonal approximation to the within-view covariances, which amounts to standardising each feature, then iterates
with \(S_\alpha\) the soft-threshold, \(\tilde X_i\) the standardised view and
\(\bar{\mathbf{s}}_{\neg i}\) the other views' summed score at unit variance:
each feature's correlation with that score is soft-thresholded at
\(\alpha_i\). Later components deflate the standardised views, so that at
alpha=0 this is :class:~sklearn.cross_decomposition.PLSCanonical
with scale=True. Weights are returned on the original feature scale.
One of three sparse PLS power iterations, with :class:PMDCCA and
:class:SpanCCA: each multiplies by the other views' summed score, then
thresholds, and they differ only in the threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
alpha
|
float | list[float]
|
Soft-threshold on each feature's correlation with the other views' score; 1 or more zeroes every weight. Per-view. Default is 0.1. |
0.1
|
max_iter
|
int
|
Maximum iterations per latent dimension. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance on the change in weights. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for the start. Default is None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Most iterations run by any component. |
References
Parkhomenko, E., Tritchler, D., & Beyene, J. (2009). Sparse canonical correlation analysis with application to genomic data integration. Statistical Applications in Genetics and Molecular Biology, 8(1).
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import ParkhomenkoCCA
>>> rng = np.random.default_rng(0)
>>> X1, X2 = rng.standard_normal((50, 10)), rng.standard_normal((50, 8))
>>> model = ParkhomenkoCCA(alpha=0.1, random_state=0).fit([X1, X2])
Source code in cca_zoo/sparse/_parkhomenko.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
ParkhomenkoCCA
|
self. |
Source code in cca_zoo/sparse/_parkhomenko.py
SAR ¶
SAR(
n_components: int = 1,
*,
center: bool = True,
n_alphas: int = 100,
max_iter: int = 500,
tol: float = 1e-06,
)
Bases: BaseModel
Sparse alternating regression with BIC-selected sparsity.
CCA as alternating regressions of each view's score on the other views' summed score, each a lasso whose penalty is chosen by BIC. Latent dimensions after the first are found on deflated views, then re-fitted against the original views (Wilms and Croux, Section 3). Each component starts from the ridge-regularised CCA direction, from which the first lasso finds a target the other views share. The extension beyond two views is this implementation's own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to centre each view. Default is True. |
True
|
n_alphas
|
int
|
Penalties on each lasso path. Default is 100. |
100
|
max_iter
|
int
|
Maximum iterations per latent dimension. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance of the alternating loop and each lasso path. Default is 1e-6. |
1e-06
|
Attributes:
| Name | Type | Description |
|---|---|---|
weights_ |
Weight matrix of each view, shape (n_features_i, n_components). |
|
n_iter_ |
Most iterations run by any component. |
References
Wilms, I., & Croux, C. (2015). Sparse canonical correlation analysis from a predictive point of view. Biometrical Journal, 57(5), 834-851.
Examples:
>>> import numpy as np
>>> from cca_zoo.sparse import SAR
>>> rng = np.random.default_rng(0)
>>> latent = rng.standard_normal(50)
>>> X1 = np.column_stack([latent, rng.standard_normal((50, 9))])
>>> X2 = np.column_stack([latent, rng.standard_normal((50, 7))])
>>> model = SAR().fit([X1, X2])
Source code in cca_zoo/sparse/_sar.py
fit ¶
Fit the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
Arrays of shape (n_samples, n_features_i), one per view. |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
SAR
|
self. |