mlnext.anomaly.apply_point_adjust_score#

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

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

  • k=0, the score of all points are adjusted to the maximum score in the segment

  • 0 < k < 100, the score for the adjustment is chosen, such that at least %k of points in the anomaly segments have a higher score and only the points below the chosen score are adjusted to the score

  • k=100, no adjustment is made

If k=0 it is equal to the original point-adjust, if k=100 it is equal to the F1. This method allows the usage of the point-adjust method in conjunction with precision-recall and other similar curves.

Parameters:
  • y_score (np.ndarray) – Prediction score in range [0, 1] (1d array).

  • y (np.ndarray) – Ground truth (1d array).

Returns:

Returns the adjusted array.

Return type:

np.ndarray

Example

>>> import numpy as np
>>> import mlnext
>>> mlnext.apply_point_adjust_score(
... y_score = np.array([0.1, 0.4, 0.6, 0.7, 0.4, 0.2, 0.4, 0.6, 0.25]),
... y=        np.array([  0,   0,   1,   1,   1,   0,   1,   1,    0]),
... k=0)
[0.1, 0.4, 0.7, 0.7, 0.7, 0.2, 0.6, 0.6, 0.25]
>>> # for k=40; both segments are adjusted
>>> mlnext.apply_point_adjust_score(
... y_score = np.array([0.1, 0.4, 0.6, 0.7, 0.4, 0.2, 0.4, 0.6, 0.25]),
... y=        np.array([  0,   0,   1,   1,   1,   0,   1,   1,    0]),
... k=40)
[0.1, 0.4, 0.6, 0.7, 0.6, 0.2, 0.6, 0.6, 0.25]