Name: piecewise
Owner: Datadog, Inc.
Description: Functions for piecewise regression on time series data
Created: 2017-06-26 18:19:43.0
Updated: 2018-05-18 11:30:11.0
Pushed: 2018-03-29 17:52:58.0
Homepage: null
Size: 39
Language: Python
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
This repo accompanies Piecewise regression: when one line simply isn?t enough, a blog post about Datadog's approach to piecewise regression. The code included here is intended to be minimal and readable; this is not a Swiss Army knife to solve all variations of piecewise regression problems.
This package was written to work with both Python 2 and Python 3.
To install this package using setup tools, clone this repo and run python setup.py install
from within the piecewise
root directory.
The package's core piecewise()
function for regression requires only numpy
. The use of plot_data_with_regression()
for plotting depends also on matplotlib
.
Start by preparing your data as list-likes of timestamps (independent variables) and values (dependent variables).
rt numpy as np
np.arange(10)
np.array(
[2*i for i in range(5)] +
[10-i for i in range(5, 10)]
np.random.normal(0, 1, 10)
Now, you're ready to import the piecewise()
function and fit a piecewise linear regression.
piecewise.regressor import piecewise
l = piecewise(t, v)
model
if a FittedModel
object. If you are at a shell, you can print the object to see the fitted segments domains and regression coefficients.
model
edModel with segments:
ttedSegment(start_t=0, end_t=5, coeffs=(-0.8576123780622642, 2.224791099812951))
ttedSegment(start_t=5, end_t=9, coeffs=(10.975487672814133, -1.0722348284390741))
Alternatively, you can use the FittedModel
's segments
attribute to get at values.
len(model.segments)
model.segments[0].coeffs
8576123780622642, 2.224791099812951)
If you want to interpolate or extrapolate, you can use the FittedModel
's predict()
function.
model.predict(t_new=[3.5, 100])
y([ 6.92915647, -96.24799517])
To see a plot, instead of getting a FittedModel
, use plot_data_with_regression()
.
piecewise.plotter import plot_data_with_regression
_data_with_regression(t, v)