Native gaussian elnet (glmnet C++ core) binding for Python.
This package wraps the header-only C++ core of R's glmnet (glmnetpp, the
=4.0 reimplementation) behind a small
extern "C"ABI and actypesbinding. It fits ridge / elastic-net gaussian models that are numerically identical to Rglmnet(including theintercept=FALSErefit path) without depending on the R runtime.
pip install glmnetpp
Heads-up — compiled extension. The numerical work is done by a native C++ extension (
glmnetpp/_core.*), built from the vendoredglmnetppC++ core + Eigen (header-only). When a prebuilt wheel is available for your platform/Python,pip install glmnetppJust Works. When it is not, pip builds from the sdist, which requires a C++17 compiler and the Eigen headers on your machine (see Building from source below). To ship wheels for every platform, build them in CI — see the Publishing wheels note at the end.
R's glmnet(family="gaussian", alpha=0, ...) is the reference. The old
glmnet_python wrapper ships a 2013 Fortran GLMnet.f whose intercept=FALSE
path diverges from R's current (C++) glmnet by ~0.2–0.3%. The C++ core
(glmnetpp) is the same code R 4.1-8 runs, so it matches exactly.
from glmnetpp import fit_gaussian_ridge
intercept, coef, jerr = fit_gaussian_ridge(
X, y, lam=0.1, alpha=0.0,
lower_limits=[0.0]*p, upper_limits=[None]*p,
standardize=True, intercept=True,
)coef is on the original (unstandardized) scale, matching
as.numeric(coef(glmnet(...))).
This is a Poetry package. poetry-core owns packaging/metadata/dependencies;
it does not compile C extensions itself — instead it runs
scripts/build-extension.py,
which drives setuptools' build_ext (Eigen discovery + compiler-conditional
flags) and builds the extension in-place under src/glmnetpp/.
Build-only dependency: Eigen (header-only).
# either (with Poetry installed):
poetry install
# or (plain pip — uses the poetry-core backend, runs the build script):
pip install -e .
The build script searches for Eigen in this order and picks the first one that
contains Eigen/Core:
third_party_eigen/eigen-<version>/inside the project (what CI uses — drop the header-only Eigen release there and it builds anywhere, no system package needed)$EIGEN_INCLUDE_DIR/usr/include/eigen3(libeigen3-dev)/usr/lib/R/site-library/RcppEigen/include(r-cran-rcppeigen)- macOS Homebrew (
/usr/local/include/eigen3,/opt/homebrew/include/eigen3) - Windows — vcpkg (
$VCPKG_ROOT/installed/x64-windows/include/eigen3) or Conda ($CONDA_PREFIX/Library/include/eigen3)
The vendored-from-R headers (the C++ glmnetpp core, copied from CRAN
glmnet) and the full list of every part copied from R are documented in
cpp/glmnetpp_include/README.md
in the source tree.
Prebuilt wheels are produced by the GitHub Actions workflow in
.github/workflows/python-publish.yml. It uses
cibuildwheel to build manylinux (Linux),
Windows and macOS (x86_64 + arm64) wheels for CPython 3.9–3.13, plus an
sdist, and publishes them all to PyPI.
Because the C++ extension needs Eigen at build time, the workflow downloads the
header-only Eigen release into third_party_eigen/ inside the project tree
before building; scripts/build-extension.py finds it there relative to the
repo root, so the same path works on every OS and inside cibuildwheel's
manylinux container. (That directory is gitignored — it's a build input, not
source.) Each wheel is smoke-tested (import + fit_gaussian_ridge) in CI before
it ships, so end users of these platforms never need a C++ compiler or Eigen.
GPL-2.0-or-later, inherited from the upstream glmnet / glmnetpp C++ core
(these headers are a derivative of that code). See the
LICENSE file.