Loda#
- class capymoa.anomaly.Loda[source]#
Bases:
AnomalyDetectorLoda: Lightweight on-line detector of anomalies We implement a streaming version of Loda that updates the histograms after every window of instances.
>>> from capymoa.datasets import ElectricityTiny >>> from capymoa.anomaly import Loda >>> from capymoa.evaluation import AnomalyDetectionEvaluator >>> stream = ElectricityTiny() >>> 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.65
- Reference:
Pevný, T. (2016). Loda: Lightweight on-line detector of anomalies. Machine Learning, 102(2), 275-304.
- __init__(
- schema: Schema,
- n_projections: int = 100,
- window_size: int = 256,
- max_bins: str | int = 'auto',
- random_state: int = 42,
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"usesfloor(n / log n)via the Birge-Rozenholc criterion; an integer caps the search at that value.random_state – Random seed for reproducibility.
- score_instance(instance: Instance) float[source]#
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.0before the first full window ofwindow_sizeinstances 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)[source]#
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_sizeinstances using the Birge-Rozenholc criterion to select the optimal number of bins.- Parameters:
instance – The instance to train on.