API CheatSheet
The following APIs are applicable for all detector models for easy use.
deepod.core.base_model.BaseDeepAD.fit(): Fit detector. y is ignored in unsupervised methods.deepod.core.base_model.BaseDeepAD.decision_function(): Predict raw anomaly score of X using the fitted detector.deepod.core.base_model.BaseDeepAD.predict(): Predict if a particular sample is an outlier or not using the fitted detector.
Key Attributes of a fitted model:
deepod.core.base_model.BaseDeepAD.decision_scores_: The outlier scores of the training data. The higher, the more abnormal. Outliers tend to have higher scores.deepod.core.base_model.BaseDeepAD.labels_: The binary labels of the training data. 0 stands for inliers and 1 for outliers/anomalies.
See base class definition below:
deepod.core.base_model module
Base class for deep Anomaly detection models some functions are adapted from the pyod library @Author: Hongzuo Xu <hongzuoxu@126.com, xuhongzuo13@nudt.edu.cn>
- class deepod.core.base_model.BaseDeepAD(model_name, data_type='tabular', network='MLP', epochs=100, batch_size=64, lr=0.001, n_ensemble=1, seq_len=100, stride=1, epoch_steps=-1, prt_steps=10, device='cuda', contamination=0.1, verbose=1, random_state=42)[source]
Bases:
objectAbstract class for deep outlier detection models
- Parameters:
data_type (str, optional (default='tabular')) – Data type, choice = [‘tabular’, ‘ts’]
network (str, optional (default='MLP')) – network structure for different data structures
epochs (int, optional (default=100)) – Number of training epochs
batch_size (int, optional (default=64)) – Number of samples in a mini-batch
lr (float, optional (default=1e-3)) – Learning rate
n_ensemble (int or str, optional (default=1)) – Number of ensemble size
seq_len (int, optional (default=100)) – Size of window used to create subsequences from the data deprecated when handling tabular data (network==’MLP’)
stride (int, optional (default=1)) – number of time points the window will move between two subsequences deprecated when handling tabular data (network==’MLP’)
epoch_steps (int, optional (default=-1)) –
- Maximum steps in an epoch
If -1, all the batches will be processed
prt_steps (int, optional (default=10)) – Number of epoch intervals per printing
device (str, optional (default='cuda')) – torch device,
contamination (float in (0., 0.5), optional (default=0.1)) – The amount of contamination of the data set, i.e. the proportion of outliers in the data set. Used when fitting to define the threshold on the decision function.
verbose (int, optional (default=1)) – Verbosity mode
random_state (int, optional (default=42)) – the seed used by the random
- decision_scores_
The outlier scores of the training data. The higher, the more abnormal. Outliers tend to have higher scores. This value is available once the detector is fitted.
- Type:
numpy array of shape (n_samples,)
- threshold_
The threshold is based on
contamination. It is then_samples * contaminationmost abnormal samples indecision_scores_. The threshold is calculated for generating binary outlier labels.- Type:
- labels_
The binary labels of the training data. 0 stands for inliers and 1 for outliers/anomalies. It is generated by applying
threshold_ondecision_scores_.- Type:
int, either 0 or 1
- decision_function(X, return_rep=False)[source]
Predict raw anomaly scores of X using the fitted detector.
The anomaly score of an input sample is computed based on the fitted detector. For consistency, outliers are assigned with higher anomaly scores.
- Parameters:
X (numpy array of shape (n_samples, n_features)) – The input samples. Sparse matrices are accepted only if they are supported by the base estimator.
return_rep (boolean, optional, default=False) – whether return representations
- Returns:
anomaly_scores – The anomaly score of the input samples.
- Return type:
numpy array of shape (n_samples,)
- fit(X, y=None)[source]
Fit detector. y is ignored in unsupervised methods.
- Parameters:
X (numpy array of shape (n_samples, n_features)) – The input samples.
y (numpy array of shape (n_samples, )) – Not used in unsupervised methods, present for API consistency by convention. used in (semi-/weakly-) supervised methods
- Returns:
self – Fitted estimator.
- Return type:
- fit_auto_hyper(X, y=None, X_test=None, y_test=None, n_ray_samples=5, time_budget_s=None)[source]
Fit detector. y is ignored in unsupervised methods.
- Parameters:
X (numpy array of shape (n_samples, n_features)) – The input samples.
y (numpy array of shape (n_samples, )) – Not used in unsupervised methods, present for API consistency by convention. used in (semi-/weakly-) supervised methods
X_test (numpy array of shape (n_samples, n_features), default=None) – The input testing samples for hyper-parameter tuning.
y_test (numpy array of shape (n_samples, ), default=None) – Label of input testing samples for hyper-parameter tuning.
n_ray_samples (int, default=5) – Number of times to sample from the hyperparameter space
time_budget_s (int, default=None) – Global time budget in seconds after which all trials of Ray are stopped.
- Returns:
config – tuned hyper-parameter
- Return type:
- predict(X, return_confidence=False)[source]
Predict if a particular sample is an outlier or not.
- Parameters:
X (numpy array of shape (n_samples, n_features)) – The input samples.
return_confidence (boolean, optional(default=False)) – If True, also return the confidence of prediction.
- Returns:
outlier_labels (numpy array of shape (n_samples,)) – For each observation, tells whether it should be considered as an outlier according to the fitted model. 0 stands for inliers and 1 for outliers.
confidence (numpy array of shape (n_samples,).) – Only if return_confidence is set to True.