Skip to content

Repository files navigation

skchange

codecov tests docs BSD 3-clause !black Python PyPI Downloads

Skchange provides fast and flexible changepoint detection algorithms within a scikit-learn-like API. See the documentation for full details.

Users upgrading from version <0.17 should consult the migration guide.

Installation

It is recommended to install skchange with numba for faster performance:

pip install skchange[numba]

Alternatively, you can install skchange without numba:

pip install skchange

Quickstart

Changepoint detection / time series segmentation

from skchange.datasets import generate_piecewise_normal_data
from skchange.detectors import MovingWindow

X = generate_piecewise_normal_data(
    means=[0, 5, 10, 5, 0],
    lengths=[50, 50, 50, 50, 50],
    seed=1,
)

detector = MovingWindow(bandwidth=20)
detector.fit_predict(X)
array([ 50, 100, 150, 200])

Automatic penalty calibration

import scipy.stats as st
from skchange.datasets import generate_piecewise_data
from skchange.detectors import SeededBinarySegmentation
from skchange.tuning import CalibratedDetectorFWER

# Change-free beta(2, 5) data used to calibrate the detection threshold.
X_train = generate_piecewise_data(st.beta(2, 5), lengths=300, seed=0)
# Test data with two changepoints where the beta shape changes.
X_test = generate_piecewise_data(
    [st.beta(2, 5), st.beta(5, 2), st.beta(1, 10)],
    lengths=100,
    seed=1,
)

detector = CalibratedDetectorFWER(
    SeededBinarySegmentation(),
    level=0.05,
    n_simulations=999,
    random_state=0,
)
detector.fit(X_train)
detector.predict(X_test)
array([100, 200])

Multivariate segment anomaly detection

from skchange.datasets import generate_piecewise_normal_data
from skchange.detectors import CAPA
from skchange.interval_scorers import L2Saving

X = generate_piecewise_normal_data(
    means=[0, 8, 0, 5],
    lengths=[100, 20, 130, 50],
    proportion_affected=[1.0, 0.1, 1.0, 0.5],
    n_variables=10,
    seed=1,
)

detector = CAPA(segment_saving=L2Saving())
detector.fit(X)
detector.predict_segment_anomalies(X)
array([[100, 120],
       [250, 300]])

License

skchange is a free and open-source software licensed under the BSD 3-clause license.

Releases

Packages

Used by

Contributors

Languages