🌐
Anaconda.org
anaconda.org › conda-forge › pwlf
Pwlf - conda-forge
conda-forge/pwlf · Community · fit piecewise linear data for a specified number of line segments · Copied fromcf-staging / pwlf · Overview · Files 261 · Labels 1 · Badges · Versions · 2.5.1 · To install this package, run one of the following: $conda install conda-forge::pwlf ·
🌐
GitHub
github.com › conda-forge › pwlf-feedstock
GitHub - conda-forge/pwlf-feedstock: A conda-smithy repository for pwlf.
Installing pwlf from the conda-forge channel can be achieved by adding conda-forge to your channels with:
Forked by 4 users
🌐
PyPI
pypi.org › project › pwlf
pwlf · PyPI
python -m pip install pwlf · If you have conda, you can also install from conda-forge. conda install -c conda-forge pwlf · Or clone the repo · git clone https://github.com/cjekel/piecewise_linear_fit_py.git · then install with pip · python ...
      » pip install pwlf
    
Published   Jul 26, 2025
Version   2.5.2
People also ask

What is pwlf?
fit piecewise linear functions to data. Visit Snyk Advisor to see a · full health score report · for pwlf, including popularity, security, maintenance · & community analysis.
🌐
snyk.io
snyk.io › advisor › python packages › pwlf
pwlf - Python Package Health Analysis | Snyk
Is pwlf popular?
The python package pwlf receives a total · of 28,236 weekly downloads. As · such, pwlf popularity was classified as · a · recognized. Visit the · popularity section · on Snyk Advisor to see the full health analysis.
🌐
snyk.io
snyk.io › advisor › python packages › pwlf
pwlf - Python Package Health Analysis | Snyk
Is pwlf well maintained?
We found that pwlf demonstrated a · healthy version release cadence and project · activity. It has a community of · 10 open source contributors · collaborating on the project. See the full · package health analysis · to learn more about the package maintenance status.
🌐
snyk.io
snyk.io › advisor › python packages › pwlf
pwlf - Python Package Health Analysis | Snyk
🌐
GitHub
github.com › conda-forge › pwlf-feedstock › blob › main › README.md
pwlf-feedstock/README.md at main · conda-forge/pwlf-feedstock
Installing `pwlf` from the `conda-forge` channel can be achieved by adding `conda-forge` to your channels with: · ``` conda config --add channels conda-forge · conda config --set channel_priority strict · ``` · Once the `conda-forge` channel ...
Author   conda-forge
🌐
Libraries.io
libraries.io › conda › pwlf
pwlf 2.2.1 on conda - Libraries.io - security & maintenance data for open source software
python -m pip install pwlf · If you have conda, you can also install from conda-forge. conda install -c conda-forge pwlf · Or clone the repo · git clone https://github.com/cjekel/piecewise_linear_fit_py.git · then install with pip · python ...
🌐
Jekel
jekel.me › piecewise_linear_fit_py › installation.html
Installation — pwlf 2.5.2 documentation
python -m pip install pwlf · If you have conda, you can also install from conda-forge. conda install -c conda-forge pwlf · Or clone the repo · git clone https://github.com/cjekel/piecewise_linear_fit_py.git · then install with pip · python -m pip install ./piecewise_linear_fit_py ·
🌐
Jekel
jekel.me › piecewise_linear_fit_py
pwlf: piecewise linear fitting — pwlf 2.5.2 documentation
pwlf.PiecewiseLinFit · PiecewiseLinFit · About · Requirements · License · Index · Search Page · Installation · How it works · Examples · pwlf package contents · About · Requirements · License · Documentation overview · Next: Installation · ©2024, Charles Jekel.
🌐
Pydigger
pydigger.com › pypi › pwlf
pwlf
June 26, 2020 - :: conda install -c conda-forge pwlf From source ----------- Or clone the repo :: git clone https://github.com/cjekel/piecewise_linear_fit_py.git then install with pip :: python -m pip install ./piecewise_linear_fit_py How it works ============ This `paper <https://github.com/cjekel/piecew...
🌐
Snyk
snyk.io › advisor › python packages › pwlf
pwlf - Python Package Health Analysis | Snyk
python -m pip install pwlf · If you have conda, you can also install from conda-forge. conda install -c conda-forge pwlf · Or clone the repo · git clone https://github.com/cjekel/piecewise_linear_fit_py.git · then install with pip · python ...
Find elsewhere
🌐
GitHub
github.com › cjekel › piecewise_linear_fit_py
GitHub - cjekel/piecewise_linear_fit_py: fit piecewise linear data for a specified number of line segments · GitHub
python -m pip install pwlf · If you have conda, you can also install from conda-forge. conda install -c conda-forge pwlf · Or clone the repo · git clone https://github.com/cjekel/piecewise_linear_fit_py.git · then install with pip · python ...
Starred by 348 users
Forked by 65 users
Languages   Python
🌐
Anaconda.org
anaconda.org › conda-forge › pwlf › files
Files | Anaconda.org
fit piecewise linear data for a specified number of line segments copied from cf-staging / pwlf · Conda · Files · Labels · Badges · No files were selected × · Filters · Type: All · All · conda · Version: All · All · 2.5.1 · 2.5.0 · 2.4.0 · 2.3.0 · 2.2.1 ·
🌐
Libraries.io
libraries.io › pypi › pwlf
pwlf 2.4.0 on PyPI - Libraries.io - security & maintenance data for open source software
python -m pip install pwlf · If you have conda, you can also install from conda-forge. conda install -c conda-forge pwlf · Or clone the repo · git clone https://github.com/cjekel/piecewise_linear_fit_py.git · then install with pip · python ...
🌐
GitHub
github.com › conda-forge › pwlf-feedstock › issues
conda-forge/pwlf-feedstock
A conda-smithy repository for pwlf. Contribute to conda-forge/pwlf-feedstock development by creating an account on GitHub.
Author   conda-forge
🌐
FreshPorts
freshports.org › math › py-pwlf
FreshPorts -- math/py-pwlf: Fit piecewise linear functions to data
pwlf is a Python library for fitting continuous piecewise linear functions to data. Just specify the number of line segments you desire and provide the data.
🌐
GitHub
github.com › PanPalitta › piecewise_analysis
GitHub - PanPalitta/piecewise_analysis: Python script for fitting and comparing piecewise linear models
The script uses piecewise regression library "pwlf.py" by Charles Jekel for 3-segment regression. To install this library, follow the instruction on Charles's blog and copy the "pwlf.py" file to the same folder as "piecewise_analysis.py".
Author   PanPalitta
Top answer
1 of 12
88

