Thresholder
Contents
Thresholder#
Thresholder can help you to normalize the score into a range of 0 to 1, where 1 represents the anomalies.
All thresholders can work in two ways:
Global way (with parameter
is_global = True): It can “remember” the distribution of all history scores with little memory usage.Rolling way (with parameter
is_global = Flase): It only keeps the score within a rolling window, and uses them as references.
ZScore Thresholder#
It reports the scores that beyond k times (sigma) of standard deviation as anomalies.
from streamad.util import StreamGenerator, UnivariateDS, plot
from streamad.model import KNNDetector
from streamad.process import ZScoreThresholder
ds = UnivariateDS()
stream = StreamGenerator(ds.data)
detector=KNNDetector()
thresholder = ZScoreThresholder(sigma=3)
scores = []
for x in stream.iter_item():
score = detector.fit_score(x)
normalized_score = thresholder.normalize(score)
scores.append(normalized_score)
data, label, date, features = ds.data, ds.label, ds.date, ds.features
plot(data=data,scores=scores,date=date,features=features,label=label)
TDigest Thresholder#
It reports the scores that beyond percentile_up and below percentile_down as anomalies.
from streamad.util import StreamGenerator, UnivariateDS, plot
from streamad.model import KNNDetector
from streamad.process import TDigestThresholder
ds = UnivariateDS()
stream = StreamGenerator(ds.data)
detector = KNNDetector()
thresholder = TDigestThresholder(percentile_up=99, percentile_down=0)
scores = []
for x in stream.iter_item():
score = detector.fit_score(x)
normalized_score = thresholder.normalize(score)
scores.append(normalized_score)
data, label, date, features = ds.data, ds.label, ds.date, ds.features
plot(data=data,scores=scores,date=date,features=features,label=label)