liveBook · Manning

PyOD

As explained in Outlier Detection in Python

PyOD is an open-source Python library dedicated to detecting anomalous or outlier observations with a collection of detection algorithms.

PyOD (Python Outlier Detection) is a Python library for finding outliers in numeric tabular data. It brings traditional machine-learning and deep-learning detectors under a consistent API, making it easier to try different approaches rather than rely on one detector. section 6.0

A typical detection workflow is:

  • Create a detector and call fit() on training data. For training rows, read numeric outlier scores from decision_scores_. section 6.1
  • For separate test rows, use decision_function() for scores or predict() for binary labels. predict_proba() scales scores to a 0–1 range by default; its outputs are not true probabilities. section 6.1
  • Check detector-specific hyperparameters even when the defaults run successfully: a setting may be unsuitable for the data or detection goal. section 6.1
Listing 6.1 Basic example using PyOD with the KNN detector
from pyod.models.knn import KNN
 
clf = KNN()
clf.fit(data)
scores = clf.decision_scores_

The KNN listing shows the minimal pattern: instantiate a detector, fit it, and retrieve training scores. section 6.1

Scores need interpretation. In a synthetic KNN example, points well outside the main cluster score higher than points near its edge; the edge points are less clearly separated from inliers. Comparing scores with known labels and their distribution is more informative than treating every predicted label as an equally clear case. section 6.1

PyOD also supplies generate_data() for initial experiments, with labeled outliers and separate training and test rows. Its simple generated data is not meant to resemble an arbitrary real dataset; realistic synthetic data is preferable for tuning and testing a detection system. Deep-learning detectors use the same API, but require the relevant TensorFlow/Keras or PyTorch installation. section 11.2.1 section 16.2

How Outlier Detection in Python covers it

How deep it goes
Demonstrates fitting, scoring, and predicting through the common API while leaving many detector-specific hyperparameters and algorithms unexplored.
Examples
Uses KNN and KDE listings, synthetic clustered points with known labels, and a labeled train/test data generator.
What it assumes
Assumes Python data and detector concepts; introduces the shared API through basic examples rather than requiring familiarity with every detector.
What you can do after
Apply it via PyOD to fit detectors, compare training or test scores, and obtain binary outlier predictions.
How it connects
Connects outlier scores and binary predictions to KNN, kernel density estimation, autoencoders, and generative adversarial networks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.