Alibi Detect

raw JSON →
0.13.0 verified Mon Apr 27 auth: no python

A Python library for outlier detection, adversarial detection, concept drift detection, and explainability. Supports TensorFlow, PyTorch, and scikit-learn backends. Current version 0.13.0, with quarterly releases.

pip install alibi-detect
error ModuleNotFoundError: No module named 'alibi_detect'
cause Library not installed or installed with wrong name.
fix
Run: pip install alibi-detect
error ImportError: cannot import name 'MMDDrift' from 'alibi_detect'
cause Wrong import path - drift detectors are in alibi_detect.cd submodule.
fix
Use: from alibi_detect.cd import MMDDrift
error tensorflow.python.framework.errors_impl.NotFoundError: No such file or directory: '.../model.h5'
cause Saving/loading detector without setting TF_USE_LEGACY_KERAS=1 when using TensorFlow 2.16+.
fix
Set os.environ['TF_USE_LEGACY_KERAS'] = '1' before any tensorflow import.
breaking In v0.13.0, TensorFlow backend requires setting environment variable TF_USE_LEGACY_KERAS=1 to use legacy Keras for saving/loading detectors with legacy=True or loading old pickles.
fix Set os.environ['TF_USE_LEGACY_KERAS'] = '1' before importing tensorflow or alibi_detect.
breaking From v0.12.0, Pydantic v2 is supported via a v1 shim. Code relying on Pydantic v1 imports may break if v2 is installed.
fix Ensure pydantic<2 or use the shim; check if your code uses internal Pydantic API.
gotcha When saving or loading detectors, the environment variable TF_USE_LEGACY_KERAS may need to be set consistently across sessions. Failure causes cryptic tensorflow errors.
fix Always set TF_USE_LEGACY_KERAS=1 when using TensorFlow backend, even if not saving/loading legacy files.
deprecated Python 3.7 support was dropped in v0.11.4. Python 3.11 support added in v0.11.3, Python 3.12 in v0.13.0.
fix Upgrade Python to 3.9+.

Basic drift detection with MMDDrift.

from alibi_detect.cd import MMDDrift
from alibi_detect.utils.data import create_outlier_batch
import numpy as np
# create some reference data
np.random.seed(0)
ref_data = np.random.randn(100, 10)
# initialize detector
cd = MMDDrift(ref_data, p_val=0.05)
# detect drift on new data
x_new = np.random.randn(100, 10)
preds = cd.predict(x_new)
print(preds)