Skip to main content

High performance Python GLMs with all the features!

Project description

glum

CI Docs Conda-forge PypiVersion PythonVersion

Documentation

Generalized linear models (GLM) are a core statistical tool that include many common methods like least-squares regression, Poisson regression and logistic regression as special cases. At QuantCo, we have used GLMs in e-commerce pricing, insurance claims prediction and more. We have developed glum, a fast Python-first GLM library. The development was based on a fork of scikit-learn, so it has a scikit-learn-like API. We are thankful for the starting point provided by Christian Lorentzen in that PR!

The goal of glum is to be at least as feature-complete as existing GLM libraries like glmnet or h2o. It supports

  • Built-in cross validation for optimal regularization, efficiently exploiting a “regularization path”
  • L1 regularization, which produces sparse and easily interpretable solutions
  • L2 regularization, including variable matrix-valued (Tikhonov) penalties, which are useful in modeling correlated effects
  • Elastic net regularization
  • Normal, Poisson, logistic, gamma, and Tweedie distributions, plus varied and customizable link functions
  • Box constraints, linear inequality constraints, sample weights, offsets

This repo also includes tools for benchmarking GLM implementations in the glum_benchmarks module. For details on the benchmarking, see here. Although the performance of glum relative to glmnet and h2o depends on the specific problem, we find that when N >> K (there are more observations than predictors), it is consistently much faster for a wide range of problems.

Performance benchmarks Performance benchmarks

For more information on glum, including tutorials and API reference, please see the documentation.

Why did we choose the name glum? We wanted a name that had the letters GLM and wasn't easily confused with any existing implementation. And we thought glum sounded like a funny name (and not glum at all!). If you need a more professional sounding name, feel free to pronounce it as G-L-um. Or maybe it stands for "Generalized linear... ummm... modeling?"

A classic example predicting housing prices

>>> from sklearn.datasets import fetch_openml
>>> from glum import GeneralizedLinearRegressor
>>>
>>> # This dataset contains house sale prices for King County, which includes
>>> # Seattle. It includes homes sold between May 2014 and May 2015.
>>> house_data = fetch_openml(name="house_sales", version=3, as_frame=True)
>>>
>>> # Use only select features
>>> X = house_data.data[
...     [
...         "bedrooms",
...         "bathrooms",
...         "sqft_living",
...         "floors",
...         "waterfront",
...         "view",
...         "condition",
...         "grade",
...         "yr_built",
...         "yr_renovated",
...     ]
... ].copy()
>>>
>>>
>>> # Model whether a house had an above or below median price via a Binomial
>>> # distribution. We'll be doing L1-regularized logistic regression.
>>> price = house_data.target
>>> y = (price < price.median()).values.astype(int)
>>> model = GeneralizedLinearRegressor(
...     family='binomial',
...     l1_ratio=1.0,
...     alpha=0.001
... )
>>>
>>> _ = model.fit(X=X, y=y)
>>>
>>> # .report_diagnostics shows details about the steps taken by the iterative solver.
>>> diags = model.get_formatted_diagnostics(full_report=True)
>>> diags[['objective_fct']]
        objective_fct
n_iter               
0            0.693091
1            0.489500
2            0.449585
3            0.443681
4            0.443498
5            0.443497
>>>
>>> # Models can also be built with formulas from formulaic.
>>> model_formula = GeneralizedLinearRegressor(
...     family='binomial',
...     l1_ratio=1.0,
...     alpha=0.001,
...     formula="bedrooms + np.log(bathrooms + 1) + bs(sqft_living, 3) + C(waterfront)"
... )
>>> _ = model_formula.fit(X=house_data.data, y=y)

Installation

Please install the package through conda-forge:

conda install glum -c conda-forge

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

glum-3.0.0.tar.gz (13.3 MB view hashes)

Uploaded Source

Built Distributions

glum-3.0.0-cp312-cp312-win_amd64.whl (530.5 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

glum-3.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

glum-3.0.0-cp312-cp312-macosx_11_0_arm64.whl (619.2 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

glum-3.0.0-cp312-cp312-macosx_10_9_x86_64.whl (992.1 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

glum-3.0.0-cp311-cp311-win_amd64.whl (521.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

glum-3.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

glum-3.0.0-cp311-cp311-macosx_11_0_arm64.whl (600.7 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

glum-3.0.0-cp311-cp311-macosx_10_9_x86_64.whl (948.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

glum-3.0.0-cp310-cp310-win_amd64.whl (520.6 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

glum-3.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

glum-3.0.0-cp310-cp310-macosx_11_0_arm64.whl (605.3 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

glum-3.0.0-cp310-cp310-macosx_10_9_x86_64.whl (949.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

glum-3.0.0-cp39-cp39-win_amd64.whl (521.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

glum-3.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

glum-3.0.0-cp39-cp39-macosx_11_0_arm64.whl (606.5 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

glum-3.0.0-cp39-cp39-macosx_10_9_x86_64.whl (950.9 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

Supported by

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