Skip to Content
OchreML
Decomposition & Manifold

PLSRegression

Partial Least Squares regression via NIPALS for single-target and multi-target collinear feature matrices.

PLSRegression implements Partial Least Squares regression using the NIPALS algorithm for both 1D (single-target) and 2D (multi-target) responses. By projecting X and Y into a shared latent space that maximizes covariance, PLSRegression handles strongly collinear or high-dimensional predictors where ordinary least squares becomes ill-conditioned. (^▽^)

Backend Support

  • Backend primitives: E1–E3 (dense matrix multiplication, A^T @ B, and matrix-vector products).
  • 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 PLSRegression
# Or via top-level lazy import:
from ochreml import PLSRegression
PLSRegression(
    n_components: int = 2,
    scale: bool = True,
    max_iter: int = 500,
    tol: float = 1e-6,
    device: Optional[str] = None,
)

Parameters

ParameterTypeDefaultDescription
n_componentsint2Number of latent PLS components to keep.
scaleboolTrueWhether to scale X and Y using sample standard deviation (ddof=1).
max_iterint500Maximum number of iterations for the NIPALS inner loop.
tolfloat1e-6Convergence tolerance for the iterative NIPALS loop.
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 weight vectors of the cross-covariance matrix.
y_weights_ndarray of shape (n_targets, n_components)Right weight vectors of the cross-covariance matrix.
x_loadings_ndarray of shape (n_features_in_, n_components)Loadings of X on the latent scores.
y_loadings_ndarray of shape (n_targets, n_components)Loadings of Y on the latent scores.
x_rotations_ndarray of shape (n_features_in_, n_components)Projection matrix mapping X into latent space.
y_rotations_ndarray of shape (n_targets, n_components)Projection matrix mapping Y into latent space.
coef_ndarray of shape (n_targets, n_features_in_)Regression coefficients mapping X to Y.
intercept_ndarray of shape (n_targets,)Regression intercept vector.
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) -> PLSRegressionFit the PLS model on training features X and targets Y (1D or 2D).
transformtransform(X, Y=None) -> ndarray | tuple[ndarray, ndarray]Project X (and optionally Y) into the learned latent subspace.
fit_transformfit_transform(X, Y) -> tuple[ndarray, ndarray]Fit the model on (X, Y) and return (X_scores, Y_scores).
predictpredict(X) -> ndarrayPredict target values for X (returns 1D if fitted on 1D Y, 2D otherwise).
inverse_transforminverse_transform(X) -> ndarrayTransform latent X scores back to the original feature space.
scorescore(X, Y) -> floatReturn the coefficient of determination (R^2) on (X, Y).
toto(device) -> PLSRegressionMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model to a binary .dit file.
loadPLSRegression.load(path, device=None) -> PLSRegressionLoad a saved model from a .dit file (also supported via ochreml.load).

Example

import numpy as np
import ochreml
from ochreml.cross_decomposition import PLSRegression

rng = np.random.default_rng(42)
latent = rng.normal(size=(180, 3))
X = latent @ rng.normal(size=(3, 12)) + rng.normal(scale=0.1, size=(180, 12))
Y = latent @ np.array([[2.0, -1.0], [0.5, 1.5], [-1.2, 0.8]]) + rng.normal(scale=0.1, size=(180, 2))

pls = PLSRegression(n_components=3, scale=True)
pls.fit(X, Y)

Y_pred = pls.predict(X)
X_scores = pls.transform(X)
X_recon = pls.inverse_transform(X_scores)

print("R^2 score:", round(pls.score(X, Y), 4))
print("Prediction shape:", Y_pred.shape, "Reconstruction shape:", X_recon.shape)

pls.save("pls_model.dit")
loaded = ochreml.load("pls_model.dit", device="cpu")
assert np.allclose(Y_pred, loaded.predict(X))

On this page