apache_beam.ml.anomaly.detectors.zscore module

class apache_beam.ml.anomaly.detectors.zscore.ZScore(*args, **kwargs)[source]

Bases: AnomalyDetector

Z-Score anomaly detector.

This class implements an anomaly detection algorithm based on Z-Score (also known as Standard Score [1] ), which measures how many standard deviations a data point is from the mean.

The score is calculated as: | (value - mean) / stdev |

Important

In the streaming setting, we use the online version of mean and standard deviation in the calculation.

This implementation is adapted from the implementations within PySAD [2] and River [3]:

Parameters:
  • sub_stat_tracker – Optional MeanTracker instance. If None, an IncSlidingMeanTracker with a default window size 1000 is created.

  • stdev_tracker – Optional StdevTracker instance. If None, an IncSlidingStdevTracker with a default window size 1000 is created.

  • threshold_criterion – threshold_criterion: Optional ThresholdFn to apply on the score. Defaults to FixedThreshold(3) due to the commonly used 3-sigma rule.

  • **kwargs – Additional keyword arguments.

learn_one(x: Row) None[source]

Updates the mean and standard deviation trackers with a new data point.

Parameters:

x – A beam.Row containing a single numerical value.

score_one(x: Row) float | None[source]

Scores a data point using the Z-Score.

Parameters:

x – A beam.Row containing a single numerical value.

Returns:

The Z-Score.

Return type:

float | None

ZScore__spec_type = 'ZScore'
classmethod from_spec(spec: Spec, _run_init: bool = True) Self | type[Self]

Generate a Specifiable subclass object based on a spec.

Parameters:
  • spec – the specification of a Specifiable subclass object

  • _run_init – whether to call __init__ or not for the initial instantiation

Returns:

the Specifiable subclass object

Return type:

Self

run_original_init() None

Execute the original __init__ method with its saved arguments.

For instances of the Specifiable class, initialization is deferred (lazy initialization). This function forces the execution of the original __init__ method using the arguments captured during the object’s initial instantiation.

classmethod spec_type()
to_spec() Spec

Generate a spec from a Specifiable subclass object.

Returns:

The specification of the instance.

Return type:

Spec

classmethod unspecifiable()