Giter VIP home page Giter VIP logo

dd_piecewise's Introduction

piecewise

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.

Installation & dependencies

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 piecewise_plot() for plotting depends also on matplotlib.

Usage

Start by preparing your data as list-likes of timestamps (independent variables) and values (dependent variables).

import numpy as np

t = np.arange(10)
v = 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.

from piecewise import piecewise

model = 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
FittedModel with segments:
* FittedSegment(start_t=0, end_t=5, coeffs=(-0.8576123780622642, 2.224791099812951))
* FittedSegment(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)
2
>>> model.segments[0].coeffs
(-0.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])
array([  6.92915647, -96.24799517])

To see a plot, instead of getting a FittedModel, use piecewise_plot(). You may also use an existing FittedModel.

from piecewise import piecewise_plot

# using an existing FittedModel
piecewise_plot(t, v, model=model)

# fitting a model on the fly
piecewise_plot(t, v)

dd_piecewise's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.