Skip to main content

lpspline

Project description

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 matplotlib and pimpmyplot

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.

LPSpline Visualization

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

lpspline-0.1.7.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

lpspline-0.1.7-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file lpspline-0.1.7.tar.gz.

File metadata

  • Download URL: lpspline-0.1.7.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for lpspline-0.1.7.tar.gz
Algorithm Hash digest
SHA256 f766028e9a654dbdddee33f9c788ae14ba7cfb1948153d99542b3179ab8377a5
MD5 453ac331d227f50333b9a6d30df52e3f
BLAKE2b-256 a94bad804744aac1a2e577998c73d6058c1fc672f0c690ed0e6a8d0ee6a45c74

See more details on using hashes here.

File details

Details for the file lpspline-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: lpspline-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 31.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for lpspline-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 8b30316dcc341af30007ec46fbb2712fba656d7ed638a508943c03e5f37fe7a9
MD5 ba9ed954762821382cdec08f5fe63a60
BLAKE2b-256 6152d4b7cc101a7c4058012e283fc1c884d1bd78e9ff3e019a86c1da66a19a11

See more details on using hashes here.

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