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 PLSRegressionPLSRegression(
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 latent PLS components to keep. |
scale | bool | True | Whether to scale 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 | Convergence tolerance for the iterative NIPALS loop. |
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 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_ | int | Number of features in X seen during fit. |
device_ | str | Active resolved compute device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, Y) -> PLSRegression | Fit the PLS model on training features X and targets Y (1D or 2D). |
transform | transform(X, Y=None) -> ndarray | tuple[ndarray, ndarray] | Project X (and optionally Y) into the learned latent subspace. |
fit_transform | fit_transform(X, Y) -> tuple[ndarray, ndarray] | Fit the model on (X, Y) and return (X_scores, Y_scores). |
predict | predict(X) -> ndarray | Predict target values for X (returns 1D if fitted on 1D Y, 2D otherwise). |
inverse_transform | inverse_transform(X) -> ndarray | Transform latent X scores back to the original feature space. |
score | score(X, Y) -> float | Return the coefficient of determination (R^2) on (X, Y). |
to | to(device) -> PLSRegression | Move execution target to a new hardware device. |
save | save(path) -> None | Save the fitted model to a binary .dit file. |
load | PLSRegression.load(path, device=None) -> PLSRegression | 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 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))