# `HalfSpaceTrees`

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

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

Half-Space Trees

This class implements the Half-Space Trees (HS-Trees) algorithm, which is
an ensemble anomaly detector capable of adapting to concept drift.

HS-Trees is implemented in MOA (Massive Online Analysis) and provides several
parameters for customization.

References:

[Fast anomaly detection for streaming data.
Swee Chuan Tan, Kai Ming Ting and Tony Fei Liu.
International joint conference on artificial intelligence (IJCAI), 106, 1469-1495, 2017.](https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=73b6b7d9e7e225719ad86234927a3b60a4a873c0)

Example:

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

#### \_\_init_\_(schema=None, CLI=None, random_seed=1, window_size=250, number_of_trees=25, max_depth=15, anomaly_threshold=0.5, size_limit=0.1)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_half_space_trees.py#L44)

Construct a Half-Space Trees anomaly detector

* **Parameters:**
  * **schema** – The schema of the stream. If not provided, it will be inferred from the data.
  * **CLI** – Command Line Interface (CLI) options for configuring the HS-Trees algorithm.
  * **random_seed** – Random seed for reproducibility.
  * **window_size** – The size of the window for each tree.
  * **number_of_trees** – The number of trees in the ensemble.
  * **max_depth** – The maximum depth of each tree.

#### cli_help()[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_base.py#L103)

#### *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/base/_base.py#L109)

#### score_instance(instance)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_base.py#L114)

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)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_base.py#L106)
