Skip to main content

fit piecewise linear functions to data

Project description

About

A library for fitting continuous piecewise linear functions to data. Just specify the number of line segments you desire and provide the data.

Downloads a month pwlf ci codecov PyPI version Conda

Check out the documentation!

Read the blog post.

Example of a continuous piecewise linear fit to data.

Example of a continuous piecewise linear fit to data.

Example of a continuous piecewise linear fit to a sine wave.

Example of a continuous piecewise linear fit to a sine wave.

Now you can perform segmented constant fitting and piecewise polynomials! Example of multiple degree fits to a sine wave.

Features

For a specified number of line segments, you can determine (and predict from) the optimal continuous piecewise linear function f(x). See this example.

You can fit and predict a continuous piecewise linear function f(x) if you know the specific x locations where the line segments terminate. See this example.

If you want to pass different keywords for the SciPy differential evolution algorithm see this example.

You can use a different optimization algorithm to find the optimal location for line segments by using the objective function that minimizes the sum of square of residuals. See this example.

Instead of using differential evolution, you can now use a multi-start gradient optimization with fitfast() function. You can specify the number of starting points to use. The default is 2. This means that a latin hyper cube sampling (space filling DOE) of 2 is used to run 2 L-BFGS-B optimizations. See this example which runs fit() function, then runs the fitfast() to compare the runtime differences!

Installation

Python Package Index (PyPI)

You can now install with pip.

python -m pip install pwlf

Conda

If you have conda, you can also install from conda-forge.

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 explains how this library works in detail.

This is based on a formulation of a piecewise linear least squares fit, where the user must specify the location of break points. See this post which goes through the derivation of a least squares regression problem if the break point locations are known. Alternatively check out Golovchenko (2004).

Global optimization is used to find the best location for the user defined number of line segments. I specifically use the differential evolution algorithm in SciPy. I default the differential evolution algorithm to be aggressive, and it is probably overkill for your problem. So feel free to pass your own differential evolution keywords to the library. See this example.

Changelog

All changes now stored in CHANGELOG.md

New weights= keyword allows you to perform weighted pwlf fits! Removed TensorFlow code which can now be found here.

Requirements

NumPy >= 1.14.0

SciPy >= 1.8.0

License

MIT License

Citation

@Manual{pwlf,
author = {Jekel, Charles F. and Venter, Gerhard},
title = {{pwlf:} A Python Library for Fitting 1D Continuous Piecewise Linear Functions},
year = {2019},
url = {https://github.com/cjekel/piecewise_linear_fit_py}
}

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

pwlf-2.5.3.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pwlf-2.5.3-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file pwlf-2.5.3.tar.gz.

File metadata

  • Download URL: pwlf-2.5.3.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for pwlf-2.5.3.tar.gz
Algorithm Hash digest
SHA256 ad5779b835ee7057d5675e8e5653ae18f32d084f75ba342f95bd2a100ed89c7b
MD5 20626f79515e1b95b605a9993c677dce
BLAKE2b-256 1a5396b03a5b540c6e7734ca32b408f927fb33be23a99eafc2b7d350da421083

See more details on using hashes here.

File details

Details for the file pwlf-2.5.3-py3-none-any.whl.

File metadata

  • Download URL: pwlf-2.5.3-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for pwlf-2.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c2f5bab57186e2bae7b87febb7b59498863b033bfa63fbb2ad90ac6d68f0ec1b
MD5 0dd6c6ebd9fa0a8757417c242bab21e0
BLAKE2b-256 a053f6cf58a4771e34b8c7f68ab6ef783c385f040ba0e1de38f8275dd7a84f10

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