Registry / data / piecewise-regression

piecewise-regression

JSON →
library1.5.0pypypi✓ verified 83d ago

Piecewise-regression is a Python library for performing segmented (or piecewise) regression analysis. It allows fitting models with one or more breakpoints, identifying where the relationship between independent and dependent variables changes. The current version is 1.5.0, with a moderate release cadence focused on statistical improvements and convenience features.

pip install piecewise-regression
INSTALL
IMPORT
SIG · PIECEWISE-REGRESSI
P
piecewise-regression
datapythonv1.5.0
Install
17.6s avg
Import
Disk
451MB
Pass rate
9/ 10
Env Coverage9 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 17.45s
py 3.11
✓ —
✓ 16.55s
py 3.12
✓ —
✓ 17.15s
py 3.13
✓ —
✓ 17.15s
py 3.9
✕ build_error
✓ 19.6s
451MB installed
● package 451MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Fit
from piecewise_regression import Fit
from piecewise_regression import PiecewiseRegression
ModelSelection
from piecewise_regression import ModelSelection
Muggeo
from piecewise_regression import Muggeo

This quickstart demonstrates how to generate synthetic data with a breakpoint, initialize the `PiecewiseRegression` model, fit it to the data, and then visualize the results. It highlights the core workflow from data preparation to model interpretation.

import numpy as np from piecewise_regression import PiecewiseRegression import matplotlib.pyplot as plt # Generate some synthetic data with a known breakpoint x = np.linspace(0, 100, 500) y = np.concatenate([ -0.1 * x[x <= 50] + 10, 0.5 * x[x > 50] - 20 ]) + np.random.normal(0, 1, 500) # Initialize and fit the model with one breakpoint pw_fit = PiecewiseRegression(x, y, n_breakpoints=1) pw_fit.fit() # Print a summary of the fit pw_fit.summary() # Plot the fit (requires matplotlib to show) fig, ax = pw_fit.plot_fit(show=False) ax.set_title('Piecewise Regression Fit') plt.show()
Debug
Known issues
gotchaThe Davies test for the existence of breakpoints was modified in v1.5.0 to use the Wald test statistic. This change affects the underlying methodology and may result in different p-values and statistical conclusions for breakpoint detection compared to versions prior to 1.5.0.
fix
Be aware that statistical results related to breakpoint significance might differ. Refer to the v1.5.0 release notes and cited literature (Muggeo, 2008) for details on the new methodology.
affects: >=1.5.0
gotchaChoosing the correct number of breakpoints (`n_breakpoints`) is crucial. Incorrectly specifying this parameter can lead to overfitting (too many breakpoints) or underfitting (too few), resulting in a poor model fit and misleading conclusions.
fix
Start with a reasonable number based on domain knowledge or visual inspection of the data. Use statistical tests provided by the library (e.g., Davies test for 1 breakpoint) or information criteria (e.g., AIC, BIC) for model selection, though these are not directly implemented for breakpoint count in the library itself.
affects: All versions
gotchaThe optimization process for finding breakpoints and regression coefficients can sometimes fail to converge, especially with noisy data, sparse data, or poorly chosen initial parameters. This might lead to warnings or incorrect results.
fix
Inspect any warnings during the `fit()` process. Consider pre-processing your data, checking for outliers, or experimenting with different `n_breakpoints` values. Ensure your data distribution is suitable for linear regression segments.
affects: All versions
Upgrade
Version history
1.5.0latest on PyPI · released Dec 18, 2023
Audit
Dependencies
numpyrequiredCore numerical operations and data handling.
scipyrequiredScientific computing, optimization routines for model fitting.
matplotlibrequiredPlotting and visualization of fit results.
statsmodelsrequiredStatistical tests and models, particularly for p-value calculations and statistical inference.
seabornrequiredEnhanced statistical data visualization.
Agent activity
9 hits · last 30 days
node
8
Resources
piecewise-regression — pip install piecewise-regression · libregistry