milia.anomaly.explain#

SHAP explanations of an Isolation Forest flag.

SHAP on an Isolation Forest says which kind of feature drove a flag (shape, spread or spectrum), not reliably which hour; read it accordingly.

Functions

explain(detector, query[, names, top])

Largest SHAP contributions to a fitted Isolation Forest's score of one row.

waterfall(detector, query[, names, top])

SHAP waterfall plot (matplotlib) of one row's explanation.

milia.anomaly.explain.explain(detector, query, names=None, top=5)#

Largest SHAP contributions to a fitted Isolation Forest’s score of one row.

Parameters:
  • detector (IForest) – A fitted IForest.

  • query (ArrayLike) – One row, shape (n_features,) or (1, n_features).

  • names (Sequence[str] | None) – Feature names; x0, x1, ... when omitted.

  • top (int) – Number of features returned.

Return type:

DataFrame

Returns:

feature, value and shap columns, largest absolute contribution first.

Examples

>>> import numpy as np
>>> from milia import make
>>> X = np.random.default_rng(0).normal(size=(200, 3))
>>> model = make("detector", "iforest").fit(X)
>>> explain(model, [0.0, 0.0, 9.0], names=["a", "b", "c"], top=1)["feature"].item()
'c'
milia.anomaly.explain.waterfall(detector, query, names=None, top=10)#

SHAP waterfall plot (matplotlib) of one row’s explanation.

Parameters:
Return type:

object

Returns:

The matplotlib axes SHAP draws on.

Examples

>>> waterfall(model, [0.0, 0.0, 9.0])