PyOD is a comprehensive and scalable Python library for outlier detection (anomaly detection), offering over 50 detection models. It provides a unified API, making it easy to use and compare various algorithms. The library is currently at version 2.1.0, with frequent minor releases addressing compatibility and adding new features, including recent advancements in multi-modal anomaly detection using foundation model embeddings.
pip install pyodVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to generate synthetic data and use the k-Nearest Neighbors (KNN) algorithm from PyOD to detect outliers. It shows the basic steps of initialization, fitting the model, and retrieving binary outlier labels and raw anomaly scores.
If you relied on TensorFlow-based models, either pin your PyOD version to <2.0.2 or refactor your code to use PyTorch-based models available in PyOD v2.0.2 and later.
Existing VAE models trained with older PyOD versions may produce different results. Consider re-training models or explicitly setting parameters like `output_activation` to maintain backward compatibility if migrating.
Understand that `contamination` is the proportion of outliers in the data. It's used for thresholding (`predict` and `labels_`). If unknown, careful validation or methods like `predict_proba` might be needed. Setting it too high or too low can lead to misclassifications.
It's generally recommended to keep PyOD updated, or ensure compatibility between your `pyod` and `scikit-learn` versions to avoid unexpected behavior, especially after major `scikit-learn` releases.
Install the necessary optional dependencies using `pip install pyod[text]` or `pip install pyod[image]` or individual packages as required by your chosen embedding model (e.g., `pip install sentence-transformers`).
pip install pyod
from pyod.models import LOF
Reshape your input data to a 2D array, for example, using 'X.reshape(-1, 1)' for a single feature.
Use 'model.decision_scores_' to get raw anomaly scores or 'model.labels_' for binary outlier labels after fitting.
Reshape your 1D input data `X` into a 2D array, for example, `X_train_reshaped = X_train.reshape(-1, 1)` if it's a feature array, or convert a pandas Series using `X_train_reshaped = X_train.values.reshape(-1, 1)`.