# `StreamRHF`

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

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

StreamRHF anomaly detector

StreamRHF: Streaming Random Histogram Forest for Anomaly Detection

StreamRHF is an unsupervised anomaly detection algorithm tailored for
real-time data streams. Building upon the principles of Random Histogram
Forests (RHF), this algorithm extends its capabilities to handle dynamic
data streams efficiently. StreamRHF combines the power of tree-based
partitioning with kurtosis-driven feature selection to detect anomalies
in a resource-constrained streaming environment.

Reference:

[STREAMRHF: Tree-Based Unsupervised Anomaly Detection for Data Streams.
Stefan Nesic, Andrian Putina, Maroua Bahri, Alexis Huet, Jose Manuel Navarro, Dario Rossi, Mauro Sozio.](https://nonsns.github.io/paper/rossi22aiccsa.pdf)

Example:

```pycon
>>> from capymoa.anomaly.datasets import TinyBlobs
>>> from capymoa.anomaly import StreamRHF
>>> from capymoa.evaluation import AnomalyDetectionEvaluator
>>> stream = TinyBlobs()
>>> schema = stream.get_schema()
>>> learner = StreamRHF(schema=schema, num_trees=5, max_height=3)
>>> 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.82
```

#### \_\_init_\_(schema, max_height=5, num_trees=100, window_size=20, random_seed=0)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_stream_rhf.py#L295)

Initialize the StreamRHF learner.
:param schema: Schema of the data stream.
:param max_height: Maximum height of the trees.
:param num_trees: Number of trees in the forest.
:param window_size: Size of the sliding window.
:param random_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)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_stream_rhf.py#L334)

Predict anomaly score for a single instance.
This method uses the anomaly score of the instance to classify it
as normal (0) or anomalous (1) based on a threshold.
:param instance: An instance from the stream.
:return: 0 if the instance is classified as normal, 1 if classified as anomalous.

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

Score a single instance.
A score value close to 1 means that is an anomaly and close to 0 it means it is a normal instance
:param instance: An instance from the stream.
:return: Anomaly score for the instance.

#### train(instance)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_stream_rhf.py#L326)

Train the learner with a single instance.
:param instance: An instance from the stream.
