Graph Detectors¶
PyOD has 8 graph anomaly detectors built on PyTorch Geometric (pip install pyod[graph]). All transductive in v1. Rankings from BOND (NeurIPS 2022).
See Layer 1: Graph Anomaly Detection for usage and input format.
pyod.models.pyg_scan module¶
SCAN: Structural Clustering Algorithm for Networks.
Detects anomalous nodes based on structural similarity to neighbors. Nodes with low average structural similarity to their neighbors are scored as anomalous. This is a structure-only method — node features are not used.
See [MXYFS07] for details.
- Reference:
Xu, X., Yuruk, N., Feng, Z. and Schweiger, T.A.J., 2007. SCAN: A Structural Clustering Algorithm for Networks. In KDD, pp. 824-833.
- class pyod.models.pyg_scan.SCAN(epsilon=0.5, mu=2, contamination=0.1)[source]¶
Bases:
BaseDetectorSCAN: Structural Clustering Algorithm for Networks.
Implements the full SCAN procedure: computes structural similarity
sigma(u,v) = |N[u] ∩ N[v]| / sqrt(|N[u]| * |N[v]|)between neighbors, identifies cores (nodes with at leastmuepsilon-neighbors), clusters cores via BFS reachability on epsilon-edges, and classifies remaining nodes as hubs (adjacent to 2+ clusters) or outliers.Continuous anomaly score is derived from this classification: cores receive low scores (inversely proportional to their epsilon-neighbor count), hubs receive medium scores, and outliers receive the highest scores.
This detector is transductive.
Parameters¶
- epsilonfloat, default=0.5
Structural similarity threshold. An edge (u,v) is an epsilon-edge if
sigma(u,v) >= epsilon.- muint, default=2
Minimum number of epsilon-neighbors for a node to be a core.
- contaminationfloat, default=0.1
Expected proportion of anomalies in the dataset.
Attributes¶
- decision_scores_numpy array of shape (n_nodes,)
Anomaly scores. Higher = more anomalous.
- labels_numpy array of shape (n_nodes,)
Binary labels (0=inlier, 1=outlier).
- threshold_float
Score threshold derived from contamination.
Examples¶
>>> from torch_geometric.data import Data >>> import torch >>> data = Data(edge_index=torch.tensor([[0,1,1,2],[1,0,2,1]]), ... num_nodes=3) >>> clf = SCAN(contamination=0.3) >>> clf.fit(data) >>> clf.decision_scores_
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
- XData or array-like
PyG Data object, or node features (n_nodes, n_features). For SCAN, node features are ignored — only structure is used. When passing a structure-only graph as Data, set
data.num_nodesexplicitly.
y : ignored
- edge_indexarray-like or None
COO edge list. Required when X is numpy.
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') SCAN¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') SCAN¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') SCAN¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_radar module¶
Radar: Residual Analysis for Anomaly Detection in Attributed Networks.
Models node attributes as X ≈ WX + R, where W is a learned n x n weight matrix that predicts each node’s attributes from other nodes’ attributes, and R is the residual. The adjacency enters only through the graph Laplacian regularizer on R. Anomaly scores are the L2 norms of residual rows (nodes with large residuals are anomalous).
See [MLDHL17] for details.
- Reference:
Li, J., Dani, H., Hu, X. and Liu, H., 2017. Radar: Residual Analysis for Anomaly Detection in Attributed Networks. In IJCAI, pp. 2152-2158.
- class pyod.models.pyg_radar.Radar(alpha=1.0, gamma=0.01, max_iter=100, contamination=0.1)[source]¶
Bases:
BaseDetectorRadar: Residual Analysis for Anomaly Detection.
Solves
min_{W,R} ||X - WX - R||_F^2 + alpha * ||W||_F^2 + gamma * (||R||_{2,1} + tr(R^T L R))via alternating optimization. W is a learned n x n weight matrix (not the adjacency). The graph Laplacian L smooths the residual R. Anomaly score per node = L2 norm of residual row.This detector is transductive.
Parameters¶
- alphafloat, default=1.0
Frobenius norm penalty on W (regularization).
- gammafloat, default=0.01
Residual regularization strength: controls both L21 row-sparsity and graph Laplacian smoothing on R. Scale-sensitive; typical range 0.001-0.1.
- max_iterint, default=100
Number of alternating optimization iterations.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
- XData or array-like
PyG Data or node features (n_nodes, n_features).
y : ignored edge_index : array-like or None
COO edge list. Required when X is numpy.
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') Radar¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') Radar¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') Radar¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_anomalous module¶
ANOMALOUS: A Joint Modeling Approach for Anomaly Detection.
CUR-decomposition-based method that models attributes as
X ≈ XWX + R, where W (d x n) jointly selects representative
attributes and nodes. The residual R captures anomalies via L21
sparsity, regularized by the graph Laplacian for network coherence.
See [MPLL+18] for details.
- Reference:
Peng, Z., Luo, M., Li, J., Liu, H. and Zheng, Q., 2018. ANOMALOUS: A Joint Modeling Approach for Anomaly Detection on Attributed Networks. In IJCAI, pp. 3529-3535.
- class pyod.models.pyg_anomalous.ANOMALOUS(alpha=1.0, gamma=1.0, lambda_r=0.01, max_iter=100, contamination=0.1)[source]¶
Bases:
BaseDetectorANOMALOUS: Joint Modeling for Anomaly Detection.
CUR-style decomposition:
min_{W,R} ||X - XWX - R||_F^2 + alpha * ||W||_F^2 + lambda_r * ||R||_{2,1} + gamma * tr(R^T L R)where W is d x n, L is the graph Laplacian. Anomaly score = L2 norm of each residual row.This detector is transductive.
Parameters¶
- alphafloat, default=1.0
Frobenius norm penalty on W (regularization).
- gammafloat, default=1.0
Graph Laplacian smoothing strength on R.
- lambda_rfloat, default=0.01
L21-norm penalty on residual R (row-sparsity). Scale-sensitive; typical range 0.001-0.1.
- max_iterint, default=100
Number of alternating optimization iterations.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
- XData or array-like
PyG Data or node features.
y : ignored edge_index : array-like or None
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') ANOMALOUS¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') ANOMALOUS¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') ANOMALOUS¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_dominant module¶
DOMINANT: Deep Anomaly Detection on Attributed Networks.
GCN autoencoder that jointly reconstructs the adjacency matrix and node attributes. Per-node anomaly score = weighted sum of structure and attribute reconstruction error.
See [MDLBL19] for details.
- Reference:
Ding, K., Li, J., Bhanushali, R. and Liu, H., 2019. Deep Anomaly Detection on Attributed Networks. In SDM, pp. 594-602.
- class pyod.models.pyg_dominant.DOMINANT(hidden_dim=64, num_layers=2, dropout=0.3, alpha=0.5, epochs=100, lr=0.005, contamination=0.1)[source]¶
Bases:
BaseDetectorDOMINANT: Deep Anomaly Detection on Attributed Networks.
GCN encoder maps nodes to embeddings. Structure decoder reconstructs adjacency via inner product. Attribute decoder reconstructs features via linear layer. Anomaly score per node =
alpha * struct_err + (1 - alpha) * attr_err.This detector is transductive.
Parameters¶
- hidden_dimint, default=64
Hidden dimension of GCN layers.
- num_layersint, default=2
Number of GCN encoder layers.
- dropoutfloat, default=0.3
Dropout rate during training.
- alphafloat, default=0.5
Weight for structure loss (1 - alpha for attribute loss).
- epochsint, default=100
Number of training epochs.
- lrfloat, default=5e-3
Learning rate.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- decision_function(X)[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¶
- Xnumpy array of shape (n_samples, n_features)
The input samples. Sparse matrices are accepted only if they are supported by the base estimator.
Returns¶
- anomaly_scoresnumpy array of shape (n_samples,)
The anomaly score of the input samples.
- predict(X, return_confidence=False)[source]¶
Predict if a particular sample is an outlier or not.
Parameters¶
- Xnumpy array of shape (n_samples, n_features)
The input samples.
- return_confidenceboolean, optional(default=False)
If True, also return the confidence of prediction.
Returns¶
- outlier_labelsnumpy 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.
- confidencenumpy array of shape (n_samples,).
Only if return_confidence is set to True.
- predict_confidence(X)[source]¶
Predict the model’s confidence in making the same prediction under slightly different training sets. See [MPVD20].
Parameters¶
- Xnumpy array of shape (n_samples, n_features)
The input samples.
Returns¶
- confidencenumpy array of shape (n_samples,)
For each observation, tells how consistently the model would make the same prediction if the training set was perturbed. Return a probability, ranging in [0,1].
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Predict the probability of a sample being outlier. Two approaches are possible:
simply use Min-max conversion to linearly transform the outlier scores into the range of [0,1]. The model must be fitted first.
use unifying scores, see [MKKSZ11].
Parameters¶
- Xnumpy array of shape (n_samples, n_features)
The input samples.
- methodstr, optional (default=’linear’)
probability conversion method. It must be one of ‘linear’ or ‘unify’.
- return_confidenceboolean, optional(default=False)
If True, also return the confidence of prediction.
Returns¶
- outlier_probabilitynumpy array of shape (n_samples, n_classes)
For each observation, tells whether or not it should be considered as an outlier according to the fitted model. Return the outlier probability, ranging in [0,1]. Note it depends on the number of classes, which is by default 2 classes ([proba of normal, proba of outliers]).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') DOMINANT¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') DOMINANT¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') DOMINANT¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_anomalydae module¶
AnomalyDAE: Dual Autoencoder for Anomaly Detection.
Attention-based structure encoder (GAT) and MLP attribute encoder, with separate decoders for each modality. Per-node anomaly score is the weighted sum of structure and attribute reconstruction error.
See [MFZL20] for details.
- Reference:
Fan, H., Zhang, F. and Li, Z., 2020. AnomalyDAE: Dual Autoencoder for Anomaly Detection on Attributed Networks. In CIKM, pp. 747-756.
- class pyod.models.pyg_anomalydae.AnomalyDAE(embed_dim=64, num_heads=4, alpha=0.5, dropout=0.3, epochs=100, lr=0.005, contamination=0.1)[source]¶
Bases:
BaseDetectorAnomalyDAE: Dual Autoencoder for Anomaly Detection.
Uses GATConv for structure encoding and an MLP for attribute encoding. Reconstructs adjacency via inner product and attributes via an MLP decoder.
This detector is transductive.
Parameters¶
- embed_dimint, default=64
Embedding dimension.
- num_headsint, default=4
Number of attention heads in GAT.
- alphafloat, default=0.5
Weight for structure loss.
- dropoutfloat, default=0.3
Dropout rate.
- epochsint, default=100
Number of training epochs.
- lrfloat, default=5e-3
Learning rate.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
X : Data or array-like y : ignored edge_index : array-like or None
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') AnomalyDAE¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') AnomalyDAE¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') AnomalyDAE¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_cola module¶
CoLA: Contrastive Self-Supervised Learning for Anomaly Detection.
Contrasts each node’s embedding against its local neighbor context (mean of neighbors’ embeddings). Nodes whose embeddings are indistinguishable from shuffled-feature embeddings are anomalous. Multi-round scoring for robustness.
See [MLLP+22] for details.
- Reference:
Liu, Y., Li, Z., Pan, S., Gool, T., Xiang, T. and Gong, B., 2022. Anomaly Detection on Attributed Networks via Contrastive Self-Supervised Learning. In WWW, pp. 2137-2147.
- class pyod.models.pyg_cola.CoLA(hidden_dim=64, num_layers=2, epochs=100, lr=0.001, contamination=0.1)[source]¶
Bases:
BaseDetectorCoLA: Contrastive Anomaly Detection on Attributed Networks.
GCN encoder maps nodes to embeddings. A bilinear discriminator scores how well a node’s embedding matches its local neighbor context (mean of neighbors’ embeddings). Nodes with low discriminator scores are anomalous.
This detector is transductive.
Parameters¶
- hidden_dimint, default=64
Hidden dimension of GCN.
- num_layersint, default=2
Number of GCN layers.
- epochsint, default=100
Training epochs.
- lrfloat, default=1e-3
Learning rate.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
X : Data or array-like y : ignored edge_index : array-like or None
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') CoLA¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') CoLA¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') CoLA¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_conad module¶
CONAD: Contrastive Attributed Network Anomaly Detection.
Constructs an anomalous view by injecting synthetic anomalies (attribute swapping + random edge injection), then contrasts with the original view. Dual reconstruction (structure + attributes) from the original view. Nodes with high contrastive distance and high reconstruction error are anomalous.
See [MXHZ+22] for details.
- Reference:
Xu, Z., Huang, X., Zhao, Y., Dong, Y., and Li, J., 2022. Contrastive Attributed Network Anomaly Detection with Data Augmentation. In PAKDD, pp. 444-457.
- class pyod.models.pyg_conad.CONAD(hidden_dim=64, num_layers=2, aug_ratio=0.2, alpha=0.5, dropout=0.3, epochs=100, lr=0.001, contamination=0.1)[source]¶
Bases:
BaseDetectorCONAD: Contrastive + Reconstruction Anomaly Detection.
Constructs an anomalous view via attribute swapping and random edge injection, encodes both views with a shared GCN, and scores nodes by contrastive distance + dual (structure + attribute) reconstruction error.
This detector is transductive.
Parameters¶
- hidden_dimint, default=64
Hidden dimension.
- num_layersint, default=2
Number of GCN layers.
- aug_ratiofloat, default=0.2
Fraction of edges/attributes to drop/mask.
- alphafloat, default=0.5
Weight for reconstruction loss (vs contrastive).
- dropoutfloat, default=0.3
Dropout rate.
- epochsint, default=100
Training epochs.
- lrfloat, default=1e-3
Learning rate.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
X : Data or array-like y : ignored edge_index : array-like or None
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') CONAD¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') CONAD¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') CONAD¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.
pyod.models.pyg_guide module¶
GUIDE: Higher-order Structure Based Anomaly Detection.
Dual GCN autoencoders: one on the original adjacency, one on a motif (triangle) adjacency. Anomaly score = weighted sum of reconstruction errors from both views.
See [MYZY+21] for details.
- Reference:
Yuan, X., Zhou, N., Yu, S., Huang, H., Chen, Z. and Xia, F., 2021. Higher-order Structure Based Anomaly Detection on Attributed Networks. In IEEE BigData, pp. 2691-2700.
- class pyod.models.pyg_guide.GUIDE(hidden_dim=64, num_layers=2, alpha=0.5, dropout=0.3, epochs=100, lr=0.005, contamination=0.1)[source]¶
Bases:
BaseDetectorGUIDE: Higher-order Structure Based Anomaly Detection.
Constructs a motif adjacency from triangle participation (binarized in v1: edges in at least one triangle) and runs two GCN autoencoders in parallel. Score =
alpha * err_orig + (1 - alpha) * err_motif.This detector is transductive.
Parameters¶
- hidden_dimint, default=64
Hidden dimension of GCN layers.
- num_layersint, default=2
Number of GCN encoder layers.
- alphafloat, default=0.5
Weight for original-graph reconstruction error.
- dropoutfloat, default=0.3
Dropout rate.
- epochsint, default=100
Training epochs.
- lrfloat, default=5e-3
Learning rate.
- contaminationfloat, default=0.1
Expected proportion of anomalies.
Attributes¶
decision_scores_ : numpy array of shape (n_nodes,) labels_ : numpy array of shape (n_nodes,) threshold_ : float
- fit(X, y=None, edge_index=None)[source]¶
Fit the detector on graph data.
Parameters¶
X : Data or array-like y : ignored edge_index : array-like or None
Returns¶
self
- predict_proba(X, method='linear', return_confidence=False)[source]¶
Not supported (transductive detector).
- set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') GUIDE¶
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- edge_indexstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
edge_indexparameter infit.
Returns¶
- selfobject
The updated object.
- set_predict_proba_request(*, method: bool | None | str = '$UNCHANGED$', return_confidence: bool | None | str = '$UNCHANGED$') GUIDE¶
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- methodstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
methodparameter inpredict_proba.- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict_proba.
Returns¶
- selfobject
The updated object.
- set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') GUIDE¶
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Parameters¶
- return_confidencestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
return_confidenceparameter inpredict.
Returns¶
- selfobject
The updated object.