# `OnlineIsolationForest`

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

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

Online Isolation Forest

This class implements the Online Isolation Forest (oIFOR) algorithm, which is
an ensemble anomaly detector capable of adapting to concept drift.

Reference:

[Online Isolation Forest.
Filippo Leveni, Guilherme Weigert Cassales, Bernhard Pfahringer, Albert Bifet, and Giacomo Boracchi.
International Conference on Machine Learning (ICML), Proceedings of Machine Learning Research (PMLR), 2024.](https://proceedings.mlr.press/v235/leveni24a.html)

Example:

```pycon
>>> from capymoa.anomaly.datasets import TinyBlobs
>>> from capymoa.anomaly import OnlineIsolationForest
>>> from capymoa.evaluation import AnomalyDetectionEvaluator
>>> stream = TinyBlobs()
>>> schema = stream.get_schema()
>>> learner = OnlineIsolationForest(schema=schema, window_size=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.75
```

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1, num_trees: [int](https://docs.python.org/3/builtins/functions.html#int) = 32, max_leaf_samples: [int](https://docs.python.org/3/builtins/functions.html#int) = 32, growth_criterion: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['fixed', 'adaptive'] = 'adaptive', subsample: [float](https://docs.python.org/3/builtins/functions.html#float) = 1.0, window_size: [int](https://docs.python.org/3/builtins/functions.html#int) = 2048, branching_factor: [int](https://docs.python.org/3/builtins/functions.html#int) = 2, split: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)['axisparallel'] = 'axisparallel', n_jobs: [int](https://docs.python.org/3/builtins/functions.html#int) = 1)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_online_isolation_forest.py#L64)

Construct an Online Isolation Forest anomaly detector

* **Parameters:**
  * **schema** – The schema of the stream. If not provided, it will be inferred from the data.
  * **random_seed** – Random seed for reproducibility.
  * **num_trees** – Number of trees in the ensemble.
  * **window_size** – The size of the window for each tree.
  * **branching_factor** – Branching factor of each tree.
  * **max_leaf_samples** – Maximum number of samples per leaf. When this number is reached, a split is performed.
  * **growth_criterion** – When to perform a split. If ‘adaptive’, the max_leaf_samples grows with tree depth,
    otherwise ‘fixed’.
  * **subsample** – Probability of learning a new sample in each tree.
  * **split** – Type of split performed at each node. Currently only ‘axisparallel’ is supported, which is the
    same type used by the IsolationForest algorithm.
  * **n_jobs** – Number of parallel jobs.

#### *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: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [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/_online_isolation_forest.py#L126)

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

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: [Instance](capymoa.core.Instance.md#capymoa.core.Instance))[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_online_isolation_forest.py#L119)
