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 -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.1.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.1-cp313-cp313-musllinux_1_2_x86_64.whl (30.2 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

glmstar-0.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (29.2 MB view details)

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

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

Uploaded CPython 3.13macOS 11.0+ ARM64

glmstar-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (30.2 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

glmstar-0.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (29.2 MB view details)

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

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

Uploaded CPython 3.12macOS 11.0+ ARM64

glmstar-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (30.1 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

glmstar-0.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (29.0 MB view details)

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

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

Uploaded CPython 3.11macOS 11.0+ ARM64

glmstar-0.1.1-cp310-cp310-musllinux_1_2_x86_64.whl (29.9 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

glmstar-0.1.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (28.9 MB view details)

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

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

Uploaded CPython 3.10macOS 11.0+ ARM64

glmstar-0.1.1-cp39-cp39-musllinux_1_2_x86_64.whl (29.9 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

glmstar-0.1.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (28.9 MB view details)

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

glmstar-0.1.1-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.1.tar.gz.

File metadata

  • Download URL: glmstar-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 c64761537912b1ed7cf40346e9c6f182f05013e7f111cd0d2f442f3fd5e1fb30
MD5 47cfaeb8e5a6a36d01d08589982d0acc
BLAKE2b-256 24c012a231ff2619e315174f07872d88bbe40a6699a61f16d94d14555124b29d

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 54fdf63cca791c40b44c25b0648469fac46597b17577d1efbe2271f1d21c42ec
MD5 f4f1a9bc913be6a8fb59a296db04ec7c
BLAKE2b-256 3488abc7b13e15da0da0013dc56a46c9358f40c40e1078d38ea7f2e54f1ae35c

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 50da598768f3ad604da7a2e4aa6e217cfaf9b1bef8622a259d704c93df3f2651
MD5 1c52f31901973843f15f426ca49a2a77
BLAKE2b-256 8643e61fbc7eddbcfd491ab4bdfeb8ef61013fbb2db67ee46ae88d03154aa684

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b82d214ebdaee5fe444366f802427969411ba1320f6ce69f5cba62368cfdb21a
MD5 5898415279e7cf63b0363935c2480ce4
BLAKE2b-256 d4f6e0296ad79b2237e5b2bcd6b2b3359ee2df99938c4ea4dc76abcdaa53dd4d

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cc5b3c15928f60258b145418e55c939e4174ba67b505d30a5047fb8b3346c426
MD5 ea13b77725e7e8380afdca2f83213256
BLAKE2b-256 90a77d955cbf1cd59f70d0d029c3a1a2618646454ba56e3df815a0952a9ee487

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 20625f75a2165584daea66588c6242961406f22b0719b16b4ada657bb6a0a732
MD5 0a3fc755677174d95e33f199db87acb2
BLAKE2b-256 358987e6562596383ae0f23dc39208053a624eb402a3f79f841371f8dd906033

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9f6617895d539941bdec6a2b3e964b7f67854e355cefbb22d053461aabb28125
MD5 8c6a1c504ae7e11d780d541bb431c21e
BLAKE2b-256 e17001d813b194020857cd19355bc80efbf9399a8f3d0cd9fe5d0da9a80dacf3

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 605c53b578fcf23ee19956e779a0d85f312527451500a3823d2082cf7911a65c
MD5 01d2e500d49cb6be12466957099fd8aa
BLAKE2b-256 f309e21a8506bf4b489f91af22c2706e29693b74e0b4ef65eb52519655150803

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 33d7218cea3ee22418556552bea549c7d43d37a251aae76566300f300c2638ba
MD5 dfa97f9e962b263f1752be408c703161
BLAKE2b-256 e25f55185a7a2981fdf1ef0e25fe945eb4b87a2c42e2eb21ef0bdd1624d1428d

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 833191927ddc859ff90e5932ba2f2b8ad0d7c653d4d156344c86c64b76e75ff1
MD5 ab28fa0014038633aff0b9257fb102a1
BLAKE2b-256 f870a2ecdc3127153a08cd8651b61269c2a9002f9b54c7743ec5648fd4bd63c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d68ce693a4a437edf15c105436152f442ea701279fe95ea7471926d3681aa29c
MD5 54041e02c63ee367a7f1b6fd24c11c86
BLAKE2b-256 f795d1df821d48b1e23ccfe5f64803d3283286d38810d414266e87614869af33

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 005e4d74541497e0bd73ad0eea2ad301496c36b5edf0ae85365e8b2739568b5e
MD5 8da344b35e6826c75389c68e769d1216
BLAKE2b-256 6daf9756091a45ed87ce7e4f086674484093f7bfdf12cc0b07cfa3f3abe238cb

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4a7caacc3789ac1b1593e8fdddb96070278e1eaa3fc2af82de48acf50c2dbf56
MD5 df3d04813660a7cf91955bf59fa688d3
BLAKE2b-256 1c9d7e4e9d3ed514b885b718568f3756e3916d3412cffecb8e550389bbbb9581

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.1-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 19cbfd630ca9dadae2b116a93b5d83e3e5e7bc2daf22e7309af64adcf458c9df
MD5 894d431ab82b5abed94d294ee4efa42c
BLAKE2b-256 a06dcb135f2fa9997cf7f60d5c43d4425e11d47eb97dc6bd49294d92eaa7e501

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f33beb64351405ca6ec12e4c4d18140f3c1995dd504e4716ebebc5e4da75bc12
MD5 a0d28779d093d8a97f50386675cf4501
BLAKE2b-256 dc994901f2934896166af1dbeeb380aec7ea489962c607b26e804f03c2234bce

See more details on using hashes here.

Provenance

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

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for glmstar-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d2f676840f2950d5544798e0fb92a3d42494d51e78e1171ab21e62a56f371d6
MD5 a8b94d786786392bbb0c51e4a897d691
BLAKE2b-256 d9a45296e013b26eca0282434f606d04cbfb3db66618276163500cbaf01f40dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for glmstar-0.1.1-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: build_wheels.yml on jonathan-taylor/glmstar

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

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