# `Loda`

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

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

Loda: Lightweight on-line detector of anomalies
We implement a streaming version of Loda that updates the histograms after every window of instances.

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

Reference:
: Pevný, T. (2016). Loda: Lightweight on-line detector of anomalies. Machine Learning, 102(2), 275-304.

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema), n_projections: [int](https://docs.python.org/3/builtins/functions.html#int) = 100, window_size: [int](https://docs.python.org/3/builtins/functions.html#int) = 256, max_bins: [str](https://docs.python.org/3/builtins/stdtypes.html#str) | [int](https://docs.python.org/3/builtins/functions.html#int) = 'auto', random_state: [int](https://docs.python.org/3/builtins/functions.html#int) = 42)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_loda.py#L85)

Initialize the Loda anomaly detector.

* **Parameters:**
  * **schema** – Schema of the data stream.
  * **n_projections** – Number of random projection histograms in the ensemble.
  * **window_size** – Number of recent instances used to fit each histogram.
  * **max_bins** – Upper bound on bins per histogram. `"auto"` uses `floor(n / log n)`
    via the Birge-Rozenholc criterion; an integer caps the search at that value.
  * **random_state** – 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: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [int](https://docs.python.org/3/builtins/functions.html#int)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/anomaly/_loda.py#L190)

#### 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/_loda.py#L167)

Return the anomaly score for a single instance.

Computes the average negative log-likelihood of the instance under
the ensemble of one-dimensional histograms. Higher scores indicate
greater anomalousness. Returns `0.0` before the first full window
of `window_size` instances has been seen.

* **Parameters:**
  **instance** – The instance to score.
* **Returns:**
  Anomaly score. Ranges from 0.0 (least anomalous) to infinity (most anomalous).

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

Train on a single instance.

Projects the instance onto each random projection vector and adds it to
the circular window. Histograms are rebuilt once every `window_size`
instances using the Birge-Rozenholc criterion to select the optimal
number of bins.

* **Parameters:**
  **instance** – The instance to train on.
