Piecewise Regression

1.5.0 · active · verified Fri Apr 17

Piecewise-regression is a Python library for performing segmented (or piecewise) regression analysis. It allows fitting models with one or more breakpoints, identifying where the relationship between independent and dependent variables changes. The current version is 1.5.0, with a moderate release cadence focused on statistical improvements and convenience features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate synthetic data with a breakpoint, initialize the `PiecewiseRegression` model, fit it to the data, and then visualize the results. It highlights the core workflow from data preparation to model interpretation.

import numpy as np
from piecewise_regression import PiecewiseRegression
import matplotlib.pyplot as plt

# Generate some synthetic data with a known breakpoint
x = np.linspace(0, 100, 500)
y = np.concatenate([
    -0.1 * x[x <= 50] + 10,
    0.5 * x[x > 50] - 20
]) + np.random.normal(0, 1, 500)

# Initialize and fit the model with one breakpoint
pw_fit = PiecewiseRegression(x, y, n_breakpoints=1)
pw_fit.fit()

# Print a summary of the fit
pw_fit.summary()

# Plot the fit (requires matplotlib to show)
fig, ax = pw_fit.plot_fit(show=False)
ax.set_title('Piecewise Regression Fit')
plt.show()

view raw JSON →