Skip to content

About

Lazy representations of normalized (scaled and centered) matrices

Topics

Resources

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

224 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LazyMatrix LazyMatrix logo

CI crates.io docs.rs PyPI DOI

LazyMatrix is a Rust library for centering and scaling design matrices without creating a normalized copy. It applies normalization during matrix operations, preserving sparse storage. It supports dense and sparse matrices, borrowed views, and chunked Zarr arrays. A companion Python package supports NumPy, SciPy, and PyTorch.

See the documentation for guides, benchmarks, and API references.

Installation

Add lazymatrix and enable the feature for your matrix library. For ndarray:

cargo add lazymatrix --features ndarray
cargo add ndarray@0.17

The core library requires Rust 1.84 or newer. Optional backends can require a newer compiler; see backend setup.

For Python, run pip install lazymatrix and see the Python README for a quick start.

Usage

Fit column means and standard deviations, then multiply by the normalized matrix:

use lazymatrix::{ColumnNormalization, LazyMatrix, MatVec};
use ndarray::array;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let x = array![[1.0, 0.0], [2.0, 3.0], [0.0, 4.0]];
    let normalization = ColumnNormalization::standardize();
    let lazy = LazyMatrix::new(x.view(), normalization)?;

    let y = lazy.matvec(&array![1.0, -1.0])?;
    println!("{y}");
    Ok(())
}

The operator borrows the original matrix and stores its fitted centers and scales. Standardization uses population standard deviations. Use ColumnNormalization::min_max() for min-max scaling or ColumnNormalization::robust() for median centering and interquartile-range scaling. Centering and scaling are independently optional.

Fit parameters once and reuse them for validation or prediction without refitting. The normalization guide shows how to share fitted parameters between operators.

Implement NormalizationRecipe<M, P> for custom fitting methods that return the existing parameter types. Custom recipes work with LazyMatrix::new, .fit(), and .fit_apply() and can report their own fitting errors.

The examples cover gradient descent, coordinate descent, ridge regression, weighted Gram matrices, and file-backed input. Run one from a checkout with:

cargo run --locked --example coordinate_descent --features faer

Features

  • Row and column normalization with mean, minimum, or median centering and scaling by standard deviation, L1 or L2 norm, maximum absolute value, range, or quantile range.
  • Reusable fitted parameters for training and validation, and ordered normalization recipes composed with .then().
  • Matrix products including forward and transpose products, batched products, reusable output buffers, and weighted Gram matrices.
  • Borrowed views of logical rows, columns, and submatrices for supported storage, plus an implicit intercept column through WithIntercept.
  • Explicit eager conversion, including in-place normalization of writable dense storage.
  • Chunked Zarr input for matrices larger than RAM.

Capabilities vary by normalization and backend. See the Rust API reference for supported combinations and traits for custom backends.

Matrix backends

No matrix backend is enabled by default. Match your matrix dependency to one of these features:

Feature Supported releases Storage
faer 0.22, 0.23, 0.24 Dense, CSC, and CSR matrices
nalgebra 0.32, 0.33, 0.34, 0.35 Dense, CSC, and CSR matrices
ndarray 0.15, 0.16, 0.17 Dense arrays and views
sprs 0.11 CSC and CSR matrices
zarrs 0.22, 0.23 Synchronous two-dimensional Zarr arrays

The short feature names select the newest supported release. Use a versioned feature such as nalgebra_v0_34 for an older release, and match your direct backend dependency to it. Enable parallel alongside a backend for parallel column statistics where supported.

Optional BLAS acceleration is available for ndarray. See the BLAS setup guide for features and provider configuration.

Documentation

Citation

If you use LazyMatrix in your research, please cite the release you used. Select that release in the Zenodo archive and export its citation. To cite LazyMatrix generally, use the concept DOI 10.5281/zenodo.23666450.

Machine-readable metadata is available in CITATION.cff.

Contributing

Bug reports, documentation improvements, and code contributions are welcome. See CONTRIBUTING.md for setup, checks, and pull request guidance for Rust, Python, and the website.

License

Licensed under either the Apache License, Version 2.0 or the MIT license, at your option.

About

Lazy representations of normalized (scaled and centered) matrices

Topics

Resources

Contributing

Stars

2 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages