Utils for Anomaly Detection¶
Common functions used by anomaly_model.py, scorers.py, aggregators.py and detectors.py
- darts.ad.utils.eval_accuracy_from_binary_prediction(actual_anomalies, binary_pred_anomalies, window=1, metric='recall')[source]¶
Score the results against true anomalies.
- checks that pred_anomalies and actual_anomalies are the same:
type,
length,
number of components
binary and has values belonging to the two classes (1 and 0)
If one series is given for actual_anomalies and pred_anomalies contains more than one series, the function will consider actual_anomalies as the true anomalies for all scores in anomaly_score.
- Parameters
actual_anomalies (
Union
[TimeSeries
,Sequence
[TimeSeries
]]) – The (sequence of) ground truth of the anomalies (1 if it is an anomaly and 0 if not)binary_pred_anomalies (
Union
[TimeSeries
,Sequence
[TimeSeries
]]) – Anomaly predictions.window (
Union
[int
,Sequence
[int
]]) – Integer value indicating the number of past samples each point represents in the pred_anomalies. The parameter will be used to transform actual_anomalies. If a list is given. the length must match the number of series in pred_anomalies and actual_anomalies. If only one window is given, the value will be used for every series in pred_anomalies and actual_anomalies.metric (
str
) – Optionally, Scoring function to use. Must be one of “recall”, “precision”, “f1”, and “accuracy”. Default: “recall”
- Returns
Score of the anomalies prediction
float
if binary_pred_anomalies is a univariate series (dimension=1).Sequence[float]
if binary_pred_anomalies is a multivariate series (dimension>1), returns one value per dimension.
if binary_pred_anomalies is a sequence of univariate series, returns one value per series
Sequence[Sequence[float]]
if binary_pred_anomalies is a sequence of multivariate series. Outer Sequence is over the sequence input, and the inner Sequence is over the dimensions of each element in the sequence input.
- Return type
Union[float, Sequence[float], Sequence[Sequence[float]]]
- darts.ad.utils.eval_accuracy_from_scores(actual_anomalies, anomaly_score, window=1, metric='AUC_ROC')[source]¶
Scores the results against true anomalies.
actual_anomalies and anomaly_score must have the same shape. actual_anomalies must be binary and have values belonging to the two classes (0 and 1).
If one series is given for actual_anomalies and anomaly_score contains more than one series, the function will consider actual_anomalies as the ground truth anomalies for all scores in anomaly_score.
- Parameters
actual_anomalies (
Union
[TimeSeries
,Sequence
[TimeSeries
]]) – The ground truth of the anomalies (1 if it is an anomaly and 0 if not).anomaly_score (
Union
[TimeSeries
,Sequence
[TimeSeries
]]) – Series indicating how anomoulous each window of size w is.window (
Union
[int
,Sequence
[int
]]) – Integer value indicating the number of past samples each point represents in the anomaly_score. The parameter will be used by the function_window_adjustment_anomalies()
to transform actual_anomalies. If a list is given. the length must match the number of series in anomaly_score and actual_anomalies. If only one window is given, the value will be used for every series in anomaly_score and actual_anomalies.metric (
str
) – Optionally, Scoring function to use. Must be one of “AUC_ROC” and “AUC_PR”. Default: “AUC_ROC”
- Returns
- Score of the anomalies score prediction
float
if anomaly_score is a univariate series (dimension=1).Sequence[float]
if anomaly_score is a multivariate series (dimension>1), returns one value per dimension.
if anomaly_score is a sequence of univariate series, returns one value per series
Sequence[Sequence[float]]
if anomaly_score is a sequence of multivariate series. Outer Sequence is over the sequence input, and the inner Sequence is over the dimensions of each element in the sequence input.
- Return type
Union[float, Sequence[float], Sequence[Sequence[float]]]
- darts.ad.utils.show_anomalies_from_scores(series, model_output=None, anomaly_scores=None, window=1, names_of_scorers=None, actual_anomalies=None, title=None, metric=None)[source]¶
Plot the results generated by an anomaly model.
- The plot will be composed of the following:
the series itself with the output of the model (if given)
the anomaly score of each scorer. The scorer with different windows will be separated.
the actual anomalies, if given.
- If model_output is stochastic (i.e., if it has multiple samples), the function will plot:
the mean per timestamp
the quantile 0.95 for an upper bound
the quantile 0.05 for a lower bound
- Possible to:
add a title to the figure with the parameter title
give personalized names for the scorers with names_of_scorers
show the results of a metric for each anomaly score (AUC_ROC or AUC_PR), if the actual anomalies is given
- Parameters
series (
TimeSeries
) – The series to visualize anomalies from.model_output (
Optional
[TimeSeries
]) – Output of the model given as input the series (can be stochastic).anomaly_scores (
Union
[TimeSeries
,Sequence
[TimeSeries
],None
]) – Output of the scorers given the output of the model and the series.window (
Union
[int
,Sequence
[int
]]) – Window parameter for each anomaly scores. Default: 1. If a list of anomaly scores is given, the same default window will be used for every score.names_of_scorers (
Union
[str
,Sequence
[str
],None
]) – Name of the scores. Must be a list of length equal to the number of scorers in the anomaly_model.actual_anomalies (
Optional
[TimeSeries
]) – The ground truth of the anomalies (1 if it is an anomaly and 0 if not)title (
Optional
[str
]) – Title of the figuremetric (
Optional
[str
]) – Optionally, Scoring function to use. Must be one of “AUC_ROC” and “AUC_PR”. Default: “AUC_ROC”