scores.PyODScore
Confidence scores based on detectors supplied by PyOD.
Usage
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
pyodlibrary that implements fit anddecision_functionis 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
fit(
X=None,
Y=None,
model=None,
loaders=None,
outdir=None,
prefix=None,
q=False
)This method supports two usage modes:
- Precomputed embeddings: Supply training embeddings via
Xand optional calibration embeddings viaY. - On-the-fly extraction: Supply a
modelwith an.embed()method and a dictionary ofDataLoadersto 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.Tensorwith training sample embeddings. Required when not usingmodelandloaders. Y: torch.Tensor | None = None-
A
torch.Tensorwith calibration sample embeddings. Optional. model: torch.nn.Module | None = None-
A
torch.nn.Modulewith an.embed()method. Required when not usingX. loaders: dict[str, DataLoader[torch.Tensor | dict[str, torch.Tensor]]] | None = None-
A
dictwithDataLoaderobjects. Required keys:["train"]. Optional key:["val"]. Required when usingmodel. outdir: Path | None = None-
A
pathlib.Pathpointing to a directory for saving/loading embeddings. Only used withmodelandloaders. prefix: str | None = None-
A
strused as filename prefix for saved embeddings. Only used withmodelandloaders. q: bool | float = False-
A
floatorboolindicating if outliers from the training distribution should be filtered before fitting. Defaults toFalse.
See Also
- seapig.scores.knn.EuclideanScore: Built-in KNN-based score (no extra dependency).
- seapig.scores.embed.EmbeddingScore: Base class for embedding-based scores.