scores.PyODScore

Confidence scores based on detectors supplied by PyOD.

Usage

Source

scores.PyODScore()

Computes outlier scores using PyOD detectors where low scores indicate samples similar to the training distribution (likely inliers) and high scores indicate samples deviating from the training distribution (likely outliers).

Parameters

detector: pyod.models.base.BaseDetector

A fitted or unfitted PyOD detector instance. Any detector from the pyod library that implements fit and decision_function is supported (e.g., pyod.models.knn.KNN).

pca: TensorPCA or None = None
Optional PCA for dimensionality reduction prior to scoring.

Notes

Requires the optional pyod dependency: pip install pyod.

Attributes

Name Description
cal_required bool(x) -> bool
ident str(object=’’) -> str
train_required bool(x) -> bool

cal_required

bool(x) -> bool

cal_required: bool = True

Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.


ident

str(object=’’) -> str

ident: str = f"{self.ident}-{detector.__class__.__name__}"

str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.


train_required

bool(x) -> bool

train_required: bool = True

Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.

Methods

Name Description
fit() Train a confidence score based on sample embeddings.

fit()

Train a confidence score based on sample embeddings.

Usage

Source

fit(
    X=None,
    Y=None,
    model=None,
    loaders=None,
    outdir=None,
    prefix=None,
    q=False
)

This method supports two usage modes:

  1. Precomputed embeddings: Supply training embeddings via X and optional calibration embeddings via Y.
  2. On-the-fly extraction: Supply a model with an .embed() method and a dictionary of DataLoaders to extract embeddings automatically.

You must use either embeddings (X/Y) OR model+loaders, but not both.

# Mode 1: Precomputed embeddings
from pyod.models.knn import KNN
from seapig.scores.pyod import PyODScore
my_score = PyODScore(detector=KNN(n_neighbors=5))
my_score.fit(X=train_embs, Y=val_embs)

# Mode 2: On-the-fly extraction
my_score = PyODScore(detector=KNN(n_neighbors=5))
my_score.fit(model=model, loaders={"train": train_loader, "val": val_loader})
Parameters
X: torch.Tensor | None = None

A torch.Tensor with training sample embeddings. Required when not using model and loaders.

Y: torch.Tensor | None = None

A torch.Tensor with calibration sample embeddings. Optional.

model: torch.nn.Module | None = None

A torch.nn.Module with an .embed() method. Required when not using X.

loaders: dict[str, DataLoader[torch.Tensor | dict[str, torch.Tensor]]] | None = None

A dict with DataLoader objects. Required keys: ["train"]. Optional key: ["val"]. Required when using model.

outdir: Path | None = None

A pathlib.Path pointing to a directory for saving/loading embeddings. Only used with model and loaders.

prefix: str | None = None

A str used as filename prefix for saved embeddings. Only used with model and loaders.

q: bool | float = False
A float or bool indicating if outliers from the training distribution should be filtered before fitting. Defaults to False.

See Also