npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

segreg

v0.5.5

Published

Piecewise regression in JavaScript

Readme

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)