Skip to main content

Simple, fast, and framework agnostic uncertainty quantification for knowing when to trust your model's predictions.

Project description

conforma

Know when to trust your model's predictions.

PyPI version Python 3.12+ NumPy only MIT License


Conformal prediction is a technique for getting honest uncertainty estimates from trained ML models. conforma is a lightweight, framework-agnostic Python library that makes it easy to apply. Give it a held-out calibration set and it will wrap your model to produce prediction sets (for classifiers) or prediction intervals (for regressors) with real statistical guarantees. No changes to your model, no retraining, and only numpy as a dependency.

Installation

python -m pip install conforma

Quick start

from conforma import calibrate_classifier, ConformalClassifier

# 1. Get your model's predicted probabilities on a held-out calibration set
cal_probs = model.predict_proba(X_cal)  # shape: (n_examples, n_classes)

# 2. Calibrate
calibration = calibrate_classifier(cal_probs, y_cal)

# 3. Wrap your model
wrapper = ConformalClassifier(
    predict_fn=model.predict_proba,
    calibration=calibration,
)

# 4. Predict with guaranteed coverage
prediction_sets = wrapper.predict(X_test, coverage=0.90)
# >>> array([[ True, False, False],
#            [ True,  True, False]])

Each row is a prediction set where True marks a plausible class. A single True means the model is confident. Multiple True values mean it is uncertain. The guarantee is that at least 90% of prediction sets will contain the correct class.

You can also get the underlying p-values for each class directly.

p_values = wrapper.predict_p_values(X_test)
# >>> array([[0.92, 0.31, 0.04],
#            [0.87, 0.79, 0.11]])

Each value is a conformal p-value measuring how plausible that class is. Higher means the model was less surprised. A class enters the prediction set at coverage level c when its p-value is at least 1 - c. This is useful when you want to rank classes by plausibility, or apply different coverage thresholds after the fact without re-running the model.

FAQ

What is conformal prediction? A way to turn any model's outputs into prediction sets (for classifiers) or prediction intervals (for regressors) that come with a coverage guarantee: the true answer will be included at least X% of the time. It doesn't change the model. It just adds a calibration step after training.

Does this work with my model? If your model can produce per-class scores (probabilities, logits, etc.) for classification or numeric predictions for regression, yes. It doesn't matter what framework you trained with.

How much calibration data do I need? More calibration data means tighter prediction sets. A few hundred examples is a reasonable starting point, but even 50 can be useful. The library will raise an error if your calibration set is too small for your requested coverage level.

What's the catch? The guarantee relies on one assumption: calibration data and test data are drawn from the same distribution. If the world changes after calibration (new user behaviour, different sensor, etc.), you should recalibrate.

Is this really guaranteed? Where's the proof? Yes. The coverage guarantee is proven. The original proof comes from Vovk, Gammerman, and Shafer in Algorithmic Learning in a Random World (2005). If you want something shorter, Shafer and Vovk also wrote "A Tutorial on Conformal Prediction" (2008) in the Journal of Machine Learning Research. Fair warning, neither is light reading. For a more practical introduction, Angelopoulos and Bates wrote "A Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification" (2022), which is an easier read.

Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

conforma-1.0.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

conforma-1.0.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file conforma-1.0.0.tar.gz.

File metadata

  • Download URL: conforma-1.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for conforma-1.0.0.tar.gz
Algorithm Hash digest
SHA256 57ea9e03589635f829187089480a30e60e8f0e69d0bd07ef882285a2ad53924e
MD5 575362650fd1c33b2500548277da07be
BLAKE2b-256 57a8a6f7a3239f7a236f8b28f75d45796cd2b0141038d1bd91c8b2bcbde1549a

See more details on using hashes here.

Provenance

The following attestation bundles were made for conforma-1.0.0.tar.gz:

Publisher: publish.yml on rudikershaw/conforma

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file conforma-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: conforma-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for conforma-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac62c5b510757156589cc78bfb06d2d3e39d26b9deb1749d2af4df38f383b665
MD5 c49553a85f2d739886c51ad2ea00ef42
BLAKE2b-256 0e8317111daafd3805fe7709a6ed092ccd9a9673c4a4b6bd3541074ef7c6d1ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for conforma-1.0.0-py3-none-any.whl:

Publisher: publish.yml on rudikershaw/conforma

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page