{"id":14989202,"url":"https://github.com/cjekel/piecewise_linear_fit_py","last_synced_at":"2025-05-14T17:09:14.532Z","repository":{"id":19300358,"uuid":"86728778","full_name":"cjekel/piecewise_linear_fit_py","owner":"cjekel","description":"fit piecewise linear data for a specified number of line segments","archived":false,"fork":false,"pushed_at":"2025-02-23T23:05:28.000Z","size":16531,"stargazers_count":330,"open_issues_count":42,"forks_count":62,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-13T10:07:52.905Z","etag":null,"topics":["numpy","piecewise-linear","piecewise-regression","python","scipy","segment","segmented","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cjekel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-30T17:14:14.000Z","updated_at":"2025-05-09T00:29:48.000Z","dependencies_parsed_at":"2025-02-28T10:07:01.011Z","dependency_job_id":"63424959-ce98-4673-8a23-8241b2c1857f","html_url":"https://github.com/cjekel/piecewise_linear_fit_py","commit_stats":{"total_commits":544,"total_committers":8,"mean_commits":68.0,"dds":0.05514705882352944,"last_synced_commit":"2e2883095fbf1783a71d52d1868f661e24f53f17"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjekel%2Fpiecewise_linear_fit_py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjekel%2Fpiecewise_linear_fit_py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjekel%2Fpiecewise_linear_fit_py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjekel%2Fpiecewise_linear_fit_py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cjekel","download_url":"https://codeload.github.com/cjekel/piecewise_linear_fit_py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254190396,"owners_count":22029632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["numpy","piecewise-linear","piecewise-regression","python","scipy","segment","segmented","tensorflow"],"created_at":"2024-09-24T14:17:51.443Z","updated_at":"2025-05-14T17:09:14.499Z","avatar_url":"https://github.com/cjekel.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About\nA library for fitting continuous piecewise linear functions to data. Just specify the number of line segments you desire and provide the data.\n\n![Downloads a month](https://img.shields.io/pypi/dm/pwlf.svg) ![pwlf ci](https://github.com/cjekel/piecewise_linear_fit_py/workflows/pwlf%20ci/badge.svg) [![codecov](https://codecov.io/gh/cjekel/piecewise_linear_fit_py/branch/master/graph/badge.svg?token=AgeDFEQXed)](https://codecov.io/gh/cjekel/piecewise_linear_fit_py) ![PyPI version](https://img.shields.io/pypi/v/pwlf) [![Conda](https://img.shields.io/conda/vn/conda-forge/pwlf)](https://anaconda.org/conda-forge/pwlf)\n\nCheck out the [documentation](https://jekel.me/piecewise_linear_fit_py)!\n\nRead the [blog post](http://jekel.me/2017/Fit-a-piecewise-linear-function-to-data/).\n\n![Example of a continuous piecewise linear fit to data.](https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/examplePiecewiseFit.png)\n\n![Example of a continuous piecewise linear fit to a sine wave.](https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/sinWaveFit.png)\n\nNow you can perform segmented constant fitting and piecewise polynomials!\n![Example of multiple degree fits to a sine wave.](https://raw.githubusercontent.com/cjekel/piecewise_linear_fit_py/master/examples/figs/multi_degree.png)\n\n# Features\nFor a specified number of line segments, you can determine (and predict from) the optimal continuous piecewise linear function f(x). See [this example](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/fitForSpecifiedNumberOfLineSegments.py).\n\nYou 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](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/fitWithKnownLineSegmentLocations.py).\n\nIf you want to pass different keywords for the SciPy differential evolution algorithm see [this example](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/fitForSpecifiedNumberOfLineSegments_passDiffEvoKeywords.py).\n\nYou 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](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/useCustomOptimizationRoutine.py).\n\nInstead 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](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/sineWave_time_compare.py) which runs fit() function, then runs the fitfast() to compare the runtime differences!\n\n# Installation\n\n## Python Package Index (PyPI)\n\nYou can now install with pip.\n```\npython -m pip install pwlf\n```\n\n## Conda\n\nIf you have conda, you can also install from conda-forge.\n```\nconda install -c conda-forge pwlf\n```\n\n## From source\n\nOr clone the repo\n```\ngit clone https://github.com/cjekel/piecewise_linear_fit_py.git\n```\n\nthen install with pip\n```\npython -m pip install ./piecewise_linear_fit_py\n```\n\n# How it works\nThis [paper](https://github.com/cjekel/piecewise_linear_fit_py/raw/master/paper/pwlf_Jekel_Venter_v2.pdf) explains how this library works in detail.\n\nThis 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](http://jekel.me/2018/Continous-piecewise-linear-regression/) which goes through the derivation of a least squares regression problem if the break point locations are known. Alternatively check out [Golovchenko (2004)](http://golovchenko.org/docs/ContinuousPiecewiseLinearFit.pdf).\n\nGlobal optimization is used to find the best location for the user defined number of line segments. I specifically use the [differential evolution](https://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.optimize.differential_evolution.html) 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](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/examples/fitForSpecifiedNumberOfLineSegments_passDiffEvoKeywords.py).\n\n# Changelog\nAll changes now stored in [CHANGELOG.md](https://github.com/cjekel/piecewise_linear_fit_py/blob/master/CHANGELOG.md)\n\nNew ```weights=``` keyword allows you to perform weighted pwlf fits! Removed TensorFlow code which can now be found [here](https://github.com/cjekel/piecewise_linear_fit_py_tf). \n\n# Requirements\n\nNumPy \u003e= 1.14.0\n\nSciPy \u003e= 1.8.0\n\n\n# License\nMIT License\n\n# Citation\n\n```bibtex\n@Manual{pwlf,\nauthor = {Jekel, Charles F. and Venter, Gerhard},\ntitle = {{pwlf:} A Python Library for Fitting 1D Continuous Piecewise Linear Functions},\nyear = {2019},\nurl = {https://github.com/cjekel/piecewise_linear_fit_py}\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjekel%2Fpiecewise_linear_fit_py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcjekel%2Fpiecewise_linear_fit_py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjekel%2Fpiecewise_linear_fit_py/lists"}