anomaly_scores
Anomaly scores that reduce per-feature reconstruction errors to one score per sample.
After an autoencoder reconstructs its input, an AnomalyScore
aggregates the per-feature reconstruction errors into a single scalar per sample.
Samples with high scores are candidate anomalies.
Each scorer subclasses AnomalyScore and is selected
via the train.anomaly_score config key.
- class MahalanobisScore(pca=<object object>, pca_min_var=0.9, mcd_support_fraction=0.9, scale=<object object>, covariance_method='auto', min_cov_det_ratio=2.0, shrinkage_method='oas')[source]
Bases:
AnomalyScoreCalculate Mahalanobis scores on the reconstruction errors.
This class implements the mahalanobis norm calculation based on multiple variants of covariance matrix estimation. The cases are defined by the user-set parameter
pcaand the relation of the number of samplesnand the number of featuresp:1. (
pca=False) and n>=min_cov_det_ratio * p: Estimates the covariance of the mean-centred reconstruction errors using the MinCovDet algorithm and returns the Mahalanobis distance of each sample. 2. (pca=False) and n<min_cov_det_ratio * p: Estimates the covariance of the mean-centred reconstruction errors using shrinkage based algorithms like Ledoit-Wolf or OAS. The estimator can be specified via the parametercovariance_methodand automatically falls back to a shrinkage estimator (Ledoit-Wolf/OAS) when MinCovDet would become unstable. 3. (pca=True): A PCA is carried out on standardized reconstruction errors to reduce the dimensionality. MinCovDet is then used to estimate the Covariance matrix in order to compute the Mahalanobis distance of each sample. This method is deprecated because it does not consider all correlations in the reconstruction errors which might lead to less useful anomaly scores.Deprecated since version The:
pca=Truepath is deprecated and will be replaced by full covariance matrix estimators. It is kept for backwards compatibility; usepca=Falsefor the direct full-dimensionality Mahalanobis distance. Thescaleparameter is deprecated and no longer has any effect.- Parameters:
pca (
bool) – deprecated. If True, standardize, reduce dimensionality via PCA and estimate the covariance with MinCovDet (legacy behaviour). If False, estimate the covariance on the full-dimensionality mean-centred errors. DefaultTrue.scale (
bool) – deprecated, no longer has any effect.pca_min_var (
float) – parameter for PCA, variance to keep. Default 0.9mcd_support_fraction (
float) – parameter for Minimum Covariance Determinant estimation. Default 0.9covariance_method (
str) –which covariance estimator to use in the (
pca=False) path. One of:"auto"(default): use MinCovDet whenn / p >= min_cov_det_ratioand the resulting covariance is stable (full rank / well conditioned), otherwise fall back to a shrinkage estimator (shrinkage_method)."min_cov_det": always use MinCovDet. May raise or be unstable whennis not sufficiently larger thanp."shrinkage": always use a shrinkage estimator (shrinkage_method).
min_cov_det_ratio (
float) – minimum sample-to-feature ration / prequired (in"auto"mode) for MinCovDet to be used instead of shrinkage. Default2.0.shrinkage_method (
str) – which shrinkage estimator to use. One of"oas"(default) or"ledoit_wolf".
Configuration example:
train: anomaly_score: name: mahalanobis params: pca: False covariance_method: auto min_cov_det_ratio: 2.0 shrinkage_method: oas
- fit(x, y=None)[source]
Fit the covariance estimator to determine Mahalanobis distance.
- Parameters:
x (
Union[DataFrame,ndarray]) – numpy 2d array or pandas DataFrame with differences between prediction and actual sensor values.y (
Optional[Series]) – not used, labels indicating whether sample is normal (True) or anomalous (False).
- Return type:
- standardize(x)[source]
Standardization of the reconstruction error in x (only used in the deprecated PCA path).
- transform(x)[source]
Calculate Mahalanobis distance from x.
- Parameters:
x (
Union[DataFrame,ndarray]) – numpy 2d array or pandas Dataframe with differences between prediction and actual sensor values- Return type:
Series- Returns:
Mahalanobis distance for each sample. Output is a pandas Series if input was a pandas DataFrame
- class RMSEScore(scale=<object object>, **kwargs)[source]
Bases:
AnomalyScoreCalculate the RMSE of given reconstruction errors.
Deprecated since version The:
scaleparameter no longer has any effect and is only accepted for backwards compatibility. It will be removed in a future version.Configuration example:
train: anomaly_score: name: rmse
- fit(x, y=None)[source]
Calculate standard deviation and mean on training data
- Parameters:
x (
Union[DataFrame,ndarray]) – numpy 2d array with differences between prediction and actual sensor valuesy (
Optional[Series]) – not used, labels indicating whether sample is normal (True) or anomalous (False).
- Return type: