pwlf
fit piecewise linear functions to data
What it is and what it does
pwlf is a Python library for fitting continuous piecewise linear functions to data using least squares optimization. You specify how many line segments you want, provide your x-y data, and the library finds the optimal breakpoint locations and segment slopes that minimize residuals. It uses scipy's differential evolution algorithm by default for global optimization, but also offers a faster multi-start gradient-based approach and supports custom optimization routines.
The library handles several fitting scenarios: automatic breakpoint discovery for a specified number of segments, fitting with known breakpoint locations, and weighted regression. It depends on numpy and scipy for numerical computation and linear algebra. The underlying mathematics are based on least squares formulation with global optimization to locate breakpoints, making it suitable for data that exhibits piecewise-linear behavior across different regions.
Use it for:
- Detect and model regime changes in time-series data where behavior shifts between distinct linear trends
- Approximate nonlinear curves with continuous piecewise linear segments for interpretability and computational efficiency
- Fit material stress-strain curves or other physical phenomena exhibiting multiple linear regions with known transition points
- Perform change-point analysis in experimental or observational data to identify where relationships between variables shift
- Build reduced-order models of complex systems by segmenting the input domain and fitting linear approximations per segment
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Fits continuous piecewise linear functions to data by specifying the number of line segments and providing x-y data, with support for weighted fits and multiple optimization strategies.
Yes. The package is actively maintained, has low install friction, carries no known vulnerabilities, and uses a permissive MIT License. It solves a specific numerical problem—fitting piecewise linear functions—with a clean API and multiple optimization strategies. Install it if you need to model data with distinct linear regimes or approximate nonlinear relationships with continuous piecewise linear segments.
Install
pwlf on PyPI
pip
pip install pwlfuv
uv add pwlfpoetry
poetry add pwlfInstalling pwlf
Before you install
Low friction install with only numpy and scipy as runtime dependencies. Actively maintained with a recent release 28 days ago and 352 repository stars.
License in practice
MIT License permits unrestricted use, modification, and distribution in proprietary and open-source projects without attribution requirement.
Quickstart
pip install pwlf
import pwlf
my_pwlf = pwlf.PiecewiseLinFit(x_data, y_data)
my_pwlf.fit(number_of_segments)
Verify before relying
- Whether the library handles edge cases like constant-valued segments or single-point data gracefully
- Performance characteristics on datasets with very large numbers of observations
- Whether weighted fits support negative or zero weights and how they are normalized
Package facts
| License | MIT License (permissive) |
| Python support | supports the current Python release (>3.5) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — numpy, scipy |
| Maintenance | actively maintained — 28 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 265,750/month — #8,315 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pwlf-2.6.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
lmfitLmfit provides non-linear least-squares…
permissive · top 5,000 on PyPI
piecewise-regressionFits piecewise linear regression models to data…
permissive · top 15,000 on PyPI
ropwrRoPWR computes optimal piecewise polynomial…
permissive · top 15,000 on PyPI
aplrAPLR builds interpretable regression and…
permissive · top 15,000 on PyPI
spglmFits Gaussian, Poisson, QuasiPoisson, and…
permissive · top 15,000 on PyPI
linearmodelsProvides panel regression, instrumental…
unclear · top 15,000 on PyPI
powerlawFits heavy-tailed distributions like power laws…
permissive · top 15,000 on PyPI
polars-olsPolars OLS provides Rust-optimized linear…
unclear · top 15,000 on PyPI
piecewise-rationalPerforms shape-preserving piecewise rational…
permissive · top 15,000 on PyPI
lineaxLineax solves linear systems and least-squares…
permissive · top 15,000 on PyPI