CLaP#

CLaP (Classification Label Profile) – classification-based state detection.

Description#

CLaP extends ClaSP from change point detection to state detection. It first segments the series with ClaSP, then cross-validates a time series classifier (e.g. ROCKET) on segment-labelled subsequences. Segments with high confusion are iteratively merged, producing a set of state labels that can be reused across non-contiguous time regions.

CLaP runs in three modes:

  • Unsupervised: the number of segments is learned by ClaSP.

  • Guided: provide n_segments or n_change_points.

  • Exact: provide pre-computed change_points; ClaSP is not run.

Type: state detection
Supervision: unsupervised or semi-supervised
Scope: univariate and multivariate
Complexity: \(O(K\,n\,(d\,n + K\log K))\), where K is the number of change points

Parameters#

Name

Type

Default

Description

window_size

str / int

"suss"

Window size or auto-detection method.

classifier

str

"rocket"

aeon time series classifier: "rocket", "mrhydra" (MultiRocket-Hydra), "weasel" (WEASEL 2.0), "quant", "rdst", "proximityforest", "freshprince" or "inception" (needs aeon[dl]).

merge_score

str

"cgain"

Score that decides whether two confused labels are merged: "cgain" (classification gain), "f1_score", "log_loss", "ami", "hamming_loss" or "roc_auc".

n_splits

int

5

Cross-validation folds.

n_jobs

int

1

Parallel jobs (-1 = all CPUs).

sample_size

int

1000

Samples for classifier training.

n_change_points

int / None

None

Number of change points (overrides n_segments; 0 = one segment).

change_points

array-like / None

None

Known change points (exact mode; ClaSP is not run).

n_segments

int / None

None

Number of segments (guided mode).

distance

str

"znormed_euclidean_distance"

Distance of ClaSP’s k-NN: "znormed_euclidean_distance", "euclidean_distance" or "cinvariant_euclidean_distance". The z-normalised default misses a change of level alone (a mean shift in noise).

random_state

int

42

Seed of CLaP’s classifiers; ClaSP keeps the fixed seed of claspy (2357).

Usage#

from tsseg.algorithms import ClapDetector

# Guided: four segments, one state label per time point
clap = ClapDetector(n_segments=4, classifier="rocket")
states = clap.fit_predict(X)

Implementation: Adapted from original ClaSP code by Arik Ermshaus. BSD 3-Clause.

Reference: Ermshaus, Schäfer & Leser (2024), CLaP – State Detection from Time Series.

API reference#

class CLaP(window_size='suss', classifier='rocket', merge_score='cgain', n_splits=5, n_jobs=1, sample_size=1000, random_state=2357)[source]#

Bases: object

fit(time_series, change_points, labels=None)[source]#
get_change_points()[source]#
get_segment_labels()[source]#
random_f1_score(y_true)[source]#
score()[source]#

This module provides a wrapper for the CLaP algorithm to integrate it with the tsseg library’s API.

class ClapDetector(*, window_size='suss', classifier='rocket', merge_score='cgain', n_splits=5, n_jobs=1, sample_size=1000, random_state=42, n_change_points=None, change_points=None, n_segments=None, distance='znormed_euclidean_distance', axis=0)[source]#

Bases: BaseSegmenter

A wrapper for the CLaP (Classification Label Profile) algorithm for time series state detection, compatible with aeon.

Parameters:
  • window_size (str or int, default="suss") – The window size or the method to determine it (‘suss’, ‘fft’, ‘acf’).

  • classifier (str, default="rocket") – The aeon time series classifier that is cross-validated on the segments: "rocket" (RocketClassifier), "mrhydra" (MultiRocketHydraClassifier), "weasel" (WEASEL_V2), "quant" (QUANTClassifier), "rdst" (RDSTClassifier), "proximityforest" (ProximityForest), "freshprince" (FreshPRINCEClassifier) or "inception" (IndividualInceptionClassifier, which needs the aeon deep learning extras, pip install aeon[dl]).

  • merge_score (str, default="cgain") – The score that decides whether two confused segment labels are merged: "cgain" (classification gain), "f1_score" (macro F1), "log_loss", "ami" (adjusted mutual information), "hamming_loss" or "roc_auc".

  • n_splits (int, default=5) – Number of splits for cross-validation.

  • n_jobs (int, default=1) – The number of jobs to run in parallel. -1 means using all processors.

  • sample_size (int, default=1000) – The number of samples to use for training the classifier.

  • random_state (int, default=42) – Seed of the classifiers of CLaP. ClaSP, which finds the change points when change_points is not given, does not use it: its ensemble draws with a fixed seed (2357, as in claspy).

  • axis (int) – The axis of the time series data.

  • n_change_points (int, optional) – The number of change points to find (guided mode). Takes precedence over n_segments; 0 yields a single segment, hence one state.

  • change_points (array-like of int, optional) – Known change points (exact mode). When given, ClaSP is not run and n_change_points and n_segments are ignored.

  • n_segments (int, optional) – The number of segments to find (guided mode), passed to ClaSP.

  • distance (str, default="znormed_euclidean_distance") – The distance ClaSP uses to find the k nearest neighbours of each window: "znormed_euclidean_distance", "euclidean_distance" or "cinvariant_euclidean_distance" (complexity invariant). The z-normalised default compares the shapes of the windows, so it misses a change of level alone (a mean shift in noise); the other two see it.

Notes

CLaP labels the segments between change points. Three usage patterns are supported:

  • Exact – pass the known change points as change_points; they are used as they are.

  • Guided – pass n_change_points (or n_segments). The count is forwarded to BinaryClaSPSegmentation, without its validation: n_change_points=k gives k change points, fewer only when the segments are too short to split further (each part must span at least five windows). claspy validates them in this mode too, so it may return fewer, or none.

  • Fully unsupervised – the default behaviour: ClaSP estimates the change points and their number, each change point passing its significance test.

CLaP may then give two segments the same state label, so the number of states can be lower than the number of segments.

y is never used: fit() ignores it.

CLaP labels the series given to fit: predict(X) returns those labels when X has the same values, and otherwise labels X with a new fit, with a warning, leaving the fitted attributes unchanged.

states_#

An array of state labels for each time point. Available only after calling the fit or fit_predict method.

Type:

np.ndarray

change_points_#

The change points used for fitting the model.

Type:

np.ndarray

set_fit_request(*, axis='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

axis (Union[bool, None, str]) – Metadata routing for axis parameter in fit.

Returns:

self – The updated object.

Return type:

ClapDetector

set_predict_request(*, axis='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

axis (Union[bool, None, str]) – Metadata routing for axis parameter in predict.

Returns:

self – The updated object.

Return type:

ClapDetector