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:
- Multiple Linear Regression (
turtles.stats.glms.MLRclass) - Logistic Regression (
turtles.stats.glms.LogRegclass, usesGLMparent class) - Poisson Regression (
turtles.stats.glms.PoissonRegclass, usesGLMparent 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:
- The
fit()method parametersX,y, and (for Poisson)exposuremust benumpyarrays. Parametersyandexposuremust be of shape(M, 1), whereMis the number of rows in the data. The package does not supportpandasorpolarsdataframes at this time. See class / instance method docstrings for exact requirements. - Each GLM class has a
learning_rateparameter, 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). - 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:
- Support flattened arrays (in addition to (m, 1))
- Tweedie GLM
- Regularization methods
Metadata
Release files for turtles-glms 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| turtles_glms-1.1.3.tar.gz | 28.0 kB | Without provenance | Details for turtles_glms-1.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| turtles_glms-1.1.3-py3-none-any.whl | Python 3 | none | any | Details for turtles_glms-1.1.3-py3-none-any.whl |
Total release size: 55.5 kB
Release files / turtles_glms-1.1.3.tar.gz
| Download URL | turtles_glms-1.1.3.tar.gz |
|---|---|
| Size | 28.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0a517deefa72575f306f1a4d3d4d99969c06c8afccf504ba4a1280757d2336d4
|
|
BLAKE2b-256 checksum How to use checksums |
f1110dfcfc4e2c857df406ff045845f6f688f90e87ecdd316f334480d725028a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Release files / turtles_glms-1.1.3-py3-none-any.whl
| Download URL | turtles_glms-1.1.3-py3-none-any.whl |
|---|---|
| Size | 27.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
64837409cc1998b50df9ca82f62495154be79085b59528b7bfbab325194bc1e1
|
|
BLAKE2b-256 checksum How to use checksums |
999791b5ba3b852fd985c261d955f488e3cb0f275ec14fe6486656244eff327f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Security for turtles-glms 1.1.3
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.