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, 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.6.tar.gz (14.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.6-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lpspline-0.0.6.tar.gz
  • Upload date:
  • Size: 14.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.6.tar.gz
Algorithm Hash digest
SHA256 2d6b41371c72ad7b4a8da687b928728d113179a9a04b0d07b8098454fd907f74
MD5 8ff7f60417ee2145b52ae8cc15db2a13
BLAKE2b-256 6f9ba0124c5a4200633c7b7afbbfb1d15859adb5ec596c8c4082f4f1ecdadfe5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lpspline-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 18.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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 00423916c7e39136a5603806b23dd4771582b14c77858e62bdd9a4ef862896d1
MD5 9ceef5a58c282d824ba2015e6db1e858
BLAKE2b-256 d27fb0b6e249af204e744998e8b9003276b5a4a7cfdf7754068a7ae83830a5a5

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