Generalized Additive Models in Python (pyGAM)

0.12.0 · active · verified Wed Apr 15

pyGAM is a Python library for building Generalized Additive Models (GAMs), emphasizing modularity and performance. It extends generalized linear models by allowing non-linear functions of features using penalized B-splines while maintaining additivity, making models both flexible and interpretable. The current stable version is 0.12.0. It is actively maintained with a focus on compatibility and contributions are welcome.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install pyGAM, load an example dataset, define a LinearGAM with spline and factor terms, fit the model to the data, and then print a statistical summary. It also includes a basic prediction example.

import numpy as np
from pygam import LinearGAM, s, f
from pygam.datasets import wage

X, y = wage() # Load example data

# Define a GAM with a spline term for features 0 and 1, and a factor term for feature 2
gam = LinearGAM(s(0) + s(1) + f(2))

# Fit the model
gam.fit(X, y)

# Print a summary of the model fit
print(gam.summary())

# Example of predicting (using dummy data for simplicity)
dummy_X = np.array([[10, 20, 1], [15, 25, 0]])
predictions = gam.predict(dummy_X)
print(f"Predictions for dummy data: {predictions}")

view raw JSON →