AdaptiveIsolationForest#

class capymoa.anomaly.AdaptiveIsolationForest[source]#

Bases: AnomalyDetector

Adaptive Isolation Forest for anomaly detection.

This implementation adapts the Isolation Forest algorithm to be more adaptive in changing environments. It uses a sliding window approach and can replace trees based on their quality scores, combining tree size and maximum mass metrics to determine which trees to keep.

References:

Liu, J.J., Cassales, G.W., Liu, F.T., Pfahringer, B., Bifet, A. (2025). Adaptive Isolation Forest. In: Džeroski, S., Levatić, J., Pio, G., Simidjievski, N. (eds) Discovery Science. DS 2025. Lecture Notes in Computer Science(), vol 16090. Springer, Cham. https://doi.org/10.1007/978-3-032-05461-6_24

Example:

>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.anomaly import AdaptiveIsolationForest
>>> from capymoa.evaluation import AnomalyDetectionEvaluator
>>> stream = ElectricityTiny()
>>> schema = stream.get_schema()
>>> learner = AdaptiveIsolationForest(schema, window_size=256, n_trees=100)
>>> evaluator = AnomalyDetectionEvaluator(schema)
>>> while stream.has_more_instances():
...     instance = stream.next_instance()
...     proba = learner.score_instance(instance)
...     evaluator.update(instance.y_index, proba)
...     learner.train(instance)
>>> auc = evaluator.auc()
>>> print(f"AUC: {auc:.2f}")
AUC: 0.81
__init__(
schema: Schema,
window_size=256,
n_trees=100,
height=None,
seed: int | None = None,
m_trees=10,
weights=0.5,
)[source]#

Construct an Adaptive Isolation Forest anomaly detector.

Parameters:
  • schema – The schema of the stream.

  • window_size – Size of the sliding window for training trees.

  • n_trees – Number of trees in the ensemble.

  • height – Maximum height of trees. If None, calculated as ceil(log2(window_size)).

  • seed – Random seed for reproducibility.

  • m_trees – Number of candidate trees to generate when replacing.

  • weights – Weight for combining tree size and max mass scores (0-1).

  • skip_default_strategy – If True, skip default replacement when quality score doesn’t improve.

predict(instance) int | None[source]#

Predict is not implemented for anomaly detection.

Parameters:

instance – The instance to predict.

Raises:

NotImplementedError – This method is not applicable for anomaly detection.

score_instance(instance: Instance) float[source]#

Calculate the anomaly score for an instance.

A high score is indicative of an anomaly.

Parameters:

instance – The instance to score (must be a LabeledInstance).

Returns:

The anomaly score between 0 and 1.

train(instance)[source]#

Train the model on a single instance.

Parameters:

instance – The instance to train on.