Skip to main content

CVMatrix

PyPI Version

Python Versions

Pepy - Total Downloads

PyPI - Downloads

License

Documentation Status

CodeFactor

Tests Status

Test Coverage

Package Status

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

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.

Citation

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 import CVMatrix
    
  • You can also install the optional JAX dependency to enable the JAX backend of CVMatrix:

    pip3 install "cvmatrix[jax]"
    
  • Now, you can select the JAX backend by passing backend="jax" to the constructor:

    from cvmatrix import CVMatrix
    
    cvm = CVMatrix(
        center_X=True, center_Y=True, scale_X=True, scale_Y=True, backend="jax"
    )
    

    With backend="jax", the per-fold training-matrix computations (training_XTX, training_XTY, training_XTX_XTY, and training_statistics) use jax.numpy and can be traced by jax.jit and batched over folds with jax.vmap on CPUs, GPUs, and TPUs. The default backend="numpy" is unchanged and remains byte-for-byte identical to previous releases.

Prerequisites for the JAX backend

The JAX backend supports running on CPU, GPU, and TPU.

  • To enable NVIDIA GPU execution, install JAX and CUDA with:

    pip3 install -U "jax[cuda13]"
    
  • To enable Google Cloud TPU execution, install JAX with:

    pip3 install -U "jax[tpu]"
    

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 the JAX backend uses float64, set the environment variable JAX_ENABLE_X64=True as per the Common Gotchas. CVMatrix also enables this automatically when constructed with backend="jax" and a 64-bit dtype (the default). Alternatively, float64 can be enabled with the following function call:

import jax
jax.config.update("jax_enable_x64", True)

Validating weights and folds before tracing

Under jax.jit and jax.vmap, traced values are unknown until the compiled computation runs, so CVMatrix cannot raise for traced weights or, with weights, for traced fold indices. Checking inside the computation would slow down every call, so CVMatrix instead checks everything that is known while tracing (concrete weights and fold indices, and the training-set size of every fold without weights) and leaves the rest to validate_folds, which you call once on the concrete values before tracing. Without it, invalid weights or folds silently give wrong or NaN results.

import jax
import jax.numpy as jnp
import numpy as np
from cvmatrix import CVMatrix, Partitioner

jax.config.update("jax_enable_x64", True)  # Before tracing, for float64.

rng = np.random.default_rng(42)
X = rng.random((8, 100, 5))  # 8 datasets of 100 samples each.
Y = rng.random((8, 100, 2))
W = rng.random((8, 100))  # One weight vector per dataset.
p = Partitioner(np.arange(100) % 5)  # 5 equal-size folds, shared by all datasets.
val_indices = jnp.asarray(np.stack(list(p.folds_dict.values())))

# Check the concrete weights and folds on the host, before they are traced.
for weights in W:
    CVMatrix(backend="jax").validate_folds(p, 100, weights)

def per_dataset(x, y, w):
    cvm = CVMatrix(backend="jax")
    cvm.fit(x, y, w)  # `w` is traced here, so it cannot be checked.
    return jax.vmap(cvm.training_XTX_XTY)(val_indices)

(XTX, XTY), stats = jax.jit(jax.vmap(per_dataset))(X, Y, W)

Quick Start

Use the cvmatrix package for fast computation of training set kernel matrices

import numpy as np
from cvmatrix import CVMatrix
from cvmatrix import Partitioner

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. If centering or scaling is used, the sum of weights
# for any training partition must be greater than zero. If scaling is used, the
# number of non-negative weights for any training partition must be greater than
# the ddof provided in the constructor.
weights = np.random.uniform(size=(N,)) + 0.1

# Instantiate CVMatrix
cvm = CVMatrix(
    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, Y, and weights
cvm.fit(X=X, Y=Y, weights=weights)

# Instantiate Partitioner
p = Partitioner(folds=folds)

# Compute training set XTWX and/or XTWY for each fold
for fold in p.folds_dict:
    val_indices = p.get_validation_indices(fold)
    # Get both XTWX, XTWY, and weighted statistics
    result = cvm.training_XTX_XTY(val_indices)
    (training_XTWX, training_XTWY) = result[0]
    (training_X_mean, training_X_std, training_Y_mean, training_Y_std) = result[1]
    
    # Get only XTWX and weighted statistics for X.
    # Weighted statistics for Y are returned as None as they are not computed when
    # only XTWX is requested.
    result = cvm.training_XTX(val_indices)
    training_XTWX = result[0]
    (training_X_mean, training_X_std, training_Y_mean, training_Y_std) = result[1]
    
    # Get only XTWY and weighted statistics
    result = cvm.training_XTY(val_indices)
    training_XTWY = result[0]
    (training_X_mean, training_X_std, training_Y_mean, training_Y_std) = result[1]

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.

The figures below add the optional JAX backend to the comparison (NumPy vs. JAX, and the JAX execution modes). They are measured multi-threaded (no thread limit; GPU on an NVIDIA RTX 3090 Ti) and exclude NaiveCVMatrix:


Left: CVMatrix with the NumPy backend versus the JAX backend (warm, JIT-compiled jax.vmap over folds) on CPU and GPU, for the three common preprocessing combinations. Right: The JAX backend under no-JIT (eager vmap), cold-JIT (compilation + run) and warm-JIT (run only), on CPU and GPU. JIT compilation is a large fixed cost: on the GPU, no-JIT beats cold-JIT for a single run, whereas on the CPU, JIT (even cold) is far faster than eager vmap at a large number of folds.

Contribute

To contribute, please read the Contribution Guidelines.

References

  1. 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).
  2. Dayal, B. S. and MacGregor, J. F. (1997). Improved PLS algorithms. Journal of Chemometrics, 11(1), 73-85.
  3. 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

Metadata

Release files for cvmatrix 3.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cvmatrix 3.4.0
File Size Uploaded Provenance Actions
cvmatrix-3.4.0.tar.gz 653.6 kB With provenance Details for cvmatrix-3.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cvmatrix 3.4.0
File Interpreter ABI Platform Actions
cvmatrix-3.4.0-py3-none-any.whl Python 3 none any Details for cvmatrix-3.4.0-py3-none-any.whl

Total release size: 680.0 kB

Release files / cvmatrix-3.4.0.tar.gz

Download URL cvmatrix-3.4.0.tar.gz
Size 653.6 kB
Tags Source
SHA-256 checksum
How to use checksums
428e5f566afc971b135f2ada265b0b10c3f56cedcf7961bb21fcad8b2b5cdbef
BLAKE2b-256 checksum
How to use checksums
9c1d337c449fcc4ba6d54f2505a1ce4cdbb0521f89692eaabc2c7660d4c1d96c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 1, 2026.

Transparency log

Release files / cvmatrix-3.4.0-py3-none-any.whl

Download URL cvmatrix-3.4.0-py3-none-any.whl
Size 26.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5b42eff04f5c4906558a47fc765847fdfe30213999dfdd84724f71e07792b16
BLAKE2b-256 checksum
How to use checksums
f6741a73a4b48eb472472024ade268e3f44a8a33555f4341ee00baf953c58ff3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 1, 2026.

Transparency log

Security for cvmatrix 3.4.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for cvmatrix-3.4.0.tar.gz

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 Nov 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.4.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page