Skip to main content

GlmNet for Python

Project description

glmstar

A Python implementation of the glmnet algorithm for fitting generalized linear models via penalized maximum likelihood. This package provides fast and efficient regularization paths for linear, logistic, multinomial, Poisson, and Cox regression models.

Features

  • Multiple GLM Families: Support for Gaussian (linear), Binomial (logistic), Multinomial, Poisson, and Cox regression
  • Elastic Net Regularization: Combines L1 (Lasso) and L2 (Ridge) penalties with mixing parameter α
  • Fast Path Algorithms: Efficient computation of regularization paths using coordinate descent
  • Cross-Validation: Built-in cross-validation for optimal λ selection
  • Prediction Methods: Interpolation and prediction at arbitrary λ values
  • sklearn Compatibility: Follows scikit-learn API conventions
  • C++ Backend: High-performance C++ implementation with pybind11 bindings

Installation

Prerequisites

The package requires the Eigen C++ library. You have two options:

Option 1: Use the included submodule (Recommended)

# Clone with submodules
git clone --recursive https://github.com/jonathan-taylor/pyglmnet.git
cd pyglmnet

# Or if already cloned, initialize submodules
git submodule update --init --recursive

Option 2: Install Eigen separately

# Clone Eigen 3.4.0
git clone --branch 3.4.0 https://gitlab.com/libeigen/eigen.git --depth 5

# Set environment variable
export EIGEN_LIBRARY_PATH=/path/to/eigen

Install the package

# Install in development mode
pip install .[dev]
pip install -e . --no-isolation-build

# Or install directly
pip install .

Run the tests

pip install .[test]
pytest tests --test-size=small

Quick Start

import numpy as np
from glmnet import GaussNet, LogNet
from sklearn.datasets import make_regression, make_classification

# Linear Regression
X, y = make_regression(n_samples=100, n_features=20, n_informative=5, random_state=42)
fit_gaussian = GaussNet().fit(X, y)

# Plot coefficient paths
ax = fit_gaussian.coef_path_.plot()
ax.set_title('Linear Regression Coefficient Paths')

# Logistic Regression
X, y = make_classification(n_samples=100, n_features=20, n_informative=5, random_state=42)
fit_logistic = LogNet().fit(X, y)

# Cross-validation
cvfit = LogNet().fit(X, y)
_, cvpath = cvfit.cross_validation_path(X, y, cv=5)
ax = cvpath.plot(score='Binomial Deviance')

Available Models

  • GaussNet: Linear regression (Gaussian family)
  • LogNet: Logistic regression (Binomial family)
  • MultiClassNet: Multinomial regression
  • FishNet: Poisson regression
  • MultiGaussNet: Multi-response linear regression
  • CoxNet: Cox proportional hazards model

Key Features

Elastic Net Regularization

# Lasso (α=1, default)
fit_lasso = GaussNet(alpha=1.0).fit(X, y)

# Ridge (α=0)
fit_ridge = GaussNet(alpha=0.0).fit(X, y)

# Elastic Net (α=0.5)
fit_elastic = GaussNet(alpha=0.5).fit(X, y)

Cross-Validation

# Perform cross-validation
cvfit = GaussNet().fit(X, y)
_, cvpath = cvfit.cross_validation_path(X, y, cv=5)

# Get optimal lambda
lambda_min = cvpath.index_best['Mean Squared Error']
coef_min, intercept_min = cvfit.interpolate_coefs(lambda_min)

Prediction and Interpolation

# Predict at specific lambda values
predictions = fit_gaussian.predict(X_new, interpolation_grid=[0.1, 0.05])

# Interpolate coefficients
coef, intercept = fit_gaussian.interpolate_coefs(0.5)

Weights and Offsets

import pandas as pd

# Create DataFrame with response, weights, and offsets
Df = pd.DataFrame({
    'response': y,
    'weight': sample_weights,
    'offset': offsets
})

# Fit with weights and offsets
fit = GaussNet(response_id="response", 
               weight_id="weight", 
               offset_id="offset").fit(X, Df)

