Skip to main content

GlmNet for Python

Project description

pyglmnet

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

# Or install directly
pip install .

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 Distribution

glmstar-0.1.0.tar.gz (4.5 MB view details)

Uploaded Source

Built Distributions

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

glmstar-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl (28.8 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

glmstar-0.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (27.8 MB view details)

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

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

Uploaded CPython 3.13macOS 11.0+ ARM64

glmstar-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl (28.8 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

glmstar-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (27.8 MB view details)

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

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

Uploaded CPython 3.12macOS 11.0+ ARM64

glmstar-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl (28.8 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

glmstar-0.1.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (27.7 MB view details)

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

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

Uploaded CPython 3.11macOS 11.0+ ARM64

glmstar-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl (28.7 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

glmstar-0.1.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (27.6 MB view details)

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

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

Uploaded CPython 3.10macOS 11.0+ ARM64

glmstar-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl (28.7 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

glmstar-0.1.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (27.5 MB view details)

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

glmstar-0.1.0-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.0.tar.gz.

File metadata

  • Download URL: glmstar-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for glmstar-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eddd61d86d8816cbcdad97825056c2765ba2d1cd2e47328d5f60e727e5bb483d
MD5 02c34892f9e9383064567bc16b830cfd
BLAKE2b-256 94a00a603099c62b56dca61b29e01bdf2a72cd14444361010a6e6118a66d656f

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0.tar.gz:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d2ff6ef23f90413434c9eac7f8ef2ab1ea86278ddc92b195ff1442bdcc9e6925
MD5 a99dee2243f7b05fa2777878af5278d9
BLAKE2b-256 3a4c3a1f7d470825388a40502484e2e542059b482981b3174c4b21ac821b0108

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5fcce43e163b68700c4ff8164dad58fc26d83a233e14888d240f020eee7defaf
MD5 2fb0d0e475706f8f37eff50265cf98d5
BLAKE2b-256 7970c702966d256941c25b443aa172a2dee43b15d21569e6d8012db432417cc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 69878a85c1df5aa56d5242abf2a0e6c875aa823ddb2d22e7c33574bfde50f69f
MD5 fedc4f1f6050a89c485c580b6f27a46e
BLAKE2b-256 0a75e30f36e431365a0c3ccc2ae9c0067322b4493cff1ca5643d02202f115149

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fbcda10adb2a5e7a2ae6f4ca8bb3fdca241d6bdfc7bfb521d50878bf42d7ac7b
MD5 c9b99bfdf3223606bb59803eebd74a52
BLAKE2b-256 1005480096b2a8cb91ee43501493ca4f6e04842f8b8c65b63922faa9aad1ab98

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 59daed74656323d1cc7fe81b67e5401d2be11c25eb6e95cd7d1271c66e4fcd90
MD5 3dea37175a8785377326901c44ce23fb
BLAKE2b-256 0a7cafe20a8d6d7341b8fb9b22ccc4325060b30598bc014281b777fd0395b287

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c94e08c8b79c443db2231b126f0fa42c63b9737c6a9a5f60815fa903067dfab3
MD5 448abc9195cbff9913a9bbf40b83d9bb
BLAKE2b-256 5c19c08ad37d7932985e238de069eeb8cacadb525036e53a95da4a1234f0483c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 99179d53449cb17d8cf06fb3c9291b367e3df0e3d520713f00cc5edddef80cea
MD5 351a445d8389b32ccbb1512211d5185d
BLAKE2b-256 a9f80fe9d35e98aab48e3cb824781b81eb87cd242f0e6a58950c23a84fe355dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1e1c41aee8a930dde6985197b1d522680aca161786074bb60e647356029141d9
MD5 47537fe6347a4e0102ccd5b9d66d7831
BLAKE2b-256 675b74358e9844cb8161701f890e8ceeca38eaedfda339e2fedc829c4899b342

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 db61beb1de6fc08d91c6088bbdff20d0832463899949007c8e6cfd736cf5354d
MD5 33709f0293916c7f0c8542271dd6fc78
BLAKE2b-256 73a80b32b6991fd1a5d2252c26fdf152f9d1a25b4dd1a05a3be4a88fcc4412bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8542e7a26e97136281f90a3d72a92a3bf3f8893aaaf1235ade18aa12e448f08b
MD5 f93ec095d38a34e72f852f8877bb93e8
BLAKE2b-256 c3e077187f99b7b5c2796e145b1338cd6d1923e9962a7134d88bca41c8bc9c4d

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8aea82bdd5a67502689192eeef599b33a7d1971b9e5d11659204959190d83470
MD5 090f63d94796ecf2972cff6339444ee9
BLAKE2b-256 3e319726ca7f88fe3d1a35527605c3a8686f48ff807266f828862e14635929e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 56280ab3707328730d0f1f43fd1e3d1a0c2a0578b1f85dd6761992bcc85fb493
MD5 b7b05a799fc67704112b7ae849e4b64b
BLAKE2b-256 3be9f60d468124296037ca5ef4f06bca5ee16c7073282dc10e2f41bf4d0cfb60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 44d2a4e4387864b27642a60ea2f14bb5be3f4c4b15d96395e9b310b9be54d1e3
MD5 f65cf055781c36e3f7680f3d4b4497d9
BLAKE2b-256 1e6e3829f6eb3ffc2037edaf66ce10822b933f4d6fdd1d94e5cc0ca90b2c27b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6e7a5dd4fd9ad6acfc343ef2d1d02ca7f1f947556c22dfe0d0c4990012a02f10
MD5 6ad29f67825ccae867097cd72554812f
BLAKE2b-256 0b79fc47c644d243e832004cd085c773bd7c45f98002b166f5241ff0b19e1932

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.0-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmnetpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for glmstar-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b2cc467598443003e195044bb3c8b35cd6c0af6a9fe8e13fac908464f91bd2f3
MD5 dec865e65853024d8c00683260976ee6
BLAKE2b-256 ff023e1ab7ec776d26b8355bf7fd2ffe926da5fb5a58af56b3c177bd269e60ba

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