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.

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
  • 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


# ---------------------------------------- Data Generation

n = 1000

# Regressors
x_linear = np.linspace(0, 10, n)
x_pwl = np.linspace(0, 10, n)
x_bs = np.linspace(0, 10, n)
x_cyc = np.linspace(0, 2*np.pi, n)
x_factor = np.random.randint(0, 3, n)

# Target
y_linear = 0.5 * x_linear
y_pwl = np.where(x_pwl < 5, 0, x_pwl - 5)
y_bs = np.sin(x_bs) 
y_cyc = np.cos(x_cyc)
y_factor = np.array([0, 2, -1])[x_factor]

y = y_linear + y_pwl + y_bs + y_cyc + y_factor + np.random.normal(0, 0.2, n)

df = pl.DataFrame({
    "xl": x_linear,
    "xpwl": x_pwl,
    "xbs": x_bs,
    "xcyc": x_cyc,
    "xfactor": x_factor,
    "target": y
})

# ---------------------------------------- Model Definition
model = (
    +l(term='xl')
    + pwl(term='xpwl', knots=3)
    + bs(term="xbs", knots=10, degree=2)
    + cs(term="xcyc", order=3)
    + f(term="xfactor")
)
# ---------------------------------------- Model Fitting
model.fit(df, df["target"])

# ---------------------------------------- Model Prediction
predictions = model.predict(df)

# ---------------------------------------- Model Visualization
plot_diagnostic(model=model, X=df, y=df['target'], 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                 | 2       
🟢 PiecewiseLinear   | xpwl         | pwl             | None                 | None                 | 5       
🟢 BSpline           | xbs          | bspline         | None                 | None                 | 11      
🟢 CyclicSpline      | xcyc         | cyclicspline    | None                 | None                 | 7       
🟢 Factor            | xfactor      | factor          | None                 | None                 | 5       
------------------------------------------------------------------------------------------------------------------------
📊 Total Parameters                                                                                 | 29
========================================================================================================================

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.0.7.tar.gz (16.1 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.0.7-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lpspline-0.0.7.tar.gz
Algorithm Hash digest
SHA256 18a5f4234820873ef7f69f33affbd8704b2d24db68a7b74df1e3d14d21ee6d02
MD5 e0a7bd41c653e3c5dc0f1f36242de9b0
BLAKE2b-256 353c3276cbdeeeebbf74eb020a151f379b169b99381b3abb4daecde9b138cc3b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lpspline-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 19.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.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d25d3c75f803110bdfb9de060a54554e749460ad542690c50f62f11146ba33a5
MD5 a3e8c4a9965999018fd4f73ca07cdd1f
BLAKE2b-256 24d50dfb275a57d9f66b1cb4d9d247ca41ec4c99d8d7580c30f3e31a17efdd59

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