BaseChangeDetector#

class BaseChangeDetector[source][source]#

Base class for all detectors providing sklearn compatibility.

Inherits from sklearn.base.BaseEstimator for cloning, pipeline support, and get_params / set_params.

Subclasses must implement:

  • fit(X, y=None) -> self

  • predict_changepoints(X) -> np.ndarray of sorted boundary indices

Both predict and predict_changepoints are the universal interface: every detector supports both. predict_changepoints returns sorted boundary indices; predict returns one segment label per input sample (labels are integers and may reoccur across non-contiguous segments).

predict is derived from predict_changepoints by default. Subclasses that natively produce labels (e.g. CAPA uses 0 = normal, 1..K = anomaly) override predict directly and also override predict_changepoints to derive boundaries from the labels.

Additional capabilities such as predict_all() or predict_segment_anomalies() are duck-typed: add them on the concrete class when the algorithm supports them. No intermediate base class is needed.

Methods

fit_predict(X[, y])

Fit to data, then predict per-sample segment labels.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X)

Detect changepoints, returning per-sample segment labels.

predict_changepoints(X)

Detect changepoints in a time series.

set_params(**params)

Set the parameters of this estimator.

Examples

>>> class MyDetector(BaseChangeDetector):
...     def __init__(self, threshold=1.0):
...         self.threshold = threshold
...
...     def fit(self, X, y=None):
...         self.n_features_in_ = X.shape[1]
...         return self
...
...     def predict_changepoints(self, X):
...         return np.array([50, 100])
predict_changepoints(X: ArrayLike) ndarray[source][source]#

Detect changepoints in a time series.

Subclasses must implement this method.

Parameters:
XArrayLike of shape (n_samples, n_features)

Time series data.

Returns:
changepointsnp.ndarray of shape (n_changepoints,)

Sorted integer indices of detected changepoints. A changepoint at index t means sample t is the first sample of a new segment, i.e. a structural break occurs between samples t-1 and t. Empty array if no changepoints are detected.

predict(X: ArrayLike) ndarray[source][source]#

Detect changepoints, returning per-sample segment labels.

Default implementation calls predict_changepoints() and converts the changepoint indices to an array with segment labels per sample. Subclasses may override this directly when the algorithm natively produces labels.

Parameters:
XArrayLike of shape (n_samples, n_features)

Time series data.

Returns:
labelsnp.ndarray of shape (n_samples,)

Dense integer segment labels, one per sample. Segment 0 is the first segment, segment 1 the next, and so on.

Examples

>>> labels = detector.fit(X_train).predict(X_test)
>>> labels.shape
(n_samples,)
fit_predict(X, y: ArrayLike | None = None, **fit_params) ndarray[source][source]#

Fit to data, then predict per-sample segment labels.

Equivalent to calling fit(X, y).predict(X).

Parameters:
XArrayLike of shape (n_samples, n_features)

Time series data.

yNone

Ignored. Exists for sklearn API compatibility.

**fit_paramsdict

Additional parameters passed to fit().

Returns:
labelsnp.ndarray of shape (n_samples,)

Dense integer segment labels, one per sample.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.