top_k_ranges_threshold

top_k_ranges_threshold(y_true: ndarray, y_score: ndarray, k: int | None = None) float[source]

Calculate a threshold such that at least k anomalies are found.

The anomalies are either single-points anomalies or continuous anomalous ranges.

Computes a threshold based on the number of expected anomalous subsequences / ranges (number of anomalies). This method iterates over all possible thresholds from high to low to find the first threshold that yields k or more continuous anomalous ranges. If k is None, the ground truth data is used to calculate the real number of anomalies (anomalous ranges).

Parameters:
y_truenp.ndarray

True binary labels of shape (n_instances,).

y_scorenp.ndarray

Anomaly scores for each point of the time series of shape (n_instances,).

koptional int

Number of expected anomalies. If k is None, the ground truth data is used to calculate the real number of anomalies.

Returns:
float

Threshold such that there are at least k anomalous ranges.