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: BaseDetector

SCAN: 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 least mu epsilon-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:
  • epsilon (float, default=0.5) – Structural similarity threshold. An edge (u,v) is an epsilon-edge if sigma(u,v) >= epsilon.

  • mu (int, default=2) – Minimum number of epsilon-neighbors for a node to be a core.

  • contamination (float, default=0.1) – Expected proportion of anomalies in the dataset.

decision_scores_

Anomaly scores. Higher = more anomalous.

Type:

numpy array of shape (n_nodes,)

labels_

Binary labels (0=inlier, 1=outlier).

Type:

numpy array of shape (n_nodes,)

threshold_

Score threshold derived from contamination.

Type:

float

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_
decision_function(X)[source]

Not supported (transductive detector).

fit(X, y=None, edge_index=None)[source]

Fit the detector on graph data.

Parameters:
  • X (Data 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_nodes explicitly.

  • y (ignored)

  • edge_index (array-like or None) – COO edge list. Required when X is numpy.

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') SCAN

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

Radar: 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:
  • alpha (float, default=1.0) – Frobenius norm penalty on W (regularization).

  • gamma (float, 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_iter (int, default=100) – Number of alternating optimization iterations.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

float

decision_function(X)[source]

Not supported (transductive detector).

fit(X, y=None, edge_index=None)[source]

Fit the detector on graph data.

Parameters:
  • X (Data 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.

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') Radar

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

ANOMALOUS: 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:
  • alpha (float, default=1.0) – Frobenius norm penalty on W (regularization).

  • gamma (float, default=1.0) – Graph Laplacian smoothing strength on R.

  • lambda_r (float, default=0.01) – L21-norm penalty on residual R (row-sparsity). Scale-sensitive; typical range 0.001-0.1.

  • max_iter (int, default=100) – Number of alternating optimization iterations.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

float

decision_function(X)[source]

Not supported (transductive detector).

fit(X, y=None, edge_index=None)[source]

Fit the detector on graph data.

Parameters:
  • X (Data or array-like) – PyG Data or node features.

  • y (ignored)

  • edge_index (array-like or None)

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') ANOMALOUS

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

DOMINANT: 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_dim (int, default=64) – Hidden dimension of GCN layers.

  • num_layers (int, default=2) – Number of GCN encoder layers.

  • dropout (float, default=0.3) – Dropout rate during training.

  • alpha (float, default=0.5) – Weight for structure loss (1 - alpha for attribute loss).

  • epochs (int, default=100) – Number of training epochs.

  • lr (float, default=5e-3) – Learning rate.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

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:

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.

Returns:

anomaly_scores – The anomaly score of the input samples.

Return type:

numpy array of shape (n_samples,)

fit(X, y=None, edge_index=None)[source]

Fit the detector on graph data.

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.

predict_confidence(X)[source]

Predict the model’s confidence in making the same prediction under slightly different training sets. See [MPVD20].

Parameters:

X (numpy array of shape (n_samples, n_features)) – The input samples.

Returns:

confidence – 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].

Return type:

numpy array of shape (n_samples,)

predict_proba(X, method='linear', return_confidence=False)[source]

Predict the probability of a sample being outlier. Two approaches are possible:

  1. simply use Min-max conversion to linearly transform the outlier scores into the range of [0,1]. The model must be fitted first.

  2. use unifying scores, see [MKKSZ11].

Parameters:
  • X (numpy array of shape (n_samples, n_features)) – The input samples.

  • method (str, optional (default='linear')) – probability conversion method. It must be one of ‘linear’ or ‘unify’.

  • return_confidence (boolean, optional(default=False)) – If True, also return the confidence of prediction.

Returns:

outlier_probability – 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]).

Return type:

numpy array of shape (n_samples, n_classes)

set_fit_request(*, edge_index: bool | None | str = '$UNCHANGED$') DOMINANT

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') DOMINANT

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

AnomalyDAE: 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_dim (int, default=64) – Embedding dimension.

  • num_heads (int, default=4) – Number of attention heads in GAT.

  • alpha (float, default=0.5) – Weight for structure loss.

  • dropout (float, default=0.3) – Dropout rate.

  • epochs (int, default=100) – Number of training epochs.

  • lr (float, default=5e-3) – Learning rate.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

float

decision_function(X)[source]

Not supported (transductive detector).

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)

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') AnomalyDAE

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

CoLA: 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_dim (int, default=64) – Hidden dimension of GCN.

  • num_layers (int, default=2) – Number of GCN layers.

  • epochs (int, default=100) – Training epochs.

  • lr (float, default=1e-3) – Learning rate.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

float

decision_function(X)[source]

Not supported (transductive detector).

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)

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') CoLA

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

CONAD: 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_dim (int, default=64) – Hidden dimension.

  • num_layers (int, default=2) – Number of GCN layers.

  • aug_ratio (float, default=0.2) – Fraction of edges/attributes to drop/mask.

  • alpha (float, default=0.5) – Weight for reconstruction loss (vs contrastive).

  • dropout (float, default=0.3) – Dropout rate.

  • epochs (int, default=100) – Training epochs.

  • lr (float, default=1e-3) – Learning rate.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

float

decision_function(X)[source]

Not supported (transductive detector).

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)

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') CONAD

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

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: BaseDetector

GUIDE: 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_dim (int, default=64) – Hidden dimension of GCN layers.

  • num_layers (int, default=2) – Number of GCN encoder layers.

  • alpha (float, default=0.5) – Weight for original-graph reconstruction error.

  • dropout (float, default=0.3) – Dropout rate.

  • epochs (int, default=100) – Training epochs.

  • lr (float, default=5e-3) – Learning rate.

  • contamination (float, default=0.1) – Expected proportion of anomalies.

decision_scores_
Type:

numpy array of shape (n_nodes,)

labels_
Type:

numpy array of shape (n_nodes,)

threshold_
Type:

float

decision_function(X)[source]

Not supported (transductive detector).

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)

Return type:

self

predict(X, return_confidence=False)[source]

Not supported (transductive detector).

predict_confidence(X)[source]

Not supported (transductive detector).

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 fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_index (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for edge_index parameter in fit.

Returns:

self – The updated object.

Return type:

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_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_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:
  • method (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for method parameter in predict_proba.

  • return_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict_proba.

Returns:

self – The updated object.

Return type:

object

set_predict_request(*, return_confidence: bool | None | str = '$UNCHANGED$') GUIDE

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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_confidence (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for return_confidence parameter in predict.

Returns:

self – The updated object.

Return type:

object