cca_zoo.linear¶
Linear CCA methods. All classes are sklearn.base.BaseEstimator subclasses.
Base class¶
BaseModel ¶
Bases: BaseEstimator, ABC
Abstract base class for all multiview CCA models.
Subclasses must implement :meth:fit. All other public methods
(transform, fit_transform, score, pairwise_correlations,
average_pairwise_correlations, get_factor_loadings) are provided
here using the weights_ attribute set by fit.
This class inherits from :class:sklearn.base.BaseEstimator so that
get_params / set_params round-trip correctly and sklearn model
selection utilities work out of the box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions to fit. Default is 1. |
1
|
center
|
bool
|
Whether to subtract per-view column means before fitting.
The means are stored in |
True
|
weights
property
¶
Weight matrices post-fit, one per view.
Shape is (n_features_i, latent_dimensions) for each view.
Raises:
| Type | Description |
|---|---|
NotFittedError
|
If |
fit
abstractmethod
¶
Fit the model to multiview data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). All arrays must have the same number of rows. |
required |
y
|
None
|
Ignored. Present for scikit-learn API compatibility. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
BaseModel
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
transform ¶
Project views into the latent space using the fitted weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of arrays, each of shape (n_samples, latent_dimensions). |
Raises:
| Type | Description |
|---|---|
NotFittedError
|
If |
fit_transform ¶
Fit and then transform the training data.
Equivalent to self.fit(views).transform(views) but may be more
efficient for some subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of arrays, each of shape (n_samples, latent_dimensions). |
score ¶
Return average pairwise canonical correlations for each dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
ndarray
|
pairwise correlation for each canonical dimension. |
pairwise_correlations ¶
Compute the full pairwise correlation matrix per latent dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
ndarray
|
entry |
ndarray
|
d-th canonical variate of view i and view j. |
average_pairwise_correlations ¶
Return the mean off-diagonal pairwise correlation per dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
ndarray
|
off-diagonal pairwise correlation for each canonical dimension. |
get_factor_loadings ¶
Compute canonical factor loadings for each view.
A loading is the Pearson correlation between an original feature and a canonical variate. Loadings indicate which original variables drive each canonical direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each of shape (n_samples, n_features_i). |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of arrays, each of shape (n_features_i, latent_dimensions), |
list[ndarray]
|
where entry |
list[ndarray]
|
view i and the d-th canonical variate of view i. |
Two-view exact methods¶
CCA ¶
Bases: rCCA
Canonical Correlation Analysis.
Finds the pair of linear projections that maximise the Pearson correlation between two views subject to unit within-view variance constraints:
.. math::
\max_{\mathbf{w}_1, \mathbf{w}_2}
\mathbf{w}_1^\top X_1^\top X_2 \mathbf{w}_2
\text{subject to }
\mathbf{w}_i^\top X_i^\top X_i \mathbf{w}_i = 1
This is a special case of :class:rCCA with c=0. The solution uses
PCA whitening followed by an SVD of the cross-covariance matrix, which is
numerically stable even for high-dimensional views.
References
Hotelling, H. (1936). Relations between two sets of variates. Biometrika, 28(3/4), 321–377.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means before fitting. Default True. |
True
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = CCA(latent_dimensions=2).fit([X1, X2]) corrs = model.score([X1, X2])
Source code in cca_zoo/linear/_cca.py
fit ¶
Fit the CCA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of exactly two arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
CCA
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of views is not exactly 2. |
ValueError
|
If views have inconsistent numbers of samples. |
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = CCA(latent_dimensions=2).fit([X1, X2])
Source code in cca_zoo/linear/_cca.py
rCCA ¶
Bases: BaseModel
Regularised Canonical Correlation Analysis (canonical ridge).
Finds the pair of linear projections of two views that maximise their correlation subject to regularised within-view variance constraints:
.. math::
\max_{\mathbf{w}_1, \mathbf{w}_2}
\mathbf{w}_1^\top X_1^\top X_2 \mathbf{w}_2
\text{subject to }
\mathbf{w}_i^\top
\bigl((1 - c_i) X_i^\top X_i + c_i I\bigr) \mathbf{w}_i = 1
The solution is found by whitening each view with its regularised covariance matrix and computing the SVD of the resulting cross-covariance.
:class:CCA (c=0) and :class:PLS (c=1) are special cases.
References
Vinod, H. D. (1976). Canonical ridge and econometrics of joint production. Journal of Econometrics, 4(2), 147–166.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means before fitting. Default True. |
True
|
c
|
float | list[float]
|
Ridge regularisation parameter(s) in |
0.0
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = rCCA(latent_dimensions=2, c=0.1).fit([X1, X2]) scores = model.transform([X1, X2])
Source code in cca_zoo/linear/_rcca.py
fit ¶
Fit the rCCA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of exactly two arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
rCCA
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of views is not exactly 2. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_rcca.py
PLS ¶
Bases: rCCA
Partial Least Squares (two-view).
Finds the pair of unit-norm weight vectors that maximise the covariance between the projected views:
.. math::
\max_{\mathbf{w}_1, \mathbf{w}_2}
\mathbf{w}_1^\top X_1^\top X_2 \mathbf{w}_2
\text{subject to }
\|\mathbf{w}_i\|_2 = 1
This is equivalent to the truncated SVD of the sample cross-covariance
matrix :math:X_1^\top X_2 / (n - 1), and corresponds to :class:rCCA
with c=1.
References
Wold, H. (1975). Soft modelling by latent variables: the nonlinear iterative partial least squares (NIPALS) approach. Perspectives in Probability and Statistics, 117–142.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means before fitting. Default True. |
True
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = PLS(latent_dimensions=2).fit([X1, X2]) scores = model.transform([X1, X2])
Source code in cca_zoo/linear/_pls.py
fit ¶
Fit the PLS model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of exactly two arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
PLS
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of views is not exactly 2. |
ValueError
|
If views have inconsistent numbers of samples. |
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = PLS(latent_dimensions=2).fit([X1, X2])
Source code in cca_zoo/linear/_pls.py
Multiview methods¶
MCCA ¶
MCCA(latent_dimensions: int = 1, center: bool = True, c: float | list[float] = 0.0, pca: bool = True, eps: float = 1e-06)
Bases: BaseModel
Multiset Canonical Correlation Analysis.
Finds linear projections of multiple (>=2) views that maximise the sum of
pairwise cross-view covariances subject to within-view variance constraints.
A ridge regularisation parameter c controls the trade-off between
correlation and variance explained.
The primal objective is:
.. math::
\max_{\mathbf{w}} \sum_{i \neq j} \mathbf{w}_i^\top X_i^\top X_j
\mathbf{w}_j
\text{subject to } \mathbf{w}_i^\top
\bigl((1-c_i) X_i^\top X_i + c_i I\bigr) \mathbf{w}_i = 1
This is solved as a generalised eigenvalue problem:
.. math::
A \mathbf{v} = \lambda B \mathbf{v}
where :math:A is the between-view block covariance matrix and :math:B
is the block-diagonal regularised within-view covariance matrix.
When pca=True (default), each view is first reduced to its principal
components, which makes the problem numerically stable for
high-dimensional data and allows an efficient closed-form :math:B.
References
Kettenring, J. R. (1971). Canonical analysis of several sets of variables. Biometrika, 58(3), 433–451.
Vinod, H. D. (1976). Canonical ridge and econometrics of joint production. Journal of Econometrics, 4(2), 147–166.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means before fitting. Default True. |
True
|
c
|
float | list[float]
|
Ridge regularisation parameter(s). Either a single float applied
to all views or a list of per-view floats in |
0.0
|
pca
|
bool
|
Whether to apply full PCA whitening as a pre-processing step before solving the eigenvalue problem. Highly recommended for high-dimensional data. Default is True. |
True
|
eps
|
float
|
Small constant added to the eigenvalues of B to ensure positive definiteness. Default is 1e-6. |
1e-06
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) X3 = rng.standard_normal((50, 6)) model = MCCA(latent_dimensions=2).fit([X1, X2, X3]) scores = model.transform([X1, X2, X3])
Source code in cca_zoo/linear/_mcca.py
fit ¶
Fit the MCCA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
MCCA
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_mcca.py
GCCA ¶
GCCA(latent_dimensions: int = 1, center: bool = True, c: float | list[float] = 0.0, view_weights: list[float] | None = None, eps: float = 1e-06)
Bases: BaseModel
Generalised Canonical Correlation Analysis.
Finds linear projections of multiple (>=2) views that maximise their joint correlation with a shared auxiliary latent vector:
.. math::
\max_{\mathbf{w}_i, T}
\sum_{i=1}^M \mathbf{w}_i^\top X_i^\top T
\text{subject to }
T^\top T = I
The solution is obtained by constructing the weighted projection matrix:
.. math::
Q = \sum_{i=1}^M \mu_i X_i
\bigl((1-c_i) X_i^\top X_i + c_i I\bigr)^{-1} X_i^\top
and computing its top-k eigenvectors :math:V, then recovering the
per-view weights as :math:\mathbf{w}_i = X_i^+ V.
References
Tenenhaus, A., & Tenenhaus, M. (2011). Regularized generalized canonical correlation analysis. Psychometrika, 76(2), 257–284.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means before fitting. Default True. |
True
|
c
|
float | list[float]
|
Ridge regularisation parameter(s) in |
0.0
|
view_weights
|
list[float] | None
|
Per-view weights :math: |
None
|
eps
|
float
|
Regularisation floor for within-view matrices. Default is 1e-6. |
1e-06
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) X3 = rng.standard_normal((50, 6)) model = GCCA(latent_dimensions=2).fit([X1, X2, X3]) scores = model.transform([X1, X2, X3])
Source code in cca_zoo/linear/_gcca.py
fit ¶
Fit the GCCA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
GCCA
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_gcca.py
TCCA ¶
TCCA(latent_dimensions: int = 1, center: bool = True, c: float | list[float] = 0.0, eps: float = 1e-06, random_state: int | None = None)
Bases: BaseModel
Tensor Canonical Correlation Analysis.
Extends CCA to more than two views by exploiting higher-order cross-view correlations via a tensor product structure. The method constructs the order-M cross-moment tensor:
.. math::
\mathcal{M}_{p_1 p_2 \ldots p_M}
= \frac{1}{n} \sum_{i=1}^n
\tilde{x}_{1,i}^{(p_1)}
\tilde{x}_{2,i}^{(p_2)}
\cdots
\tilde{x}_{M,i}^{(p_M)}
where :math:\tilde{X}_j = X_j \Sigma_j^{-1/2} are the whitened views,
and then decomposes :math:\mathcal{M} using PARAFAC to recover the
canonical directions.
References
Kim, T.-K., Wong, S.-F., & Cipolla, R. (2007). Tensor canonical correlation analysis for action classification. CVPR 2007. IEEE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means before fitting. Default True. |
True
|
c
|
float | list[float]
|
Ridge regularisation in |
0.0
|
eps
|
float
|
Regularisation floor for within-view covariance matrices. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility (passed to PARAFAC). |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 5)) X2 = rng.standard_normal((50, 5)) X3 = rng.standard_normal((50, 5)) model = TCCA(latent_dimensions=2, random_state=0).fit([X1, X2, X3]) scores = model.transform([X1, X2, X3])
Source code in cca_zoo/linear/_tcca.py
fit ¶
Fit the TCCA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
TCCA
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_tcca.py
Gradient-descent methods¶
PLS_EY ¶
PLS_EY(latent_dimensions: int = 1, center: bool = True, learning_rate: float = 0.01, max_iter: int = 1000, batch_size: int | None = None, tol: float = 1e-06, random_state: int | None = None)
Bases: BaseModel
Stochastic Eckart-Young PLS for large-scale data.
Optimises the Eckart-Young (EY) objective for PLS by mini-batch Riemannian gradient descent on the Stiefel manifold:
.. math::
\min_{U, V \,:\, U^\top U = I,\, V^\top V = I}
\left\| X_1 U - X_2 V \right\|_F^2
which is equivalent to maximising :math:\mathrm{tr}(U^\top X_1^\top X_2 V)
(the PLS objective). At each step the Euclidean gradient is projected onto
the tangent space of the Stiefel manifold, and the result is retracted back
to the manifold via polar decomposition.
Suitable for high-dimensional or streaming data where forming the full (p × p) cross-covariance matrix is too expensive.
References
Gemp, I., McWilliams, B., Vernade, C., & Graepel, T. (2022). EigenGame Unloaded: When playing games is better than optimizing. ICLR 2022.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
learning_rate
|
float
|
Riemannian gradient step size. Default is 1e-2. |
0.01
|
max_iter
|
int
|
Number of gradient iterations. Default is 1000. |
1000
|
batch_size
|
int | None
|
Mini-batch size. |
None
|
tol
|
float
|
Convergence tolerance on the objective change. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((200, 500)) X2 = rng.standard_normal((200, 400)) model = PLS_EY(latent_dimensions=4, batch_size=64, random_state=0) model.fit([X1, X2])
Source code in cca_zoo/linear/_gradient.py
fit ¶
Fit PLS_EY by Riemannian gradient descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
PLS_EY
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_gradient.py
CCA_EY ¶
CCA_EY(latent_dimensions: int = 1, center: bool = True, c: float | list[float] = 0.0, learning_rate: float = 0.01, max_iter: int = 1000, batch_size: int | None = None, tol: float = 1e-06, random_state: int | None = None)
Bases: PLS_EY
Eckart-Young CCA for large-scale data.
Equivalent to :class:PLS_EY but applies per-view PCA whitening before
the gradient updates, so the resulting objective is the CCA correlation
rather than covariance. This makes the method applicable to views with
very different scales.
The whitening pre-processing is computed once at the start of fit
using the full data, then the gradient updates operate in the whitened
space.
References
Gemp, I., McWilliams, B., Vernade, C., & Graepel, T. (2022). EigenGame Unloaded: When playing games is better than optimizing. ICLR 2022.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
c
|
float | list[float]
|
Ridge regularisation parameter(s) in |
0.0
|
learning_rate
|
float
|
Riemannian gradient step size. Default is 1e-2. |
0.01
|
max_iter
|
int
|
Number of gradient iterations. Default is 1000. |
1000
|
batch_size
|
int | None
|
Mini-batch size. |
None
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((200, 500)) X2 = rng.standard_normal((200, 400)) model = CCA_EY(latent_dimensions=4, c=0.1, batch_size=64, random_state=0) model.fit([X1, X2])
Source code in cca_zoo/linear/_gradient.py
fit ¶
Fit CCA_EY with whitening pre-processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
CCA_EY
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_gradient.py
MCCA_EY ¶
MCCA_EY(latent_dimensions: int = 1, center: bool = True, c: float | list[float] = 0.0, learning_rate: float = 0.01, max_iter: int = 1000, batch_size: int | None = None, tol: float = 1e-06, random_state: int | None = None)
Bases: CCA_EY
Eckart-Young multiview CCA for large-scale data (>=2 views).
Extends :class:CCA_EY to handle more than two views by optimising
the multiview EY loss:
.. math::
\min_{\{W_i\}} \sum_{i \neq j}
\left\| \tilde{X}_i W_i - \tilde{X}_j W_j \right\|_F^2
where :math:\tilde{X}_i are the whitened views, and all weight
matrices are constrained to lie on the Stiefel manifold.
References
Gemp, I., McWilliams, B., Vernade, C., & Graepel, T. (2022). EigenGame Unloaded: When playing games is better than optimizing. ICLR 2022.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
c
|
float | list[float]
|
Ridge regularisation parameter(s) in |
0.0
|
learning_rate
|
float
|
Riemannian gradient step size. Default is 1e-2. |
0.01
|
max_iter
|
int
|
Number of gradient iterations. Default is 1000. |
1000
|
batch_size
|
int | None
|
Mini-batch size. |
None
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((200, 500)) X2 = rng.standard_normal((200, 400)) X3 = rng.standard_normal((200, 300)) model = MCCA_EY(latent_dimensions=4, c=0.1, batch_size=64, random_state=0) model.fit([X1, X2, X3])
Source code in cca_zoo/linear/_gradient.py
fit ¶
Fit MCCA_EY for 2 or more views.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
MCCA_EY
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
ValueError
|
If views have inconsistent numbers of samples. |
Source code in cca_zoo/linear/_gradient.py
Sparse / iterative methods¶
PLS_ALS ¶
PLS_ALS(latent_dimensions: int = 1, center: bool = True, max_iter: int = 500, tol: float = 1e-06, random_state: int | None = None)
Bases: _BaseIterative
Alternating Least Squares variant of Partial Least Squares.
Maximises the sum of cross-view covariances using simple power-iteration updates, without regularisation:
.. math::
\mathbf{w}_i \leftarrow
\frac{X_i^\top \bar{\mathbf{s}}_{\neg i}}
{\|X_i^\top \bar{\mathbf{s}}_{\neg i}\|_2}
where :math:\bar{\mathbf{s}}_{\neg i} is the normalised sum of
projected scores from all views except :math:i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
max_iter
|
int
|
Maximum ALS iterations per dimension. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = PLS_ALS(latent_dimensions=2, random_state=0).fit([X1, X2])
Source code in cca_zoo/linear/_iterative.py
SCCA_PMD ¶
SCCA_PMD(latent_dimensions: int = 1, center: bool = True, tau: float | list[float] = 1.0, max_iter: int = 500, tol: float = 1e-06, random_state: int | None = None)
Bases: _BaseIterative
Sparse CCA via Penalized Matrix Decomposition.
Maximises the cross-view covariance subject to L1 norm constraints on each weight vector:
.. math::
\max_{\mathbf{w}_1, \mathbf{w}_2}
\mathbf{w}_1^\top X_1^\top X_2 \mathbf{w}_2
\text{subject to }
\|\mathbf{w}_i\|_1 \leq \tau_i \sqrt{p_i},\quad
\|\mathbf{w}_i\|_2 = 1
The update for each view uses bisection to find the soft-threshold that satisfies the L1 constraint exactly.
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
tau
|
float | list[float]
|
L1 bound scaling factor(s) in |
1.0
|
max_iter
|
int
|
Maximum ALS iterations. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = SCCA_PMD(tau=0.5, random_state=0).fit([X1, X2])
Source code in cca_zoo/linear/_iterative.py
fit ¶
Fit the SCCA_PMD model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
views
|
list[ArrayLike]
|
List of arrays, each (n_samples, n_features_i). |
required |
y
|
None
|
Ignored. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
SCCA_PMD
|
Fitted estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than 2 views are provided. |
Source code in cca_zoo/linear/_iterative.py
SCCA_ADMM ¶
SCCA_ADMM(latent_dimensions: int = 1, center: bool = True, tau: float | list[float] = 0.1, mu: float = 1.0, max_iter: int = 500, tol: float = 1e-06, random_state: int | None = None)
Bases: _BaseIterative
Sparse CCA via Alternating Direction Method of Multipliers.
Solves the sparse CCA problem using ADMM to enforce both the L1 sparsity constraint on weight vectors and the unit-norm constraint on the projected scores simultaneously.
For view :math:i the ADMM sub-problems are:
- :math:
\mathbf{w}_iupdate — proximal gradient step w.r.t. the data fidelity term. - Auxiliary variable :math:
\mathbf{z}_iupdate — soft thresholding. - Dual variable update.
References
Suo, X., Mineiro, P., & Anandkumar, A. (2017). Sparse canonical correlation analysis. arXiv:1705.10865.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
tau
|
float | list[float]
|
L1 regularisation weight(s). Default is 0.1. |
0.1
|
mu
|
float
|
ADMM penalty parameter (step size). Default is 1.0. |
1.0
|
max_iter
|
int
|
Maximum outer iterations. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = SCCA_ADMM(tau=0.1, random_state=0).fit([X1, X2])
Source code in cca_zoo/linear/_iterative.py
SCCA_IPLS ¶
SCCA_IPLS(latent_dimensions: 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: _BaseIterative
Iterative PLS with elastic net penalty on weight vectors.
Alternates between penalised regression sub-problems. For view :math:i:
.. math::
\hat{\mathbf{w}}_i = \arg\min_{\mathbf{w}}
\frac{1}{2n} \|X_i \mathbf{w} - \bar{\mathbf{s}}_{\neg i}\|_2^2
+ \alpha_i \Bigl(
l_1 \|\mathbf{w}\|_1
+ \tfrac{1-l_1}{2} \|\mathbf{w}\|_2^2
\Bigr)
followed by a normalisation step to enforce unit variance of the score.
References
Mai, Q., & Zhang, X. (2019). An iterative penalized least squares approach to sparse canonical correlation analysis. Biometrics, 75(3), 734–744.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
alpha
|
float | list[float]
|
Elastic net penalty strength(s). Default is 0. |
0.0
|
l1_ratio
|
float | list[float]
|
Ratio of L1 to total penalty. 1 = lasso, 0 = ridge. Default is 1. |
1.0
|
max_iter
|
int
|
Maximum ALS iterations. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = SCCA_IPLS(alpha=0.1, random_state=0).fit([X1, X2])
Source code in cca_zoo/linear/_iterative.py
SCCA_Span ¶
SCCA_Span(latent_dimensions: 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: _BaseIterative
SpanCCA — sparse CCA via truncated power iteration.
Solves sparse CCA by a sparse power iteration where each weight update
retains only the span entries with the largest absolute values.
References
Asteris, M., Khanna, R., Kyrillidis, A., & Dimakis, A. G. (2016). Bilinear approaches for online learning over large feature spaces. NeurIPS 2016. (SpanCCA algorithm).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
span
|
int | list[int] | None
|
Number of non-zero entries to retain per view. Either a single int or a list. Default is None (keep all — no sparsity). |
None
|
max_iter
|
int
|
Maximum ALS iterations. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = SCCA_Span(span=5, random_state=0).fit([X1, X2])
Source code in cca_zoo/linear/_iterative.py
ElasticCCA ¶
ElasticCCA(latent_dimensions: int = 1, center: bool = True, alpha: float | list[float] = 0.0, l1_ratio: float | list[float] = 0.5, max_iter: int = 500, tol: float = 1e-06, random_state: int | None = None)
Bases: _BaseIterative
Elastic net regularised CCA.
Alternates between elastic net regression sub-problems, regressing each view's score against the sum of all other views' scores:
.. math::
\hat{\mathbf{w}}_i = \arg\min_{\mathbf{w}}
\frac{1}{2n} \|X_i \mathbf{w} - \mathbf{s}_{\text{all}}\|_2^2
+ \alpha_i \Bigl(
l_1 \|\mathbf{w}\|_1
+ \tfrac{1 - l_1}{2} \|\mathbf{w}\|_2^2
\Bigr)
where :math:\mathbf{s}_{\text{all}} = \sum_j X_j \mathbf{w}_j / \|\cdot\|.
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).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
alpha
|
float | list[float]
|
Elastic net regularisation strength. Default is 0. |
0.0
|
l1_ratio
|
float | list[float]
|
L1 / total penalty ratio. Default is 0.5. |
0.5
|
max_iter
|
int
|
Maximum ALS iterations. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = ElasticCCA(alpha=0.1, l1_ratio=0.5, random_state=0).fit([X1, X2])
Source code in cca_zoo/linear/_iterative.py
ParkhomenkoCCA ¶
ParkhomenkoCCA(latent_dimensions: int = 1, center: bool = True, tau: float | list[float] = 0.1, max_iter: int = 500, tol: float = 1e-06, random_state: int | None = None)
Bases: _BaseIterative
Sparse CCA via soft-thresholding power iteration (Parkhomenko 2009).
Uses a fixed soft-threshold :math:\tau_i rather than the adaptive
bisection search of :class:SCCA_PMD:
.. math::
\mathbf{w}_i \leftarrow
S_{\tau_i}(X_i^\top \bar{\mathbf{s}}_{\neg i})
where :math:S_\tau is the element-wise soft-threshold operator.
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).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
latent_dimensions
|
int
|
Number of latent dimensions. Default is 1. |
1
|
center
|
bool
|
Whether to subtract column means. Default True. |
True
|
tau
|
float | list[float]
|
Soft-threshold parameter(s). Default is 0.1. |
0.1
|
max_iter
|
int
|
Maximum ALS iterations. Default is 500. |
500
|
tol
|
float
|
Convergence tolerance. Default is 1e-6. |
1e-06
|
random_state
|
int | None
|
Seed for reproducibility. |
None
|
Example
import numpy as np rng = np.random.default_rng(0) X1 = rng.standard_normal((50, 10)) X2 = rng.standard_normal((50, 8)) model = ParkhomenkoCCA(tau=0.1, random_state=0).fit([X1, X2])