GLUM - High Performance Generalized Linear Models

3.3.0 · active · verified Thu Apr 16

glum is a Python library providing high-performance implementations of Generalized Linear Models (GLMs), including various distributions and link functions. It focuses on speed and feature richness, supporting regularized fitting (L1, L2, ElasticNet) and cross-validation. The current version is 3.3.0, and it maintains an active release cadence with multiple updates throughout the year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fit a Poisson GLM with a log link using `glum.GeneralizedLinearRegressor`. It generates synthetic data, then initializes and fits the model, finally printing the intercept, coefficients, deviance, and predictions for the first few samples.

import numpy as np
import pandas as pd
from glum import GeneralizedLinearRegressor, PoissonDistribution, LogLink

# Generate some synthetic data
np.random.seed(42)
n_samples = 100
X = pd.DataFrame({
    'feature_1': np.random.rand(n_samples) * 10,
    'feature_2': np.random.rand(n_samples) * 5
})

# True coefficients
beta_0 = 1.0
beta_1 = 0.5
beta_2 = 0.2

# Generate Poisson-distributed target variable using a log link
linear_predictor = beta_0 + beta_1 * X['feature_1'] + beta_2 * X['feature_2']
mu = np.exp(linear_predictor)
y = np.random.poisson(mu)

# Create and fit the GLM
glm = GeneralizedLinearRegressor(
    distribution=PoissonDistribution(), 
    link=LogLink(),
    fit_intercept=True
)
glm.fit(X, y)

print(f"Intercept: {glm.intercept_:.4f}")
print(f"Coefficients: {glm.coef_}")
print(f"Deviance: {glm.deviance_:.4f}")
print("Predictions (first 5 samples):\n", glm.predict(X.head()).round(2))

view raw JSON →