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

Quick Start

Visit the marimo playground to try out the package.

Here a small code example:

import numpy as np
import polars as pl
from lpspline import l, pwl, bs, cs



# ---------------------------------------- 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', bias=True)
    + pwl(term='xpwl', knots=[5.])
    + bs(term="xbs", knots=np.linspace(0, 10, 5), degree=3)
    + cs(term="xcyc", period=2*np.pi, order=2)
    + f(term="xfactor", n_classes=3)
)

# ---------------------------------------- Model Fitting
model.fit(df, df["target"])

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

# ---------------------------------------- Model Visualization
plot_components(model=model, df=df, ncols=3)

Expected output

Once the model is fitted, you will see a detailed summary to the console:

==================================================
✨ Model Summary ✨
==================================================
Problem Status: ✅ optimal
--------------------------------------------------
Spline Type          | Term            | Params    
--------------------------------------------------
🟢 Linear            | linear_col      | 2         
🟢 PiecewiseLinear   | pwl_col         | 3         
🟢 BSpline           | bs_col          | 1         
🟢 CyclicSpline      | cyc_col         | 5         
🟢 Factor            | factor_col      | 3         
--------------------------------------------------
📊 Total Parameters                    | 14        
==================================================

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.5.tar.gz (14.9 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.5-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lpspline-0.0.5.tar.gz
  • Upload date:
  • Size: 14.9 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.5.tar.gz
Algorithm Hash digest
SHA256 a0ef7197c724e0cf393147f50e67107158376152e20b6e881e144c246764322c
MD5 348a6f6bc28e43de2c9ffddc5cd0b5ef
BLAKE2b-256 1a6789a7cd20c13d10645880737c6dc99d2ff24222bb20e98ab72788f7d4762d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lpspline-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 18.3 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1010d01719fa7859e9fb39a6bb4c004019db0dd5b6b465a53d2722d7ccf56995
MD5 1cf96b93dfcc0b0742f085efac1c7482
BLAKE2b-256 cf016982a44b6f0fe8f7fd1dc61b864bf3c16f91a74e616a9ef155c34106e92f

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