mlnext.anomaly.rank_features#

mlnext.anomaly.rank_features(*, error: ndarray, y: ndarray) 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).

Raises:

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

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 = 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]]