milia.anomaly.models.iforest#

Isolation Forest: how few random splits it takes to isolate a point.

Classes

IForest([n_estimators, max_samples, ...])

Tree-ensemble detector (Liu, Ting and Zhou, 2008).

IForestConfig(*[, name, kind, space, ...])

Validated hyperparameters of the Isolation Forest detector.

class milia.anomaly.models.iforest.IForest(n_estimators=100, max_samples='auto', max_features=1.0, random_state=0, contamination=0.1)#

Bases: BaseDetector

Tree-ensemble detector (Liu, Ting and Zhou, 2008).

The fitted sklearn.ensemble.IsolationForest is kept as estimator_, so it can be explained with SHAP (milia.anomaly.explain).

Parameters:
  • n_estimators (int) – Number of trees.

  • max_samples (int | float | str) – Samples drawn per tree.

  • max_features (float) – Features drawn per tree.

  • random_state (int | None) – Seed for the random splits.

  • contamination (float) – Expected share of outliers, used for the default threshold.

Examples

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> train, cal = rng.normal(size=(200, 2)), rng.normal(size=(100, 2))
>>> model = IForest().fit(train).calibrate(cal, alpha=0.1)
>>> model.flag(np.array([[0.0, 0.0], [8.0, 8.0]])).tolist()
[False, True]
class milia.anomaly.models.iforest.IForestConfig(*, name='iforest', kind='detector', space=SearchSpace(n_estimators=(50, 500), max_features=(0.3, 1.0)), n_estimators=100, max_samples='auto', max_features=1.0, random_state=0, contamination=0.1)#

Bases: BaseModelConfig

Validated hyperparameters of the Isolation Forest detector.

Fields mirror IForest’s constructor; space is the default Optuna search space. Build the detector with make("detector", "iforest", ...) or IForest(**config.params()).

Examples

>>> IForestConfig().name
'iforest'