top_k_points_threshold

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

Calculate a threshold such that at least k anomalous points are found.

The anomalies are single-point anomalies.

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

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 points.