mlnext.anomaly.apply_point_adjust#

mlnext.anomaly.apply_point_adjust(*, y_hat: ndarray, y: ndarray, k: float = 0) ndarray[source]#

Implements the point-adjust approach from https://arxiv.org/abs/1802.03903 and its variation from https://arxiv.org/abs/2109.05257 (parameter k). For a ground truth anomaly segment in y:

  • k=0, if any point x in the segment was classified as anomalous (y_hat=1 for x)

  • 0 < k < 100, if more than (>) %k of points in the segment are classified as anomalous (y_hat=1 for %k of points)

  • k=100 if all points in the segment are classified as anomalous (y_hat=1 for all points)

then the label for all observations in the segment are adjusted to y_hat=1. If k=0 it is equal to the original point-adjust, if k=100 it is equal to the F1.

Parameters:
  • y_hat (np.ndarray) – Label predictions (1d).

  • y (np.ndarray) – Ground Truth (1d).

  • k (int) – Percentage [0, 100] of points detected as an anomaly in a segment before an adjustment is made (Default: 0).

Returns:

Returns the point-adjusted y_hat.

Return type:

np.ndarray

Example

>>> import mlnext
>>> import numpy as np
>>> mlnext.apply_point_adjust(
...   y_hat = np.array([1, 0, 0, 1, 0, 0, 0, 1, 1]),
...   y =     np.array([0, 0, 1, 1, 1, 0, 1, 1, 0]))
[1, 0, 1, 1, 1, 0, 1, 1, 1]
>>> # for k = 40; only adjusts the second segment
>>> mlnext.apply_point_adjust(
...   y_hat = np.array([1, 0, 0, 1, 0, 0, 0, 1, 1]),
...   y =     np.array([0, 0, 1, 1, 1, 0, 1, 1, 0])
...   k = 40)
[1, 0, 0, 1, 0, 0, 1, 1, 1]