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, RegressorMixin

Improved 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 jax For GPU support: pip install jax[cuda12]

n_features_in_

Number of features seen during fit.

Type:

int

n_components_

Actual number of components used (may be less than n_components if limited by data dimensions).

Type:

int

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 ikpls

For 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.PLSRegression

Standard 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:

IKPLS

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’.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

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_params(**params)[source]

Set the parameters of this estimator.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

IKPLS

set_predict_request(*, n_components: bool | None | str = '$UNCHANGED$') IKPLS

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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.

Parameters:

n_components (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for n_components parameter in predict.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') IKPLS

Configure whether metadata should be requested to be passed to the score method.

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 (see sklearn.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 to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • 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.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object