Skip to main content

glmnet-rs

A port of glmnet's elastic-net coordinate descent to Rust, with a Python front end.

Ported from glmnetpp (the C++17 core of R glmnet >= 4.1), not the legacy Fortran, and validated against R glmnet 5.0.

Status: Gaussian, two-class binomial (logistic), and Poisson (dense X), plus sparse X for Gaussian and binomial (CSC). Cross-validation, summaries, and R-style plots are implemented. ~90 parity fixtures pass at ~1e-14 relative error with iteration counts (npasses) identical to R — the sparse ones against R's own sparse path — and coefficients match R to ~1e-13 on real long/wide datasets (see datasets/). What's still missing (multinomial, Cox, relax, offsets, sparse Poisson, …) is tracked in docs/ROADMAP.md; porting notes are in docs/PORTING.md.

Example: examples/glmnet_demo.ipynb is a runnable tour on two real datasets — Wine Quality (long, gaussian) and Leukemia gene expression (wide, binomial) — covering the path, cross-validation, plots, and prediction.

Layout

crates/glmnet-core/   pure Rust kernels (no Python, no C)
crates/glmnet-py/     PyO3 bindings, deliberately thin
python/glmnetrs/      the user-facing package
scripts/gen_fixtures.R   generates the R reference fixtures
tests/fixtures/       committed R glmnet output (tests run without R)

Two APIs, one solver

Faithful to R — the lambda path is the primitive, because it is what the algorithm actually computes:

from glmnetrs import glmnet

path = glmnet(X, y, alpha=1.0)     # alpha = elastic-net mixing (1 = lasso)
path.lambda_                       # (lmu,) descending
path.beta                          # (p, lmu)
path.coef(s=0.05)                  # interpolated, as in R's coef(fit, s=)
path.predict(X, s=0.05)
path.df                            # nonzeros per lambda

# logistic regression, same path object
lpath = glmnet(X, y01, family="binomial")
lpath.predict(X, s=0.05, type="response")   # class-1 probability

# poisson counts
ppath = glmnet(X, counts, family="poisson")
ppath.predict(X, s=0.05, type="response")   # expected count, exp(eta)

# sparse X (gaussian): pass a scipy sparse matrix, never densified
import scipy.sparse as sp
spath = glmnet(sp.csc_matrix(X), y)          # ~20x faster when p >> n and sparse

# cross-validation to pick lambda (matches R's cv.glmnet)
from glmnetrs import cv_glmnet
cv = cv_glmnet(X, y, family="gaussian", type_measure="mse", nfolds=10)
cv.lambda_min, cv.lambda_1se
cv.predict(X, s="lambda.1se")

# summaries, like R's print()
print(path)        # Df / %Dev / Lambda table
print(cv)          # lambda.min / lambda.1se with measure and SE
path.to_frame()    # optional pandas DataFrame

# plots, like R's plot.glmnet / plot.cv.glmnet (needs matplotlib)
path.plot(xvar="lambda")   # coefficient paths, Df on the top axis
cv.plot()                  # CV curve with error bars + min/1se lines

scikit-learn compatible, using scikit-learn's meaning of alpha:

from glmnetrs.sklearn import ElasticNet, Lasso, LogisticRegression

m = ElasticNet(alpha=0.1, l1_ratio=0.7).fit(X, y)   # alpha = penalty strength
m.coef_, m.intercept_

clf = LogisticRegression(C=1.0, penalty="l2").fit(X, y01)
clf.predict_proba(X)

The alpha trap. In glmnet alpha is the mixing parameter and lambda is the penalty strength. In scikit-learn alpha is the penalty strength and l1_ratio is the mixing. Worse, the two objectives are not related by a simple rename: glmnet rescales y to unit variance, which leaves the L2 term carrying a factor of 1/sd(y). glmnetrs.sklearn handles the conversion; the derivation is in docs/PORTING.md.

Develop

cargo test -p glmnet-core --release        # parity against committed fixtures
maturin develop --release --uv             # build the extension
python -m pytest tests/test_python.py      # end-to-end + sklearn agreement

