# `kernel_pls_rbf` — Kernel PLS (Rosipal & Trejo 2001)
_Group_: **Nonlinear / local** · _Registry tolerance_: `2.0`
## Description
Non-linear kernel PLS (RBF kernel)
From the `pls4all.sklearn.KernelPLSRegression` docstring:
> Non-linear kernel PLS (Rosipal & Trejo 2001).
> **Registry note** — R `kernlab::kernelMatrix` (RBF/poly/sigmoid) + `pls::plsr` on the centered kernel matrix is the Rosipal-Trejo (2001) reference. pls4all's deflation ordering differs from the kernel-PLS-2 of Rosipal & Trejo so parity is qualitative.
### Parameters
| Name | Type | Default | Notes |
|------|------|---------|-------|
| `n_components` | `int` | `2` | Number of latent components extracted (k). |
| `kernel_type` | `int` | `1` | Kernel family: 0=linear, 1=RBF, 2=polynomial, 3=sigmoid. |
| `gamma` | `float` | `0.0` | RBF kernel bandwidth γ (with K(x, x') = exp(-γ‖x − x'‖²)). |
| `coef0` | `float` | `1.0` | Independent term in polynomial / sigmoid kernels. |
| `degree` | `int` | `3` | Degree of the polynomial kernel (ignored otherwise). |
## Explanations
### Bibliographic source
Rosipal, R. & Trejo, L. J. (2001). *Kernel partial least squares regression in reproducing kernel Hilbert space*. Journal of Machine Learning Research 2, 97–123.
### Mathematical principle
Kernel PLS runs the NIPALS PLS procedure entirely in the feature space of a Mercer kernel $k(\mathbf{x}, \mathbf{x}') = \langle \phi(\mathbf{x}), \phi(\mathbf{x}') \rangle$ without ever forming $\phi$ explicitly. The kernel matrix $\mathbf{K}_{ij} = k(\mathbf{x}_i, \mathbf{x}_j) \in \mathbb{R}^{n \times n}$ replaces $\mathbf{X}\mathbf{X}^{\top}$ in the NIPALS recursion and the score matrix is built directly from $\mathbf{K}$.
The RBF kernel $k(\mathbf{x}, \mathbf{x}') = \exp(-\gamma \|\mathbf{x} - \mathbf{x}'\|^2)$ is the standard choice for non-linear PLS: it captures smooth non-linear relationships between $\mathbf{X}$ and $y$ at the cost of a single bandwidth hyperparameter $\gamma$. Other kernels (polynomial, sigmoid) are available via the `kernel_type` enum.
Memory scales as $O(n^2)$ which is the binding constraint for kernel PLS on spectroscopy datasets; subsampling (Nyström) or random Fourier features are the standard scale-up strategies but are not currently exposed.
### Implementation
`n4m_kernel_pls_fit`. Predict-on-new-X is currently marked in-sample-only in the Python `sklearn` wrapper because the C ABI does not yet export the kernel-centring constants required to handle a fresh test point. The tier-1 entry point will refit on (X_train ∪ X_test) on demand.
MATLAB header (`bindings/matlab/+pls4all/KernelPlsRegression.m`):
```text
pls4all.KernelPlsRegression Non-linear kernel PLS (Rosipal & Trejo 2001).
**In-sample only**: the C ABI exports `alpha` and `y_mean` but NOT the
kernel-centering state needed to compute K(X_new, X_train) at predict
time. predict(X) returns the stored predictions when X matches the
training matrix (content equality), otherwise raises an error.
```
### Usage
Every pls4all binding tab dispatches into the same C kernel; the external libraries listed at the bottom of the page are the parity references registered in `benchmarks.parity_timing.registry`. Switch tabs to read the same fit in your language. The R package now ships drop-in-compatible facades for the CRAN `pls` package (`plsr`, `pcr`, `mvr`) and for the `mdatools::pls(x, y, ...)` matrix idiom — those tabs appear only on the methods that have a meaningful equivalence.
**pls4all bindings**
::::{tab-set}
:class: pls4all-bindings
:::{tab-item} C ABI · libn4m
:sync: c
:class-label: lang-c
```c
/* C ABI — libn4m */
n4m_context_t* ctx = n4m_context_create();
n4m_config_t* cfg = n4m_config_create();
n4m_method_result_t* res = NULL;
n4m_kernel_pls_rbf_fit(ctx, cfg, &x_view, &y_view, /* hyperparams */, &res);
/* … read coefficients / mask / scores via */
/* n4m_method_result_get_double_matrix / vector / scalar … */
n4m_method_result_destroy(res);
n4m_config_destroy(cfg);
n4m_context_destroy(ctx);
```
:::
:::{tab-item} Python · pls4all (raw)
:sync: python-raw
:class-label: lang-python
```python
import pls4all
from pls4all._methods import kernel_pls_rbf_fit
with pls4all.Context() as ctx, pls4all.Config() as cfg:
res = kernel_pls_rbf_fit(ctx, cfg, X, y, n_components=4)
# then: res.matrix("predictions"), res.matrix("coefficients"),
# res.vector("mask"), res.scalar("intercept"), …
```
:::
:::{tab-item} Python · pls4all.sklearn
:sync: python-sklearn
:class-label: lang-python
```python
from pls4all.sklearn import KernelPLSRegression
mdl = KernelPLSRegression(n_components=2, kernel_type=1, gamma=0.0, coef0=1.0, degree=3)
mdl.fit(X, y)
y_hat = mdl.predict(X_test)
```
:::
:::{tab-item} R · pls4all_method()
:sync: r-dispatcher
:class-label: lang-r
```r
library(pls4all)
# Unified low-level dispatcher (May 2026 R cleanup):
res <- pls4all_method("kernel_pls_rbf", X, y,
n_components = 4L, params = list(kernel_type = 1L, gamma = 0.02, coef0 = 1.0, degree = 3L))
# res is a named list with MethodResult arrays/scalars.
# selected_indices / top_k_intervals are 1-based.
```
:::
:::{tab-item} MATLAB · pls4all (MEX)
:sync: matlab-mex
:class-label: lang-matlab
```matlab
res = pls4all.kernel_pls(X, y, 4);
% see header of bindings/matlab/+pls4all/kernel_pls.m for full
% parameter surface:
% res = kernel_pls(X, Y, n_components, kernel_type, gamma, coef0, degree)
yhat = predict(res, Xtest);
```
:::
:::{tab-item} MATLAB · pls4all (classdef)
:sync: matlab-classdef
:class-label: lang-matlab
```matlab
mdl = pls4all.fit("kernel_pls_rbf", X, y, "NumComponents", 4);
yhat = predict(mdl, Xtest);
```
:::
::::
**Registry parity references** 📐
:::{card}
:class-card: external-refs
- 📐 **`ref.r_kernlab_pls`** (R · r) — `kernlab+pls` 0.9.33+2.8.5 · qualitative (rmse_rel ≤ 2e+00) — R `kernlab::kernelMatrix` (RBF/poly/sigmoid) + `pls::plsr` on the centered kernel matrix is a Rosipal-Trejo-style kernel PLS reference. pls4all uses a different deflation order so the parity is qualitative.
:::
### Benchmarks
Adaptive wall-clock per cell measured against [`full_matrix.csv`](../benchmarks/overview.md). Only backends that implement this method are listed; libraries without the method are omitted.
**Verdict** · ✓ ref / ≈ ref / ~ shape mark a reference-gate pass at strict / relaxed / qualitative tolerance · ✓ bind = pls4all binding agrees with the C++ baseline · ✗ divergent · ⚠ error · — not run. The fastest backend per column is marked 🏆.
**Reference gate**: qualitative — shape/smoke comparison only. The external library and pls4all do not produce numerically equivalent output for this method (see the MethodSpec notes); the `rmse_rel_tol ≤ 2e+00` budget is set wide on purpose. Treat ~ shape as *“we ran both, both finished”*, not as numerical agreement.
Rows tagged with **📐** are the canonical parity references for this method (declared in [`parity_timing.registry`](../benchmarks/methodology.md)). C++ and external rows show reference parity; pls4all language bindings show binding parity against the C++ backend. Hover the icon for role and tolerance band.
::::{tab-set}
:class: parity-tabs
:::{tab-item} 1 thread
:sync: threads-1
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×50 (ms) | 250×50 (ms) | 500×50 (ms) | 500×500 (ms) | 500×2500 (ms) | 2500×50 (ms) | 2500×500 (ms) | 2500×2500 (ms) | 10000×50 (ms) | 10000×500 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas | ≈ +5e-10 | 2.86 ms | 1.94 ms🏆 | 19.6 ms | 86.5 ms🏆 | 3.36 ms🏆 | 4.84 ms🏆 | 19.6 ms🏆 | 225.3 ms | 1.6 s | 511.6 ms | 7.2 s | 36.7 s | 9.8 s🏆 | 120.4 s🏆 |
pls4all.cpp.blas+omp | ≈ +5e-10 | 2.85 ms🏆 | 3.14 ms | 18.7 ms | 89.7 ms | 3.54 ms | 4.85 ms | 20.1 ms | 224.3 ms | 1.6 s🏆 | 508.9 ms🏆 | 7.1 s🏆 | 36.6 s | 9.9 s | 136.9 s |
pls4all.cpp.omp | ≈ +6e-10 | 3.49 ms | 2.81 ms | 16.2 ms | 88.3 ms | 3.50 ms | 4.92 ms | 24.1 ms | 226.9 ms | 1.6 s | 534.2 ms | 7.3 s | 34.8 s | 10.2 s | 140.1 s |
pls4all.cpp.ref | ≈ +6e-10 | 3.07 ms | 3.01 ms | 15.5 ms🏆 | 93.3 ms | 3.47 ms | 5.30 ms | 22.2 ms | 221.9 ms🏆 | 1.6 s | 527.0 ms | 7.1 s | 34.5 s🏆 | 10.1 s | 154.2 s |
| Python · pls4all |
pls4all.python | ✓ bind | 2.93 ms | — | — | — | 3.67 ms | 5.34 ms | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ bind | 2.93 ms | — | — | — | 3.74 ms | 5.52 ms | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e+00 | 11.6 ms | — | — | — | 8.70 ms | 14.9 ms | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e+00 | 18.8 ms | — | — | — | 11.4 ms | 13.0 ms | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e+00 | 19.8 ms | — | — | — | 9.19 ms | 11.7 ms | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e+00 | 20.5 ms | — | — | — | 9.36 ms | 11.6 ms | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +8e+00 | 4.91 ms | — | — | — | 5.00 ms | 6.66 ms | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +8e+00 | 8.11 ms | — | — | — | 5.52 ms | 8.60 ms | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_kernlab_pls | source | 35.7 ms | — | — | — | 42.2 ms | 38.5 ms | — | — | — | — | — | — | — | — |
:::
:::{tab-item} 3 threads
:sync: threads-3
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×50 (ms) | 250×50 (ms) | 500×50 (ms) | 500×500 (ms) | 500×2500 (ms) | 2500×50 (ms) | 2500×500 (ms) | 2500×2500 (ms) | 10000×50 (ms) | 10000×500 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas | ~ shape 5e-15 | — | — | — | — | 3.37 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ~ shape 5e-15 | — | — | — | — | 3.61 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ~ shape 3e-15 | — | — | — | — | 3.43 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ~ shape 3e-15 | — | — | — | — | 3.44 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ bind | — | — | — | — | 3.40 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ bind | — | — | — | — | 3.75 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e+00 | — | — | — | — | 8.00 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e+00 | — | — | — | — | 9.22 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e+00 | — | — | — | — | 9.66 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e+00 | — | — | — | — | 9.72 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +8e+00 | — | — | — | — | 4.69 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +8e+00 | — | — | — | — | 6.24 ms | — | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_kernlab_pls | source | — | — | — | — | 37.9 ms | — | — | — | — | — | — | — | — | — |
:::
:::{tab-item} 10 threads
:sync: threads-10
| Backend | Parity | 50×250 (ms) | 100×50 (ms) | 100×500 (ms) | 100×2500 (ms) | 200×50 (ms) | 250×50 (ms) | 500×50 (ms) | 500×500 (ms) | 500×2500 (ms) | 2500×50 (ms) | 2500×500 (ms) | 2500×2500 (ms) | 10000×50 (ms) | 10000×500 (ms) |
| C++ native · libn4m |
pls4all.cpp.blas | ~ shape 5e-15 | — | — | — | — | 3.06 ms🏆 | — | — | — | — | — | — | — | — | — |
pls4all.cpp.blas+omp | ~ shape 5e-15 | — | — | — | — | 3.11 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.omp | ~ shape 3e-15 | — | — | — | — | 3.18 ms | — | — | — | — | — | — | — | — | — |
pls4all.cpp.ref | ~ shape 3e-15 | — | — | — | — | 3.21 ms | — | — | — | — | — | — | — | — | — |
| Python · pls4all |
pls4all.python | ✓ 1e-14 | — | — | — | — | 3.08 ms | — | — | — | — | — | — | — | — | — |
pls4all.sklearn | ✓ 1e-14 | — | — | — | — | 3.25 ms | — | — | — | — | — | — | — | — | — |
| R · pls4all |
pls4all.R | ✗ +1e+00 | — | — | — | — | 6.39 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.formula | ✗ +1e+00 | — | — | — | — | 7.45 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.mdatools | ✗ +1e+00 | — | — | — | — | 7.42 ms | — | — | — | — | — | — | — | — | — |
pls4all.R.pls | ✗ +1e+00 | — | — | — | — | 7.49 ms | — | — | — | — | — | — | — | — | — |
| MATLAB · pls4all |
pls4all.matlab | ✗ +8e+00 | — | — | — | — | 4.25 ms | — | — | — | — | — | — | — | — | — |
pls4all.matlab.classdef | ✗ +8e+00 | — | — | — | — | 4.66 ms | — | — | — | — | — | — | — | — | — |
| R · external |
📐ref.r_kernlab_pls | source | — | — | — | — | 41.4 ms | — | — | — | — | — | — | — | — | — |
:::
::::
---
_See also_: [benchmark overview](../benchmarks/overview.md) · [methods index](index.md) · [interactive dashboard](../landing/dashboard.md)