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

LPSpline allows you to easily compose additive models. Here's a quick example:

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



# ---------------------------------------- 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({
    "linear_col": x_linear,
    "pwl_col": x_pwl,
    "bs_col": x_bs,
    "cyc_col": x_cyc,
    "factor_col": x_factor,
    "target": y
})

# ---------------------------------------- Model Definition
model = (
    l(term='linear_col', bias=True)
    + pwl(term='pwl_col', knots=[5.])
    + bs(term="bs_col", knots=np.linspace(0, 10, 5), degree=3)
    + cs(term="cyc_col", period=2*np.pi, order=2)
    + f(term="factor_col", 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.3.tar.gz (11.7 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.3-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lpspline-0.0.3.tar.gz
  • Upload date:
  • Size: 11.7 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.3.tar.gz
Algorithm Hash digest
SHA256 f633cd817b50561c3866e88ee8be32945495bd68b7e5f21ab9f4b2cac3758819
MD5 966cefbd8476d249b70fdeaab3fddea7
BLAKE2b-256 1bd8dc0ec42c2790db65c3e4443f2c76e73bd1f475bc6a9b36ab7fc7fcd761e4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lpspline-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.5 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c8fceb1cf2033425fd8756c5fd3918a7d093e3a6d52d689479e4c6343b7efb1e
MD5 99cb1376e78b499a40f8c97cf0fb0f43
BLAKE2b-256 fe71a992ec0b7578785f4a6059e4de5d135acd963a73a0427d8f1e989e131db7

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