IForestASD#

class capymoa.anomaly.IForestASD[source]#

Bases: AnomalyDetector

iForestASD

Reference: Ding, Z., & Fei, M. (2013). An anomaly detection approach based on isolation forest algorithm for streaming data using sliding window. IFAC proceedings volumes, 46(20), 12-17.

Example: >>> from capymoa.datasets import ElectricityTiny >>> from capymoa.anomaly import IForestASD >>> from capymoa.evaluation import AnomalyDetectionEvaluator >>> stream = ElectricityTiny() >>> schema = stream.get_schema() >>> learner = IForestASD(schema, window_size=256, n_trees=100, … sample_size=64, random_state=42) >>> 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.61

TODO: implement concept drift method

__init__(
schema: Schema,
window_size: int = 2048,
n_trees: int = 100,
sample_size: int = 256,
height_limit: int | None = None,
random_state: int | None = None,
)[source]#

Initialize the IForestASD anomaly detector. :param schema: The schema of the data stream. :param window_size: The size of the sliding window to maintain. :param n_trees: The number of isolation trees to build. :param sample_size: The number of instances to sample for each tree. :param height_limit: The maximum height of the isolation trees. If None, it will be set to ceil(log2(sample_size)). :param random_state: The seed for the random number generator.

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.

Returns:

The anomaly score between 0 and 1.

train(instance: Instance)[source]#