BaseChangeDetector#
- class BaseChangeDetector[source][source]#
Base class for all detectors providing sklearn compatibility.
Inherits from
sklearn.base.BaseEstimatorfor cloning, pipeline support, andget_params/set_params.Subclasses must implement:
fit(X, y=None) -> selfpredict_changepoints(X) -> np.ndarrayof sorted boundary indices
Both
predictandpredict_changepointsare the universal interface: every detector supports both.predict_changepointsreturns sorted boundary indices;predictreturns one segment label per input sample (labels are integers and may reoccur across non-contiguous segments).predictis derived frompredict_changepointsby default. Subclasses that natively produce labels (e.g. CAPA uses0 = normal, 1..K = anomaly) overridepredictdirectly and also overridepredict_changepointsto derive boundaries from the labels.Additional capabilities such as
predict_all()orpredict_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 of this object.
get_params([deep])Get parameters for this estimator.
predict(X)Detect changepoints, returning per-sample segment labels.
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
tmeans sampletis the first sample of a new segment, i.e. a structural break occurs between samplest-1andt. 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
MetadataRequestencapsulating 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.