Dependencies

  • Core: numpy, scipy, pandas, scikit-learn
  • Build: pybind11, setuptools, wheel
  • Optional: matplotlib, joblib, statsmodels, tqdm

Development

Building from Source

# Install build dependencies
pip install -r requirements.txt

# Build the package
python setup.py build_ext --inplace

Running Tests

# Run all tests
pytest tests/

# Run specific test suites
pytest tests/paths/
pytest tests/compare_R/

Citation

If you use this package in your research, please cite:

Friedman, J., Hastie, T., & Tibshirani, R. (2010). 
Regularization paths for generalized linear models via coordinate descent. 
Journal of Statistical Software, 33(1), 1-22.

License

This project is licensed under the BSD-3-Clause License - see the LICENSE.txt file for details.

Authors

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Acknowledgments

This package is based on the original R glmnet package by Jerome Friedman, Trevor Hastie, Rob Tibshirani, and others. The C++ implementation uses the Eigen library for linear algebra operations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

glmstar-0.1.2-cp314-cp314t-musllinux_1_2_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp314-cp314t-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

glmstar-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp314-cp314-macosx_11_0_arm64.whl (5.2 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

glmstar-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp313-cp313-macosx_11_0_arm64.whl (5.2 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

glmstar-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (5.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

glmstar-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl (6.4 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (5.2 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

glmstar-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl (6.4 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp310-cp310-macosx_11_0_arm64.whl (5.2 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

glmstar-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl (6.4 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

glmstar-0.1.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

glmstar-0.1.2-cp39-cp39-macosx_11_0_arm64.whl (5.2 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file glmstar-0.1.2-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 891622f55e296deccd6483b9836ca17e5b29e06d11a74f1d4f5e5481fad8e9e2
MD5 19676ecd57cf6719891e22971404487a
BLAKE2b-256 56e277cbadb1e5845b5542915559e1e36f9688ce39329d32b74ba28f70c67113

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dd6f4d564492349c5440b95254aadb365b591c31496fecddaa2044ac5004abe9
MD5 217d0a2cbfaa27536c9d483a9a0aaceb
BLAKE2b-256 c33b9c45ed2503c4070d97d9e921fa17d96f401a0e5cd6d592bf5b56c88bc1c7

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1648de08f63334c7e105218f973479e192c6431af75272f10a164db64715ddab
MD5 a5a718ed9f3afb6786d3c35c334493b1
BLAKE2b-256 cccbefcda5a86c7c412640e555a0d9f5cfbd4f454bbad66718fa7ca2a474547e

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cca3de1d9239f3ac4e376fcd2e9a49af06425bf9357e1250792a8d5e0b580ef9
MD5 3bb4de9e5cf1c4687b1e8d44745914f1
BLAKE2b-256 33c659440102801939b4223f89709834b9e4309ed2f118ea40af6e655492110b

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fee2cfdae1125a319d0e0eb1cc6659199c621b8bf9ce05ae04af8e75d1e2e1e8
MD5 8204994062a97a8e20579c668f1c6bb4
BLAKE2b-256 efdab226af13f57d7b4639ae889e4023d3a3762349a480beaad1b9ce831690f7

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9535ac5ba698ed7fd8638fa35f768557e9bb03e1c17f8e58853c61c395bf40f7
MD5 99b1adb59ef5e2a4149ba3535a7570ee
BLAKE2b-256 c1f4d625a9590b4977a9ebcd2b19890c90cff4e172f57035b7615c00ee3c90e1

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b7aa100bcb10574373c5b92ce0aa6b9b8a84d251471f2415a95980c35ac618d9
MD5 b64b82b795ad97c3cccee593df688ff8
BLAKE2b-256 14d615b43b978d4ed922e8e281dde4b03ea3c268f855fd8d61568643bc06e108

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9b9bbc6e964ad5ae3b2735a83b0a772e3838a5ce5d2380ef4102ba1141777fb3
MD5 c3e3eebd9c866a9bf7c2155e1a7baf62
BLAKE2b-256 c8f80408bbd565e04be6638ad8660deb9f87d684c7a52aae140e0f7b558bff32

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5dd46e5ea1fc91a149af49e3e8ecf2a602f44e7452fcb45fda341e2907da3a89
MD5 b8a1ba47e592fa9fff4a013dc7c35f76
BLAKE2b-256 d9efda603040b57dcca51eb9d720dd9d861c8e76728e3203ab97b7695c44b5bb

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9909c2d5a86e42bcff75c462421673514b9d1ffbc66df047dca4eec6a61e6afb
MD5 1acea409b90f5ae59e62d5b313fa573b
BLAKE2b-256 aac1fa4fa691ee61e2567d930df606023d4bee9aa8dc7e398d4c4d3ebe3f28dd

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9799b8124ea1e0334eb4ca9c387e1d225dbb0cf3cb0ad2da208b74c401ae66f3
MD5 1f8b27728a48b76fc5201bd25771d1de
BLAKE2b-256 d68258c01c6e743569c9d64ec2ed4b30d86296d12e944e2e2c9052e1ee237c73

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 23fa5c0d3cbcceccc7d580dc2990f3d10d80b4c1e6707efc6414f5f986760cd0
MD5 493a2fb18ec20106cdc0eb6167c541bb
BLAKE2b-256 96bfd7ab4e20abf7ff1dcef38ba5999d2cd5c5d9dabe77a98e02cdded5fb7e76

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b56be454bb1acedeae2c108735c7df0c6a79dc8aa3be18e1935f0ceb18de639b
MD5 5e5ee775740ab1d6825a026d137e6322
BLAKE2b-256 4516f063177ae66390d815ebe33a9f1d486f49ed277fcf74dc6427c062c6abb7

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d6bc238cdcbf667c990d54fa0d60d988a8465fa4c816bc4649f960d7069e0843
MD5 e7b80e9d15ac4c46cbfcd6d8da40878c
BLAKE2b-256 0b9a7bc8a7e28f63f4db4745d39929c6fa12948bdfa97cda21d37c84879947e2

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9b31c4e789ddc040cfc41ed6f78e2e9a453db4e83df4a082b1b5e7f1508ce3f0
MD5 db27b44a02cf3ff5701b3354a7734f6e
BLAKE2b-256 698ebc97790ca5683902086f24e7cf99ebde91c608e5562fb678374b21092d23

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 12001070a276cf70328ad62cd6ece33d1a30f1c156b0973c9b63e9aa8b6a38df
MD5 3aff55b74549cd64bf8e4d488ee4b5b9
BLAKE2b-256 d0de96e95630fc73a9129a48eca12a28223212bfad086ca7a714b423b84c475e

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4ceb75e69593ed6426807cce13b4055429e98cfd734b2cf38c8c868644f027b2
MD5 28c4c0029169c1cb9c0e378a49a76ab3
BLAKE2b-256 3c56a6731e1057dab846249c8d2b9e581b57c78d6d9b7d04d79fd23bc4668531

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37d3b534576fca006a6a90199c1afca2267ce0a8048b21134bbe8c298570d81d
MD5 829195af9f7723dd59a34790f86564ff
BLAKE2b-256 295065fd5f7516bbf10e9f3a14cdb43ce64740806ee85cff03989e7b20ae304f

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4870f1ba10d5aef580d93ef566d6c71adddbd32eb5d01ce2f2444f8ed3ecbda9
MD5 6a6c717708543a561de140a489297339
BLAKE2b-256 e20df2ed3e4986e01e24ceb532d4239588f68cea389814d9dea434206210f248

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 137d8cd112f8e290dc62ca3d6e5c9187c89efc57c03fbaf74fd9724679f7009c
MD5 3e6325f894cac34ed33d7f09f5998a3a
BLAKE2b-256 38a5b4226e0e39d9ebb1b029377abe874acb66a0dc9f864dd433a246f2d70ae3

See more details on using hashes here.

File details

Details for the file glmstar-0.1.2-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 efc171d43d0ffa1f3f7ed8518d92058e337512580ffaece970fc9a790a2e46de
MD5 13fb19cafb74787b4b0a95ce8db46573
BLAKE2b-256 7826c25b971deba1f37d933fa7a3916761b48a3a4637d0ef2fe01a542df51a27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page