Skip to main content

fit piece-wise linear function to data

Project description

About

A library for fitting a continuous piecewise linear function f(x) to data. Just specify the number of line segments you desire and your data set.

Check out the examples!

Read the blog post.

Example of a continuous piecewise linear fit to a data set.

Example of a continuous piecewise linear fit to a data set.

Example of a continuous piecewise linear fit to a sin wave

Example of a continuous piecewise linear fit to a sin 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.

Installation

You can now install with pip.

sudo pip install pwlf

Or clone the repo

git clone https://github.com/cjekel/piecewise_linear_fit_py.git

then install with pip

sudo pip install piecewise_linear_fit_py/

or easy_install

sudo easy_install piecewise_linear_fit_py/

or using setup.py

cd piecewise_linear_fit_py/
sudo python setup.py install

How it works

This is based on a formulation of a piecewise linear least squares fit, where the user must specify the location of break points. A simple derivation of this fit has been done by Golovchenko (2004). The routine for fitting the piecewise linear function is based on Golovchenko’s MATLAB code (which I can’t seem to find on the internet), which I ported to Python. Alternatively you can view this code.

Global optimization is used to find the best location for the user defined number of lline 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.

Why

All other methods require the user to specify the specific location of break points, but in most cases the best location for these break points is unknown. It makes more sense to rather have the user specify the desired number of line segments, and then to quantitatively choose the best location for the ends of these line segments.

Changelog

  • 2017/11/03 add setup.py, new tests folder and test scripts, new version tracking, initialize break0 breakN in the beginning

  • 2017/10/31 bug fix related to the case where break points exactly equal to x data points ( as per issue https://github.com/cjekel/piecewise_linear_fit_py/issues/1 ) and added attributes .sep_data_x, .sep_data_y, .sep_predict_data_x for troubleshooting issues related to the separation of data points to their respective regions

  • 2017/10/20 remove determinant calculation and use try-except instead, this will offer a larger performance boost for big problems. Change library name to something more Pythonic. Add version attribute.

  • 2017/08/03 gradients (slopes of the line segments) now stored as piecewise_lin_fit.slopes (or myPWLF.slopes) after they have been calculated by performing a fit or predicting

  • 2017/04/01 initial release

Requirements

Python 2.7+ (Python 2.7 and Python 3.4 have been tested)

NumPy (Tested on version >= 1.11.3 )

SciPy (Tested on version >= 0.19.0)

License

MIT License

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-0.0.12.tar.gz (6.4 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pwlf-0.0.12.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pwlf-0.0.12.tar.gz
Algorithm Hash digest
SHA256 da704ecdecf24a08e67eb91c403b214b2ccef9ba04b7958b62524efffdc618c4
MD5 d152f74ca3691fdef607591120a3a5a8
BLAKE2b-256 b77c55ebcd21011c0e0fea4674a7b5b5915b68c9e84e53d8e5f95e87ab5b2e07

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