You can use numpy.piecewise() to create the piecewise function and then use curve_fit(), Here is the code

from scipy import optimize
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15], dtype=float)
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 98.36, 112.25, 126.14, 140.03])

def piecewise_linear(x, x0, y0, k1, k2):
    return np.piecewise(x, [x < x0], [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0])

p , e = optimize.curve_fit(piecewise_linear, x, y)
xd = np.linspace(0, 15, 100)
plt.plot(x, y, "o")
plt.plot(xd, piecewise_linear(xd, *p))

the output:

For an N parts fitting, please reference segments_fit.ipynb

2 of 12
39

You can use pwlf to perform continuous piecewise linear regression in Python. This library can be installed using pip.

There are two approaches in pwlf to perform your fit:

  1. You can fit for a specified number of line segments.
  2. You can specify the x locations where the continuous piecewise lines should terminate.

Let's go with approach 1 since it's easier, and will recognize the 'gradient change point' that you are interested in.

I notice two distinct regions when looking at the data. Thus it makes sense to find the best possible continuous piecewise line using two line segments. This is approach 1.

import numpy as np
import matplotlib.pyplot as plt
import pwlf

x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59,
              84.47, 98.36, 112.25, 126.14, 140.03])

my_pwlf = pwlf.PiecewiseLinFit(x, y)
breaks = my_pwlf.fit(2)
print(breaks)

[ 1. 5.99819559 15. ]

The first line segment runs from [1., 5.99819559], while the second line segment runs from [5.99819559, 15.]. Thus the gradient change point you asked for would be 5.99819559.

We can plot these results using the predict function.

x_hat = np.linspace(x.min(), x.max(), 100)
y_hat = my_pwlf.predict(x_hat)

plt.figure()
plt.plot(x, y, 'o')
plt.plot(x_hat, y_hat, '-')
plt.show()

🌐
ResearchGate
researchgate.net › publication › 331231072_pwlf_A_Python_Library_for_Fitting_1D_Continuous_Piecewise_Linear_Functions
(PDF) pwlf: A Python Library for Fitting 1D Continuous Piecewise Linear Functions
February 20, 2019 - A brief overview of the pwlf library is provided. This information includes · installation details, versioning semantics, and details about the tting class.
🌐
GitHub
github.com › cjekel › piecewise_linear_fit_py › blob › master › pwlf › pwlf.py
piecewise_linear_fit_py/pwlf/pwlf.py at master · cjekel/piecewise_linear_fit_py
fit piecewise linear data for a specified number of line segments - piecewise_linear_fit_py/pwlf/pwlf.py at master · cjekel/piecewise_linear_fit_py
Author   cjekel