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.

Version 0.1.0 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

At this time, this library is not published to pypi. I will update the README when it is published and available.

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 a 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-0.1.0a1.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

Details for the file conforma-0.1.0a1.tar.gz.

File metadata

  • Download URL: conforma-0.1.0a1.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for conforma-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 f909269170673ae660f9878418a9aada14e00ac795473544b660e236dde679f1
MD5 fd9593124cf1301564b9b3cc68cff016
BLAKE2b-256 be1484a75b161c54ac41af3e76573e9fe3e7650cc7e5a502b5ebef00626c35fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for conforma-0.1.0a1.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-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: conforma-0.1.0a1-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.12

File hashes

Hashes for conforma-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 1493780ad74148a07040dbd2e77dd1a5402b29a6eda34d3f5e2f9ef81708c547
MD5 36323fc92d2329fd970b8fdcdf5049a5
BLAKE2b-256 32d0c937908b9fa332a0f818a99ad5d65643a0121b747792dbb77280b6e70a32

See more details on using hashes here.

Provenance

The following attestation bundles were made for conforma-0.1.0a1-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