Improved Kernel Partial Least Squares (IKPLS) and Fast Cross-Validation
The ikpls software package provides fast and efficient tools for PLS (Partial Least Squares) modeling. This package is designed to help researchers and practitioners handle PLS modeling faster than previously possible - particularly on large datasets.
Citation
If you use the ikpls software package for your work, please cite this Journal of Open Source Software article. If you use the fast cross-validation algorithm implemented in ikpls.fast_cross_validation.numpy_ikpls, please also cite this arXiv preprint.
Unlock the Power of Fast and Stable Partial Least Squares Modeling with IKPLS
Dive into cutting-edge Python implementations of the IKPLS (Improved Kernel Partial Least Squares) Algorithms #1 and #2 [1] for CPUs, GPUs, and TPUs. IKPLS is both fast [2] and numerically stable [3] making it optimal for PLS modeling.
- Use our NumPy [4] based CPU implementations for seamless integration with scikit-learn's [5] ecosystem of machine learning algorithms and pipelines. As the implementations subclass scikit-learn's BaseEstimator, they can be used with scikit-learn's cross_validate.
- Use our JAX [6] implementations on CPUs or leverage powerful GPUs and TPUs for PLS modelling. Our JAX implementations are end-to-end differentaible allowing gradient propagation when using PLS as a layer in a deep learning model.
- Use our combination of IKPLS with Engstrøm's unbelievably fast cross-validation algorithm [7] to quickly determine the optimal combination of preprocessing and number of PLS components.
The documentation is available at https://ikpls.readthedocs.io/en/latest/; examples can be found at https://github.com/Sm00thix/IKPLS/tree/main/examples.
Fast Cross-Validation
In addition to the standalone IKPLS implementations, this package
contains an implementation of IKPLS combined with the novel, fast cross-validation
by Engstrøm [7]. The fast cross-validation algorithm
benefit both IKPLS Algorithms and especially Algorithm #2. The fast
cross-validation algorithm is mathematically equivalent to the
classical cross-validation algorithm. Still, it is much quicker.
The fast cross-validation algorithm correctly handles (column-wise)
centering and scaling of the X and Y input matrices using training set means and
standard deviations to avoid data leakage from the validation set. This centering
and scaling can be enabled or disabled independently from eachother and for X and Y
by setting the parameters center_X, center_Y, scale_X, and scale_Y, respectively.
In addition to correctly handling (column-wise) centering and scaling,
the fast cross-validation algorithm correctly handles row-wise preprocessing
that operates independently on each sample such as (row-wise) centering and scaling
of the X and Y input matrices, convolution, or other preprocessing. Row-wise
preprocessing can safely be applied before passing the data to the fast
cross-validation algorithm.
Prerequisites
The JAX implementations support running on both CPU, GPU, and TPU.
-
To enable NVIDIA GPU execution, install JAX and CUDA with:
pip3 install -U "jax[cuda12]"
-
To enable Google Cloud TPU execution, install JAX with:
pip3 install -U "jax[tpu]" -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
These are typical installation instructions that will be what most users are looking for. For customized installations, follow the instructions from the JAX Installation Guide.
To ensure that JAX implementations use Float64, set the environment variable JAX_ENABLE_X64=True as per the Current Gotchas.
Installation
-
Install the package for Python3 using the following command:
pip3 install ikpls
-
Now you can import the NumPy and JAX implementations with:
from ikpls.numpy_ikpls import PLS as NpPLS from ikpls.jax_ikpls_alg_1 import PLS as JAXPLS_Alg_1 from ikpls.jax_ikpls_alg_2 import PLS as JAXPLS_Alg_2 from ikpls.fast_cross_validation.numpy_ikpls import PLS as NpPLS_FastCV
Quick Start
Use the ikpls package for PLS modeling
import numpy as np from ikpls.numpy_ikpls import PLS N = 100 # Number of samples. K = 50 # Number of features. M = 10 # Number of targets. A = 20 # Number of latent variables (PLS components). # Using float64 is important for numerical stability. X = np.random.uniform(size=(N, K)).astype(np.float64) Y = np.random.uniform(size=(N, M)).astype(np.float64) # The other PLS algorithms and implementations have the same interface for fit() # and predict(). The fast cross-validation implementation with IKPLS has a # different interface. np_ikpls_alg_1 = PLS(algorithm=1) np_ikpls_alg_1.fit(X, Y, A) # Has shape (A, N, M) = (20, 100, 10). Contains a prediction for all possible # numbers of components up to and including A. y_pred = np_ikpls_alg_1.predict(X) # Has shape (N, M) = (100, 10). y_pred_20_components = np_ikpls_alg_1.predict(X, n_components=20) (y_pred_20_components == y_pred[19]).all() # True # The internal model parameters can be accessed as follows: # Regression coefficients tensor of shape (A, K, M) = (20, 50, 10). np_ikpls_alg_1.B # X weights matrix of shape (K, A) = (50, 20). np_ikpls_alg_1.W # X loadings matrix of shape (K, A) = (50, 20). np_ikpls_alg_1.P # Y loadings matrix of shape (M, A) = (10, 20). np_ikpls_alg_1.Q # X rotations matrix of shape (K, A) = (50, 20). np_ikpls_alg_1.R # X scores matrix of shape (N, A) = (100, 20). # This is only computed for IKPLS Algorithm #1. np_ikpls_alg_1.T
Examples
In examples, you will find:
- Fit and Predict with NumPy.
- Fit and Predict with JAX.
- Cross-validate with NumPy.
- Cross-validate with NumPy and fast cross-validation.
- Cross-validate with JAX.
- Compute the gradient of a preprocessing convolution filter with respect to the RMSE between the target value and the value predicted by PLS after fitting with JAX.
Contribute
To contribute, please read the Contribution Guidelines.
References
- Dayal, B. S., & MacGregor, J. F. (1997). Improved PLS algorithms. Journal of Chemometrics, 11(1), 73-85.
- Alin, A. (2009). Comparison of PLS algorithms when the number of objects is much larger than the number of variables. Statistical Papers, 50, 711-720.
- Andersson, M. (2009). A comparison of nine PLS1 algorithms. Journal of Chemometrics, 23(10), 518-529.
- NumPy
- scikit-learn
- JAX
- Engstrøm, O.-C. G. (2024). Shortcutting Cross-Validation: Efficiently Deriving Column-Wise Centered and Scaled Training Set $\mathbf{X}^\mathbf{T}\mathbf{X}$ and $\mathbf{X}^\mathbf{T}\mathbf{Y}$ Without Full Recomputation of Matrix Products or Statistical Moments
Metadata
Release files for ikpls 1.2.5.post11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ikpls-1.2.5.post11.tar.gz | 26.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ikpls-1.2.5.post11-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 58.7 kB
Release files / ikpls-1.2.5.post11.tar.gz
| Download URL | ikpls-1.2.5.post11.tar.gz |
|---|---|
| Size | 26.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
faaf284b52b2e890a4fd88994bff18c9da59ebd402d204b0e6009350cd5d65a3
|
|
BLAKE2b-256 checksum How to use checksums |
5f9143170c5b77b6f536896abc73c24f9650959e9923e84b70fdd3cd32909804
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 31, 2025.
Transparency logRelease files / ikpls-1.2.5.post11-py3-none-any.whl
| Download URL | ikpls-1.2.5.post11-py3-none-any.whl |
|---|---|
| Size | 32.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8afceea7341ed8d80a4e0c15202732c9a45671eb1bd7dbc67ae564598e171856
|
|
BLAKE2b-256 checksum How to use checksums |
f06406c5cc6ce6e2aba0066c24fe7a1574d845ff165ecd4276fc217befb0213e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 31, 2025.
Transparency log