# `StreamingIsolationForest`

### *class* capymoa.anomaly.StreamingIsolationForest[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_streaming_isolation_forest.py#L401)

Bases: [`AnomalyDetector`](capymoa.base.AnomalyDetector.md#capymoa.base.AnomalyDetector)

Streaming Isolation Forest anomaly detector.

Streaming Isolation Forest anomaly detector <sup>[1](#f0)</sup> constructs an ensemble of
isolation trees incrementally in a streaming manner to perform anomaly detection.
Each tree employs reservoir sampling to maintain a fixed-size window of training
instances. The anomaly score of an instance is calculated as the average path length
across all trees, normalized by the expected path length for a randomly chosen
instance in a tree of equivalent size. Scores are scaled between 0 and 1, with
higher values indicating greater anomaly likelihood.

```pycon
>>> from capymoa.anomaly.datasets import TinyBlobs
>>> from capymoa.anomaly import StreamingIsolationForest
>>> from capymoa.evaluation import AnomalyDetectionEvaluator
>>> stream = TinyBlobs()
>>> schema = stream.get_schema()
>>> learner = StreamingIsolationForest(schema, window_size=256, n_trees=20, seed=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.96
```

* <a id='f0'>**[1]**</a> Liu, J.J., Cassales, G.W., Liu, F.T., Pfahringer, B., Bifet, A. (2025). Streaming Isolation Forest. In: Wu, X., et al. Advances in Knowledge Discovery and Data Mining . PAKDD 2025. Lecture Notes in Computer Science(), vol 15870. Springer, Singapore. [https://doi.org/10.1007/978-981-96-8170-9_8](https://doi.org/10.1007/978-981-96-8170-9_8)

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema), window_size=256, n_trees=100, height=None, seed: [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None) = None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_streaming_isolation_forest.py#L434)

Construct a Streaming Isolation Forest anomaly detector.
:param schema: The schema of the stream. If not provided, it will be inferred from the data.
:param window_size: The size of the window for each tree.
:param n_trees: The number of trees in the ensemble.
:param height: The maximum height of each tree. If None, it will be set to log2(window_size).
:param seed: Random seed for reproducibility.

#### *classmethod* from_params(schema: [Any](https://docs.python.org/3/library/typing.html#typing.Any) = None, params: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1) → [Any](https://docs.python.org/3/library/typing.html#typing.Any)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L170)

Construct an instance from parameters produced by `get_params`.

#### get_params() → [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)][[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L163)

Return the hyper-parameters captured from the constructor.

#### predict(instance: [LabeledInstance](capymoa.core.LabeledInstance.md#capymoa.core.LabeledInstance)) → [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_streaming_isolation_forest.py#L489)

#### score_instance(instance: [LabeledInstance](capymoa.core.LabeledInstance.md#capymoa.core.LabeledInstance)) → [float](https://docs.python.org/3/builtins/functions.html#float)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_streaming_isolation_forest.py#L475)

Returns the anomaly score for the instance.

A high score is indicative of an anomaly.

* **Parameters:**
  **instance** – The instance for which the anomaly score is calculated.
* **Returns:**
  The anomaly score for the instance.

#### train(instance: [LabeledInstance](capymoa.core.LabeledInstance.md#capymoa.core.LabeledInstance))[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_streaming_isolation_forest.py#L458)
