LPSpline
LPSpline is a Python package for building and optimizing linear spline models using an intuitive additive API. It provides a flexible way to model non-linear relationships using various spline types like Piecewise Linear, B-Splines, Cyclic Splines, and Categorical Factors.
Documentation
Visit the documentation for a complete overview and some examples with plots.
Installation
Install lpspline via pip directly from the repository, or if published:
pip install lpspline
Main Features
- Additive model definition
- CVXPY backend for optimization
- Multiple spline types: Linear, Piecewise Linear, B-Splines, Cyclic Splines, Categorical Factors, Constant
- Penalties on the splines: Ridge, Lasso
- Constraints on the splines: Monotonic, Convex, Concave, Anchor, Bound
- Save and load models with a single line of code
- Polars DataFrame integration
- Nice plots using
matplotlibandpimpmyplot
Sandbox
Visit the marimo playground for a live demo
Quick Start
Here a small code example:
import numpy as np
import polars as pl
from lpspline import l, pwl, bs, cs
from lpspline.viz import plot_diagnostic
from lpspline.datasets import load_demo_dataset
# ---------------------------------------- Data Generation
X, y = load_demo_dataset(samples = 1000)
# ---------------------------------------- Model Definition
model = (
+l(term='xl', by='xfactor')
+ pwl(term='xpwl', knots=3)
+ bs(term="xbs", knots=10, degree=2)
+ cs(term="xcyc", order=3)
+ f(term="xfactor")
)
# ---------------------------------------- Model Fitting
model.fit(X, y)
# ---------------------------------------- Model Prediction
predictions = model.predict(X)
# ---------------------------------------- Model Visualization
plot_diagnostic(model=model, X=X, y=y, ncols=3)
Expected output
Once the model is fitted, you will see a detailed summary to the console and a diagnostic plot showing the fitted splines.
========================================================================================================================
✨ Model Summary ✨
========================================================================================================================
Problem Status: ✅ optimal
------------------------------------------------------------------------------------------------------------------------
Spline Type | Term | Tag | Constraints | Penalties | Params
------------------------------------------------------------------------------------------------------------------------
🟢 Linear | xl | linear | None | None | 6
🟢 PiecewiseLinear | xpwl | pwl | None | None | 5
🟢 BSpline | xbs | bspline | None | None | 11
🟢 CyclicSpline | xcyc | cyclicspline | None | None | 7
🟢 Factor | xfactor | factor | None | None | 3
------------------------------------------------------------------------------------------------------------------------
📊 Total Parameters | 32
========================================================================================================================
Model fitted successfully.
Metadata
Release files for lpspline 0.1.13
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 | |
|---|---|---|---|---|
| lpspline-0.1.13.tar.gz | 30.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lpspline-0.1.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 67.5 kB
Release files / lpspline-0.1.13.tar.gz
| Download URL | lpspline-0.1.13.tar.gz |
|---|---|
| Size | 30.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d567401b33d4f4c7421d1b62ac9e5479bba5bfb92bff5e530f48778f85eace70
|
|
BLAKE2b-256 checksum How to use checksums |
f64f732ba9124cd50ba1224e7308a47566c4d10c835bdf10b8dea09f676e921f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.9
|
Release files / lpspline-0.1.13-py3-none-any.whl
| Download URL | lpspline-0.1.13-py3-none-any.whl |
|---|---|
| Size | 36.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c96094559ab6a48a19e158c33ed5bbd292272a352700adc91ec80cd35e8e9079
|
|
BLAKE2b-256 checksum How to use checksums |
f979c5dd8d0c925147ffe33484188a9bd8129b38e816e090013215f85b092af7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.9
|
Security for lpspline 0.1.13
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.