Choose a detector¶
Choose two or three candidates from the table, then compare them on your data. For interactive constraint filtering and auto-generated pipeline snippets across all detectors, open Choosing Anomaly Detector or try candidates live in the PySAD Space Playground.
Find candidates¶
In the table, a window is the set of the most recent points that a detector keeps.
Your data |
Start with |
Why |
|---|---|---|
One series of values with daily or weekly cycles |
Removes the cycle. Earlier anomalies in the window have little effect on the detector |
|
One series of values without a strong cycle |
Anomalies in the window have little effect on the median absolute deviation (MAD). It uses only the last |
|
Several features, first try |
Is fast, has few parameters and learns from every point |
|
Features with known value ranges |
Uses the range of each feature. Takes constant time per point. Scores each point against the previous window |
|
Many features |
Projects the points to fewer features with hashing, so it can use many features |
|
Network traffic |
Its authors made it for live detection of network intrusions. With the defaults, it gives 0 for the first 100 points and stops learning after 1,100. Set |
|
A PyOD model that you already use |
Fits the model again on a sliding window. See Run a PyOD model on a stream |
These suggestions follow method design, not benchmark rank. See Detectors for inputs, memory and papers, and benchmark for multivariate results on 17 datasets.
Compare the candidates¶
Compare candidates on labeled data using a metric such as AUROC, which measures ranking quality. This example compares four detectors on cardio:
from pysad.evaluation import AUROCMetric
from pysad.models import HalfSpaceTrees, LODA, RSHash, xStream
from pysad.utils import ArrayStreamer, Data, get_minmax_array
X, y = Data().get_data("cardio.mat")
mins, maxes = get_minmax_array(X)
candidates = {
"LODA": LODA(random_state=0),
"xStream": xStream(random_state=0),
"HalfSpaceTrees": HalfSpaceTrees(feature_mins=mins, feature_maxes=maxes, random_state=0),
"RSHash": RSHash(feature_mins=mins, feature_maxes=maxes, random_state=0),
}
for name, model in candidates.items():
metric = AUROCMetric()
for x, label in ArrayStreamer().iter(X, y):
metric.update(label, model.fit_score_partial(x))
print(f"{name:>15}: AUROC {metric.get():.3f}")
LODA: AUROC 0.946
xStream: AUROC 0.859
HalfSpaceTrees: AUROC 0.928
RSHash: AUROC 0.700
This example preserves file order, with all cardio anomalies at the end, and scores before learning. Results depend on the data. Repeat the comparison on your stream.
HalfSpaceTrees and RSHash require feature ranges. Compute them with get_minmax_array(). In production, use historical data or known physical limits.
Follow these rules to make the comparison fair:
Use the same stream, in the same order, for every detector.
Fix
random_state. Then repeat the comparison with three to five seeds (values ofrandom_state), because randomized detectors give different results for each seed.If you have no labels, add synthetic anomalies to a copy of your data. You can also label some of the alerts (points with a high score) by hand.
Evaluate a detector explains the metrics.
Tune the parameters¶
Start with the defaults. If tuning is needed, focus on these parameters:
window_sizeThe number of recent points that the detector uses. A larger window gives more stable scores. A smaller window follows changes in the data faster.
num_trees,num_random_cuts,n_chains,num_componentsEnsemble size: larger ensembles generally stabilize scores but cost more time. RSHash uses
num_components. xStream usesn_chainsfor ensemble size andnum_componentsfor projected features.periodFor seasonal detectors, the number of points in one cycle. For example, use 24 for hourly data with a daily cycle.
Next steps¶
Evaluate a detector explains the metrics for the comparison.
Turn scores into alerts shows how to turn the scores into alerts.