Rscript scripts/gen_fixtures.R             # regenerate Gaussian fixtures (needs R + glmnet)
Rscript scripts/gen_fixtures_binomial.R    # regenerate binomial fixtures
Rscript scripts/gen_fixtures_poisson.R     # regenerate Poisson fixtures
Rscript scripts/gen_fixtures_sparse.R      # regenerate sparse Gaussian fixtures (needs Matrix)
Rscript scripts/gen_fixtures_sparse_glm.R  # regenerate sparse binomial/poisson fixtures

python scripts/bench.py                    # wall-clock vs R glmnet on identical data
python scripts/compare_datasets.py         # coefficients + timing vs R on real datasets
cargo run --release -p glmnet-core --example bench_core   # pure-core timings

Performance

Full-path wall clock vs R glmnet on identical data (Apple Silicon): Gaussian runs at ~0.6–0.85x of R, two-class logistic at ~0.7–1.1x (faster than R when n >> p). glmnet's compiled core is heavily tuned Eigen/SIMD, so parity-to-1.5x-slower is the expected range for a pure-Rust port. Inner products use four-accumulator reductions (matrix::dot4) that vectorize; see docs/PORTING.md.

License

GPL-2.0-only, matching upstream glmnet.

Metadata

Release files for glmnet-rs 0.1.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 glmnet-rs 0.1.0
File Size Uploaded
glmnet_rs-0.1.0.tar.gz 49.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for glmnet-rs 0.1.0
File
glmnet_rs-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
glmnet_rs-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
glmnet_rs-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
glmnet_rs-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
glmnet_rs-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 1.6 MB

Release files / glmnet_rs-0.1.0.tar.gz

Download URL glmnet_rs-0.1.0.tar.gz
Size 49.9 kB
Tags Source
SHA-256 checksum
How to use checksums
69eeea52c48a7591ca596ba4bd68effa15b9086ac70523f538453763bc5ce4ef
BLAKE2b-256 checksum
How to use checksums
536219736d1d84884488c65615a1ab76c0b393ae6de1fc8bf79d233334638e9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 16, 2026.

Transparency log

Release files / glmnet_rs-0.1.0-cp39-abi3-win_amd64.whl

Download URL glmnet_rs-0.1.0-cp39-abi3-win_amd64.whl
Size 229.7 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c53e68edfceab851947ac415b2796b7fc2e612b835b79a4cb8951abda3022f82
BLAKE2b-256 checksum
How to use checksums
4aa3c88d8d2527af47db7506c4f8433eab438d6a7ed25ba564a92f3152c5a7b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 16, 2026.

Transparency log

Release files / glmnet_rs-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL glmnet_rs-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 341.9 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
0abeabcbc65b350a1a4a54d2c6b742eaa5231f10a3ca327b85d2766a6430f840
BLAKE2b-256 checksum
How to use checksums
b847a82a8aa2ea30859a1ed1f6da1ff1581ed2e07b0958a7651f9acf61211962
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 16, 2026.

Transparency log

Release files / glmnet_rs-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL glmnet_rs-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 334.5 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
2723a9d11b72131253f3f1d13daf64713f27c7affa6c943c52ac56187a6ed6f8
BLAKE2b-256 checksum
How to use checksums
f2e904e0f7d2284a3e95084e4786a497dff90e1b42a108a8a418073424241434
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 16, 2026.

Transparency log

Release files / glmnet_rs-0.1.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL glmnet_rs-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 313.0 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9ae0ed90dbfd000f3bb848331895c948ecebaffde2e3cfb5e773af2ccd6d71d3
BLAKE2b-256 checksum
How to use checksums
3412bd10609955158da7cc0a9ee5db8d324897a0056428752ff0b4a91fd45096
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 16, 2026.

Transparency log

Release files / glmnet_rs-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL glmnet_rs-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 324.2 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7de399854393b732c232ef60616436c49ac64a159e92e771ccfadcd97432038d
BLAKE2b-256 checksum
How to use checksums
c420fbcfe94948ed4b6eaa5c516195216e94a89d1ba052c50e43c60a5b04fdf3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files

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