nirs4all.operators.models.sklearn.ikpls module
Improved Kernel PLS (IKPLS) regressor for nirs4all.
A sklearn-compatible wrapper for the ikpls package, which provides fast PLS implementations using NumPy or JAX (for GPU/TPU acceleration). IKPLS is significantly faster than sklearn’s PLSRegression, especially for cross-validation.
- class nirs4all.operators.models.sklearn.ikpls.IKPLS(n_components: int = 10, algorithm: int = 1, center: bool = True, scale: bool = True, backend: str = 'numpy')[source]
Bases:
BaseEstimator,RegressorMixinImproved Kernel PLS (IKPLS) regressor.
A sklearn-compatible wrapper for the ikpls package, which provides fast PLS implementations using NumPy or JAX (for GPU/TPU acceleration). IKPLS is significantly faster than sklearn’s PLSRegression, especially for cross-validation.
- Parameters:
n_components (int, default=10) – Number of PLS components to extract.
algorithm (int, default=1) – IKPLS algorithm variant (1 or 2). Algorithm 1 is generally faster.
center (bool, default=True) – Whether to center X and Y before fitting.
scale (bool, default=True) – Whether to scale X and Y before fitting.
backend (str, default='numpy') – Backend to use for computation. Options are: - ‘numpy’: Use NumPy backend (CPU only). - ‘jax’: Use JAX backend (supports GPU/TPU acceleration). JAX backend requires JAX to be installed:
pip install jaxFor GPU support:pip install jax[cuda12]
- n_components_
Actual number of components used (may be less than n_components if limited by data dimensions).
- Type:
- coef_
Regression coefficients.
- Type:
ndarray of shape (n_features, n_targets)
Examples
>>> from nirs4all.operators.models.sklearn.pls import IKPLS >>> import numpy as np >>> X = np.random.randn(100, 50) >>> y = np.random.randn(100) >>> # NumPy backend (default) >>> model = IKPLS(n_components=10) >>> model.fit(X, y) IKPLS(n_components=10) >>> predictions = model.predict(X) >>> # JAX backend for GPU acceleration >>> model_jax = IKPLS(n_components=10, backend='jax')
Notes
Requires the ikpls package:
pip install ikplsFor JAX backend with GPU support, install JAX with CUDA:
pip install jax[cuda12]The JAX backend is end-to-end differentiable, allowing gradient propagation when using PLS as a layer in a deep learning model.
See also
sklearn.cross_decomposition.PLSRegressionStandard sklearn PLS.
References
- fit(X, y)[source]
Fit the IKPLS model.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data.
y (array-like of shape (n_samples,) or (n_samples, n_targets)) – Target values.
- Returns:
self – Fitted estimator.
- Return type:
- Raises:
ImportError – If ikpls package is not installed, or JAX is not available when using ‘jax’ backend.
ValueError – If backend is not ‘numpy’ or ‘jax’.
- predict(X, n_components=None)[source]
Predict using the IKPLS model.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Samples to predict.
n_components (int, optional) – Number of components to use for prediction. If None, uses all fitted components.
- Returns:
y_pred – Predicted values (always returns NumPy arrays).
- Return type:
ndarray of shape (n_samples,) or (n_samples, n_targets)
- set_predict_request(*, n_components: bool | None | str = '$UNCHANGED$') IKPLS
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') IKPLS
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.