mlnext.anomaly.rank_features

mlnext.anomaly.rank_features(*, error: ndarray, y: ndarray, reduce: Literal['mean', 'max', 'sum'] = 'mean', mask: ndarray[Any, dtype[bool]] | None = None) Tuple[List[Tuple[int, int]], ndarray, ndarray][source]

Finds the anomalies in y and calculates the feature-wise error for each anomaly. Each feature is ranked accordingly to their mean error during the anomaly.

Parameters:
  • error (np.ndarray) – Error (2d or 3d).

  • y (np.ndarray) – Labels (1d).

  • reduce (str) –

    Function used to reduce the error over the segment to a single feature-wise scalar.

    Added in version 0.5.0.

  • mask (np.NDArray[np.bool] | None) –

    Mask applied to the error. Only elements where the mask is True are counted for a segment. If there is no valid element for a segment, then the error is 0 for all features. See example below.

    Added in version 0.5.0.

Raises:

ValueError – Raised if length do not align for error and y or no anomalies were found in y.

Returns:

Returns a tuple of 1. List of tuple where a tuple contains the start and end index of an anomaly. 2. A 2d array where each rows contains the ranked feature indexes. 3. A 2d array where each rows contains the mean error for the features in order of 2.

Return type:

T.Tuple[T.List[T.Tuple[int, int]], np.ndarray]

Example

>>> errors = np.array([[0.1, 0.8, 0.3, 0.25], [0.2, 0.4, 0.2, 0.6]]).T
>>> y = np.array([0, 1, 0, 1])
>>> segments, rankings, mean_errors = mlnext.rank_features(
...     error=errors,
...     y=y
... )
>>> segments
    [(1, 1), (3, 3)]
>>> rankings
    [[0, 1], [1, 0]]
>>> mean_errors
    [[0.8, 0.4], [0.6, 0.25]]
>>> errors = np.array([[0.1, 0.8, 0.3, 0.25], [0.2, 0.4, 0.2, 0.6]]).T
>>> y = np.array([1, 0, 1, 1])
>>> y_hat = np.array([0, 0, 1, 0])
>>> mask = (y & y_hat).astype('bool')
>>> segments, rankings, mean_errors = mlnext.rank_features(
...     error=errors,
...     y=y,
...     reduce='sum',
...     mask=mask,
... )
>>> segments
    [(0, 0), (2, 3)]
>>> rankings
    [[0, 1], [0, 1]]
>>> mean_errors
    [[0, 0], [0.3, 0.2]]