# `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
BackendParity50×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-102.86 ms1.94 ms🏆19.6 ms86.5 ms🏆3.36 ms🏆4.84 ms🏆19.6 ms🏆225.3 ms1.6 s511.6 ms7.2 s36.7 s9.8 s🏆120.4 s🏆
pls4all.cpp.blas+omp≈ +5e-102.85 ms🏆3.14 ms18.7 ms89.7 ms3.54 ms4.85 ms20.1 ms224.3 ms1.6 s🏆508.9 ms🏆7.1 s🏆36.6 s9.9 s136.9 s
pls4all.cpp.omp≈ +6e-103.49 ms2.81 ms16.2 ms88.3 ms3.50 ms4.92 ms24.1 ms226.9 ms1.6 s534.2 ms7.3 s34.8 s10.2 s140.1 s
pls4all.cpp.ref≈ +6e-103.07 ms3.01 ms15.5 ms🏆93.3 ms3.47 ms5.30 ms22.2 ms221.9 ms🏆1.6 s527.0 ms7.1 s34.5 s🏆10.1 s154.2 s
Python · pls4all
pls4all.python✓ bind2.93 ms3.67 ms5.34 ms
pls4all.sklearn✓ bind2.93 ms3.74 ms5.52 ms
R · pls4all
pls4all.R✗ +1e+0011.6 ms8.70 ms14.9 ms
pls4all.R.formula✗ +1e+0018.8 ms11.4 ms13.0 ms
pls4all.R.mdatools✗ +1e+0019.8 ms9.19 ms11.7 ms
pls4all.R.pls✗ +1e+0020.5 ms9.36 ms11.6 ms
MATLAB · pls4all
pls4all.matlab✗ +8e+004.91 ms5.00 ms6.66 ms
pls4all.matlab.classdef✗ +8e+008.11 ms5.52 ms8.60 ms
R · external
📐ref.r_kernlab_plssource35.7 ms42.2 ms38.5 ms
::: :::{tab-item} 3 threads :sync: threads-3
BackendParity50×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-153.37 ms🏆
pls4all.cpp.blas+omp~ shape 5e-153.61 ms
pls4all.cpp.omp~ shape 3e-153.43 ms
pls4all.cpp.ref~ shape 3e-153.44 ms
Python · pls4all
pls4all.python✓ bind3.40 ms
pls4all.sklearn✓ bind3.75 ms
R · pls4all
pls4all.R✗ +1e+008.00 ms
pls4all.R.formula✗ +1e+009.22 ms
pls4all.R.mdatools✗ +1e+009.66 ms
pls4all.R.pls✗ +1e+009.72 ms
MATLAB · pls4all
pls4all.matlab✗ +8e+004.69 ms
pls4all.matlab.classdef✗ +8e+006.24 ms
R · external
📐ref.r_kernlab_plssource37.9 ms
::: :::{tab-item} 10 threads :sync: threads-10
BackendParity50×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-153.06 ms🏆
pls4all.cpp.blas+omp~ shape 5e-153.11 ms
pls4all.cpp.omp~ shape 3e-153.18 ms
pls4all.cpp.ref~ shape 3e-153.21 ms
Python · pls4all
pls4all.python✓ 1e-143.08 ms
pls4all.sklearn✓ 1e-143.25 ms
R · pls4all
pls4all.R✗ +1e+006.39 ms
pls4all.R.formula✗ +1e+007.45 ms
pls4all.R.mdatools✗ +1e+007.42 ms
pls4all.R.pls✗ +1e+007.49 ms
MATLAB · pls4all
pls4all.matlab✗ +8e+004.25 ms
pls4all.matlab.classdef✗ +8e+004.66 ms
R · external
📐ref.r_kernlab_plssource41.4 ms
::: :::: --- _See also_: [benchmark overview](../benchmarks/overview.md) · [methods index](index.md) · [interactive dashboard](../landing/dashboard.md)