RSHash#

class capymoa.anomaly.RSHash[source]#

Bases: AnomalyDetector

RS-Hash: subspace outlier detection in linear time with randomized hashing.

The paper describes two streaming variants (Section III): a sliding window and time-decayed scores. This module implements the sliding window, which the paper notes is straightforward because the count-min sketch supports both insertion and deletion.

Reference: Sathe, S. and Aggarwal, C. C. (2016). Subspace Outlier Detection in Linear Time with Randomized Hashing. IEEE ICDM, pp. 459-468.

Example: >>> from capymoa.datasets import ElectricityTiny >>> from capymoa.anomaly import RSHash >>> from capymoa.evaluation import AnomalyDetectionEvaluator >>> stream = ElectricityTiny() >>> schema = stream.get_schema() >>> learner = RSHash(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.61

__init__(
schema: Schema,
m: int = 300,
s: int = 1000,
w: int = 4,
p: int = 10_000,
seed: int = 42,
)[source]#

Construct an RS-Hash anomaly detector.

Parameters:
  • schema – Schema of the stream.

  • m – Number of ensemble components.

  • s – Sliding window length.

  • w – Number of hash tables per component.

  • p – Hash range per component.

  • seed – Random seed.

predict(instance: Instance) int | None[source]#
score_instance(instance: Instance) float[source]#

Return the anomaly score for the given instance.

Higher values indicate more anomalous instances.

RS-Hash reports a normality score, so this method multiplies the ensemble average by -1 to stay consistent with the other detectors. Instances arriving before the window has filled score 0.0.

Parameters:

instance – The instance to score.

Returns:

The anomaly score.

train(instance: Instance) None[source]#