piecewise-regression
piecewise (segmented) regression in python
What it is and what it does
Piecewise-regression implements Muggeo's iterative algorithm to fit piecewise linear models—straight lines with one or more breakpoints where the slope changes. You provide x and y data plus either initial breakpoint guesses or a desired number of breakpoints, and the package estimates the segment gradients, breakpoint positions, and confidence intervals for all parameters. It includes a Davies test to assess whether breakpoints exist statistically.
The package uses bootstrap restarting to escape local optima and improve the chance of finding a global solution, though convergence is not guaranteed. It also offers a ModelSelection tool based on Bayesian Information Criterion (BIC) to compare models with different numbers of breakpoints. Results can be extracted as structured data or plotted with matplotlib for visualization of the fit, breakpoints, and confidence bands.
Use it for:
- Detect and quantify shifts in trend in time-series data, such as changes in growth rate before and after a policy intervention.
- Identify optimal threshold points in dose-response or dose-effect studies where biological or physical response changes slope.
- Analyze piecewise relationships in economic or environmental data where structural breaks occur at known or unknown transition points.
- Compare competing segmented models using BIC to select the number of breakpoints that best explains the data.
- Extract confidence intervals around estimated breakpoint positions for hypothesis testing or uncertainty quantification.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Fits piecewise linear regression models to data with one or more breakpoints, estimating segment gradients and breakpoint positions with confidence intervals.
Yes, if you need to fit piecewise linear models with statistical inference. The package is well-maintained enough (last commit mid-2024), has no known vulnerabilities, and low install friction. Dormant status is not a blocker for a stable, narrowly-scoped tool. Install with caution if you require active development or frequent updates; for one-off analysis or established workflows, it is reliable.
Install
piecewise-regression on PyPI
pip
pip install piecewise-regressionuv
uv add piecewise-regressionpoetry
poetry add piecewise-regressionInstalling piecewise-regression
Before you install
Low install friction with a pure Python wheel and four standard scientific dependencies (numpy, scipy, matplotlib, statsmodels). Maintenance is dormant—last release was 2023-12-18 and last commit 2024-06-09—but the repository remains active and unarchived.
License in practice
MIT license is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.
Quickstart
pip install piecewise-regression
import piecewise_regression
import numpy as np
x = np.linspace(0, 20, 100)
y = 100 - 4*x + 2*np.maximum(x - 7, 0) + np.random.normal(size=100)
pw_fit = piecewise_regression.Fit(x, y, n_breakpoints=1)
pw_fit.summary()
Verify before relying
- Whether the package handles edge cases (e.g., collinear data, very few observations) gracefully or requires specific data characteristics.
- Performance characteristics on large datasets or high-dimensional breakpoint searches.
- Whether bootstrap restarting convergence guarantees improve with specific parameter tuning beyond n_boot.
Package facts
| License | MIT (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — numpy, matplotlib, scipy, statsmodels |
| Maintenance | dormant — 970 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 133,799/month — #11,495 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: piecewise_regression-1.5.0-py3-none-any.whl
Tags
More Mathematics packages
NetworkX provides data structures and…
permissive · top 1,000 on PyPI
kiwisolverkiwisolver is a Python binding to a fast C++…
permissive · top 1,000 on PyPI
sympySymPy is a Python library for symbolic…
permissive · top 1,000 on PyPI
contourpyContourPy calculates contours of 2D…
permissive · top 1,000 on PyPI
torchPyTorch provides GPU-accelerated tensor…
permissive · top 1,000 on PyPI
onnxruntimeonnxruntime loads and executes Open Neural…
permissive · top 1,000 on PyPI
aplrAPLR builds interpretable regression and…
permissive · top 15,000 on PyPI
pwlfFits continuous piecewise linear functions to…
permissive · top 15,000 on PyPI
ropwrRoPWR computes optimal piecewise polynomial…
permissive · top 15,000 on PyPI
bootstrappedBuilds confidence intervals from data samples…
unclear · top 15,000 on PyPI
lmfitLmfit provides non-linear least-squares…
permissive · top 5,000 on PyPI
linearmodelsProvides panel regression, instrumental…
unclear · top 15,000 on PyPI
piecewise-rationalPerforms shape-preserving piecewise rational…
permissive · top 15,000 on PyPI
forestciComputes confidence intervals and variance…
permissive · top 15,000 on PyPI
powerlawFits heavy-tailed distributions like power laws…
permissive · top 15,000 on PyPI
betacalFits beta calibration models to classifier…
permissive · top 15,000 on PyPI