Fast computation of possibly weighted and possibly centered/scaled training set kernel matrices in a cross-validation setting.
Project description
CVMatrix
The cvmatrix package implements the fast cross-validation algorithms by Engstrøm and Jensen [1] for computation of training set $\mathbf{X}^{\mathbf{T}}\mathbf{X}$ and $\mathbf{X}^{\mathbf{T}}\mathbf{Y}$ in a cross-validation setting. In addition to correctly handling arbitrary row-wise pre-processing, the algorithms allow for and efficiently and correctly handle any combination of column-wise centering and scaling of X and Y based on training set statistical moments.
For an implementation of the fast cross-validation algorithms combined with Improved Kernel Partial Least Squares [2], see the Python package ikpls by Engstrøm et al. [3].
NEW IN 2.0.0: Weighted CVMatrix
The cvmatrix software package now also features weigthed matrix produts $\mathbf{X}^{\mathbf{T}}\mathbf{W}\mathbf{Y}$ without increasing time or space complexity compared to the unweighted case. This is due to a generalization of the algorithms by Engstrøm and Jensen [1]. A new article formally describing the generalization is to be announced.
Installation
-
Install the package for Python3 using the following command:
pip3 install cvmatrix
-
Now you can import the class implementing all the algorithms with:
from cvmatrix.cvmatrix import CVMatrix
Quick Start
Use the cvmatrix package for fast computation of training set kernel matrices
import numpy as np from cvmatrix.cvmatrix import CVMatrix N = 100 # Number of samples. K = 50 # Number of features. M = 10 # Number of targets. X = np.random.uniform(size=(N, K)) # Random X data Y = np.random.uniform(size=(N, M)) # Random Y data folds = np.arange(100) % 5 # 5-fold cross-validation # Weights must be non-negative and the sum of weights for any training partition must # be greater than zero. weights = np.random.uniform(size=(N,)) + 0.1 # Instantiate CVMatrix cvm = CVMatrix( folds=folds, center_X=True, # Cemter around the weighted mean of X. center_Y=True, # Cemter around the weighted mean of Y. scale_X=True, # Scale by the weighted standard deviation of X. scale_Y=True, # Scale by the weighted standard deviation of Y. ) # Fit on X and Y cvm.fit(X=X, Y=Y, weights=weights) # Compute training set XTWX and/or XTWY for each fold for fold in cvm.folds_dict.keys(): # Get both XTWX and XTWY training_XTX, training_XTY = cvm.training_XTX_XTY(fold) # Get only XTWX training_XTX = cvm.training_XTX(fold) # Get only XTWY training_XTY = cvm.training_XTY(fold)
Examples
In examples, you will find:
Benchmarks
In benchmarks, we have benchmarked cross-validation of the fast algorithms in cvmatrix against the baseline algorithms implemented in NaiveCVMatrix.
Left: Benchmarking cross-validation with the CVMatrix implementation versus the baseline implementation using three common combinations of (column-wise) centering and scaling. Right: Benchmarking cross-validation with the CVMatrix implementation for all possible combinations of (column-wise) centering and scaling. Here, most of the graphs lie on top of eachother. In general, no preprocessing is faster than centering which, in turn, is faster than scaling.
Contribute
To contribute, please read the Contribution Guidelines.
References
- Engstrøm, O.-C. G. and Jensen, M. H. (2025). Fast partition-based cross-validation with centering and scaling for $\mathbf{X}^\mathbf{T}\mathbf{X}$ and $\mathbf{X}^\mathbf{T}\mathbf{Y}$. Journal of Chemometrics, 39(3).
- Dayal, B. S. and MacGregor, J. F. (1997). Improved PLS algorithms. Journal of Chemometrics, 11(1), 73-85.
- Engstrøm, O.-C. G. and Dreier, E. S. and Jespersen, B. M. and Pedersen, K. S. (2024). IKPLS: Improved Kernel Partial Least Squares and Fast Cross-Validation Algorithms for Python with CPU and GPU Implementations Using NumPy and JAX. Journal of Open Source Software, 9(99).
Funding
- Up until May 31st 2025, this work has been carried out as part of an industrial Ph. D. project receiving funding from FOSS Analytical A/S and The Innovation Fund Denmark. Grant number 1044-00108B.
- From June 1st 2025 and onward, this work is sponsored by FOSS Analytical A/S.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cvmatrix-2.0.1.tar.gz.
File metadata
- Download URL: cvmatrix-2.0.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4554255462b778c304515c92502c6a33788ad831b7fde91635eb98fc432ca75a
|
|
| MD5 |
e4f8670accfce5808d68c4517250dcf1
|
|
| BLAKE2b-256 |
c8dd9830b311bf826fc30a84a178297eaf78adfde409a1188f5e37af03563836
|
Provenance
The following attestation bundles were made for cvmatrix-2.0.1.tar.gz:
Publisher:
package_workflow.yml on sm00thix/cvmatrix
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cvmatrix-2.0.1.tar.gz -
Subject digest:
4554255462b778c304515c92502c6a33788ad831b7fde91635eb98fc432ca75a - Sigstore transparency entry: 259699827
- Sigstore integration time:
-
Permalink:
sm00thix/cvmatrix@39e01703e63186e35f42847d6e74ec9ca805fc25 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sm00thix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package_workflow.yml@39e01703e63186e35f42847d6e74ec9ca805fc25 -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file cvmatrix-2.0.1-py3-none-any.whl.
File metadata
- Download URL: cvmatrix-2.0.1-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ba3118e9ce40f4f321350490f7bd578e6a942e5e6654cbc2b89a47c1a80cd7e
|
|
| MD5 |
75c5599988d4cf5c68ddaa903a6ef85a
|
|
| BLAKE2b-256 |
83c126dc4733d5cb37d7c3664b0ca41fc0b5ee12a63a010d92215f97e7828d19
|
Provenance
The following attestation bundles were made for cvmatrix-2.0.1-py3-none-any.whl:
Publisher:
package_workflow.yml on sm00thix/cvmatrix
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cvmatrix-2.0.1-py3-none-any.whl -
Subject digest:
5ba3118e9ce40f4f321350490f7bd578e6a942e5e6654cbc2b89a47c1a80cd7e - Sigstore transparency entry: 259699829
- Sigstore integration time:
-
Permalink:
sm00thix/cvmatrix@39e01703e63186e35f42847d6e74ec9ca805fc25 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sm00thix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package_workflow.yml@39e01703e63186e35f42847d6e74ec9ca805fc25 -
Trigger Event:
workflow_run
-
Statement type: