Skip to main content

CI coverage Python License PyPI

PyGLMs

An implementation of various Generalized Linear Models (GLMs), written in Python.

GLMs are an excellent foundation for predictive modeling and machine learning, so the code is intentionally written for clarity to help other readers and developers understand the underlying mathematics. Building this package also served as my own personal refresher on the topic.

Overview

The code is packaged as a Python library named turtles-glms (I like turtles).

It is written using numpy for linear algebra operations, scipy for (some) optimization, pandas for displaying tabular results, and matplotlib for plots.

The following model frameworks have been implemented:

  1. Multiple Linear Regression (turtles.stats.glms.MLR class)
  2. Logistic Regression (turtles.stats.glms.LogReg class, uses GLM parent class)
  3. Poisson Regression (turtles.stats.glms.PoissonReg class, uses GLM parent class)

The GLM parent class supports three optimization methods for parameter estimation: Momentum-based Gradient Descent for first-order optimization, Newton's Method for second-order optimization, and Limited-memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS). The user can specify the desired optimization method during class instantiation.

Momentum-based Gradient Descent and Newton's Method are implemented in Python as part of the code base. L-BFGS is implemented using scipy.optimize; it's a quasi-Newton method that approximates the Hessian (instead of fully computing it, like Newton's Method), so it's quite fast.

Usage

You can pip install the package from PyPI:

pip install turtles-glms

See examples/ in the GitHub repo for example usage of the GLM classes and statistical functions.

Fitting GLMs

You can fit GLMs by instantiating a GLM child class and calling its fit() method.

model = PoissonReg(
    method="newton",
    learning_rate=1.0,
    tolerance=0.00001
)
model.fit(
    X=X, 
    y=y, 
    exposure=exposure
)

A few important notes about fitting turtles GLMs:

  1. The fit() method parameters X, y, and (for Poisson) exposure must be numpy arrays. Parameters y and exposure must be of shape (M, 1), where M is the number of rows in the data. The package does not support pandas or polars dataframes at this time. See class / instance method docstrings for exact requirements.
  2. Each GLM class has a learning_rate parameter, applicable to Gradient Descent and Newton's optimization methods. The learning rate (or step size) is a hyperparameter that controls the magnitude of parameter updates during the optimization process. If it's too large, the Hessian matrix may become singular, in which case the learning rate should be decreased. This is typically part of the tuning process. (NOTE: learning rate is typically just 1.0 for Newton's method).
  3. There are currently no regularization methods implemented in the package. Future versions may include L1, L2, and Elastic Net methods.

Unreleased

Some future updates I'd like to make:

  1. Support flattened arrays (in addition to (m, 1))
  2. Tweedie GLM
  3. Regularization methods

Metadata

Release files for turtles-glms 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for turtles-glms 1.1.4
File Size Uploaded Provenance Actions
turtles_glms-1.1.4.tar.gz 386.9 kB Without provenance Details for turtles_glms-1.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for turtles-glms 1.1.4
File Interpreter ABI Platform Actions
turtles_glms-1.1.4-py3-none-any.whl Python 3 none any Details for turtles_glms-1.1.4-py3-none-any.whl

Total release size: 414.7 kB

Release files / turtles_glms-1.1.4.tar.gz

Download URL turtles_glms-1.1.4.tar.gz
Size 386.9 kB
Tags Source
SHA-256 checksum
How to use checksums
255a595cf8084cfe60b77ac0b1ffefe0b515757dc77e55c451ef1b6b36005d9d
BLAKE2b-256 checksum
How to use checksums
514a745b2d2c6001fe567a352da8f8b1d36c3d19e749556b22f48e4a1e49c7b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Release files / turtles_glms-1.1.4-py3-none-any.whl

Download URL turtles_glms-1.1.4-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27490e1f62333810a3107f0294fdd646cc1efef61e0610df01f0217872ea6b27
BLAKE2b-256 checksum
How to use checksums
a3304d0f9addc7dcf7b856afbd81ac9204004bdcbe4eeb7201071354e5110e71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Security for turtles-glms 1.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page