Skip to main content

pymgcv: Generalized additive models using mgcv, in Python.

Project description

pymgcv: Generalized Additive Models in Python

pymgcv provides a Pythonic interface to R's powerful mgcv library for fitting Generalized Additive Models (GAMs). It combines the flexibility and statistical rigor of mgcv with the convenience of Python's data science ecosystem.

Currently in development. As this is a multilanguage project (R and Python), we use pixi, a package management tool which supports this (via conda). For development, the pymgcv can be installed by installing pixi and running:

git clone https://github.com/danielward27/pymgcv.git
cd pymgcv
pixi shell --environment=dev

Installation options

Installing the python package only includes the python package and dependencies. This means an R installation with mgcv is also required. Conda and pixi provide two convenient options for handling both Python and R dependencies:

Using conda:

  • Install conda e.g. miniforge
  • Install uv (or use pip).
conda create --name my_env python r-base r-mgcv
conda activate my_env
uv pip install pymgcv

Using pixi:

pixi init
pixi add python r-base r-mgcv
pixi add --pypi pymgcv
pixi shell

Using either method the below example should now run e.g. in the terminal after running python, or in an IDE after selecting the pixi/conda environment.

Simple example

import pandas as pd
import numpy as np
from pymgcv.gam import GAM
from pymgcv.terms import S, T, L
from pymgcv.plot import plot_gam
import matplotlib.pyplot as plt

# Generate sample data with non-linear relationship
np.random.seed(42)
n = 100
x0 = np.random.uniform(-1, 1, n)
x1 = np.random.uniform(-1, 1, n)
y = 0.5 * x0 + np.sin(np.pi * x1) + np.random.normal(0, 0.5, n)
data = pd.DataFrame({'x0': x0, 'x1': x1, 'y': y})

# Define model: linear effect of x0, smooth function of x1
model = GAM({'y': L('x0') + S('x1')})

# Fit the model
model = model.fit(data)
plot_gam(fit=model, residuals=True)
plt.show()

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

pymgcv-0.0.1.tar.gz (348.3 kB view details)

Uploaded Source

Built Distribution

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

pymgcv-0.0.1-py3-none-any.whl (50.9 kB view details)

Uploaded Python 3

File details

Details for the file pymgcv-0.0.1.tar.gz.

File metadata

  • Download URL: pymgcv-0.0.1.tar.gz
  • Upload date:
  • Size: 348.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pymgcv-0.0.1.tar.gz
Algorithm Hash digest
SHA256 40ff93bfa458379519f1000a9b294e7c2163d8dcb03bb4013e15714c1754fab6
MD5 bf994ce4ea3fa6d5aed18c371bf64080
BLAKE2b-256 2ab25df083ca45e2bf9694efdf06011ed37aeab870000c1cb7c71f69730df0ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymgcv-0.0.1.tar.gz:

Publisher: publish.yml on danielward27/pymgcv

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

File details

Details for the file pymgcv-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pymgcv-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 50.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pymgcv-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b6131c712a07b45b64e9c2906c17c63e4512413ba5fb0f3c0f41f9a8c09a8050
MD5 8839577db4030dcc3fc3516ac4a75f4f
BLAKE2b-256 96a7791ed430a1f186148236d98a414d1213e19d76876ba3fa071d3bae8f26f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pymgcv-0.0.1-py3-none-any.whl:

Publisher: publish.yml on danielward27/pymgcv

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