Skip to Content
OchreML
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 CCA
CCA(
    n_components: int = 2,
    scale: bool = True,
    max_iter: int = 500,
    tol: float = 1e-6,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_componentsint2Number of canonical components to keep.
scaleboolTrueWhether to standardize X and Y using sample standard deviation (ddof=1).
max_iterint500Maximum number of iterations for the NIPALS inner loop.
tolfloat1e-6Iterative convergence tolerance for NIPALS.
devicestr or NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeType / ShapeDescription
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_intNumber of features in X seen during fit.
device_strActive resolved compute device string.

Methods

MethodSignatureDescription
fitfit(X, Y) -> CCAFit the CCA model on paired datasets (X, Y).
transformtransform(X, Y=None) -> ndarray | tuple[ndarray, ndarray]Project X (and optionally Y) into the learned canonical subspace.
fit_transformfit_transform(X, Y) -> tuple[ndarray, ndarray]Fit CCA on (X, Y) and return (X_scores, Y_scores).
predictpredict(X) -> ndarrayPredict target block Y from input samples X.
scorescore(X, Y) -> floatReturn the coefficient of determination (R^2) of predict(X) against Y.
toto(device) -> CCAMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model to a binary .dit file.
loadCCA.load(path, device=None) -> CCALoad 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))

On this page