Decomposition & Manifold
CCA
Canonical Correlation Analysis using NIPALS Mode B to find maximally correlated projections between two feature views.
CCA (Canonical Correlation Analysis) implements the NIPALS Mode B algorithm to discover pairs of linear projections of X and Y that maximize canonical correlation between the two blocks. It supports transforming X alone or (X, Y) jointly, as well as predicting Y from X. (*≧ω≦*)
Backend Support
- Backend primitives:
E1–E5(dense linear algebra and normal-equation solves),N5(spectral / SVD helpers). - Supported devices:
"auto","cpu","cuda:0","cuda:all","wgpu","tpu:0". Falls back cleanly to multi-threaded Rayon CPU (f64) when an accelerator is unavailable.
Import & Signature
from ochreml.cross_decomposition import CCA
# Or via top-level lazy import:
from ochreml import CCACCA(
n_components: int = 2,
scale: bool = True,
max_iter: int = 500,
tol: float = 1e-6,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n_components | int | 2 | Number of canonical components to keep. |
scale | bool | True | Whether to standardize X and Y using sample standard deviation (ddof=1). |
max_iter | int | 500 | Maximum number of iterations for the NIPALS inner loop. |
tol | float | 1e-6 | Iterative convergence tolerance for NIPALS. |
device | str or None | None | Target compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type / Shape | Description |
|---|---|---|
x_weights_ | ndarray of shape (n_features_in_, n_components) | Left singular / weight vectors of the cross-covariance for X. |
y_weights_ | ndarray of shape (n_targets, n_components) | Right singular / weight vectors of the cross-covariance for Y. |
x_loadings_ | ndarray of shape (n_features_in_, n_components) | Loadings of X on the canonical scores. |
y_loadings_ | ndarray of shape (n_targets, n_components) | Loadings of Y on the canonical scores. |
x_rotations_ | ndarray of shape (n_features_in_, n_components) | Projection matrix mapping centered/scaled X directly to canonical X scores. |
y_rotations_ | ndarray of shape (n_targets, n_components) | Projection matrix mapping centered/scaled Y directly to canonical Y scores. |
coef_ | ndarray of shape (n_targets, n_features_in_) | Linear regression coefficients predicting Y from X. |
intercept_ | ndarray of shape (n_targets,) | Intercept vector for target prediction. |
n_iter_ | list[int] | Number of NIPALS iterations per component. |
n_features_in_ | int | Number of features in X seen during fit. |
device_ | str | Active resolved compute device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, Y) -> CCA | Fit the CCA model on paired datasets (X, Y). |
transform | transform(X, Y=None) -> ndarray | tuple[ndarray, ndarray] | Project X (and optionally Y) into the learned canonical subspace. |
fit_transform | fit_transform(X, Y) -> tuple[ndarray, ndarray] | Fit CCA on (X, Y) and return (X_scores, Y_scores). |
predict | predict(X) -> ndarray | Predict target block Y from input samples X. |
score | score(X, Y) -> float | Return the coefficient of determination (R^2) of predict(X) against Y. |
to | to(device) -> CCA | Move execution target to a new hardware device. |
save | save(path) -> None | Save the fitted model to a binary .dit file. |
load | CCA.load(path, device=None) -> CCA | Load a saved model from a .dit file (also supported via ochreml.load). |
Example
import numpy as np
import ochreml
from ochreml.cross_decomposition import CCA
rng = np.random.default_rng(42)
latent = rng.normal(size=(150, 2))
X = latent @ rng.normal(size=(2, 5)) + rng.normal(scale=0.2, size=(150, 5))
Y = latent @ rng.normal(size=(2, 3)) + rng.normal(scale=0.2, size=(150, 3))
cca = CCA(n_components=2, scale=True, max_iter=500, tol=1e-6)
X_c, Y_c = cca.fit_transform(X, Y)
print("Canonical score shapes:", X_c.shape, Y_c.shape)
print("R^2 score:", round(cca.score(X, Y), 4))
print("Predicted Y shape:", cca.predict(X).shape)
cca.save("cca_model.dit")
loaded = ochreml.load("cca_model.dit", device="cpu")
assert np.allclose(cca.predict(X), loaded.predict(X))