Anomaly Detection#

This notebook shows some basic usage of CapyMOA for Anomaly Detection tasks.


More information about CapyMOA can be found in https://www.capymoa.org

last update on 05/06/2024

1. Unsupervised Anomaly Detection for data streams#

  • Recent research has been focused on unsupervised anomaly detection for data streams, as it is often difficult to obtain labeled data for training.

  • Instead of using evaluation functions such as test-then-train loop, we will show a basic loop from scratch to evaluate the model’s performance.

  • Please notice that lower scores indicate higher anomaly likelihood.

[1]:
from capymoa.datasets import ElectricityTiny
from capymoa.anomaly import HalfSpaceTrees
from capymoa.evaluation import AUCEvaluator
stream = ElectricityTiny()
schema = stream.get_schema()
learner = HalfSpaceTrees(schema)
evaluator = AUCEvaluator(schema)
while stream.has_more_instances():
    instance = stream.next_instance()
    score = learner.score_instance(instance)
    evaluator.update(instance.y_index, score)
    learner.train(instance)

auc = evaluator.auc()
print(f"AUC: {auc:.2f}")
AUC: 0.54