Skip to main content

Conformal prediction for machine learning classifiers

Project description

Python package Upload Python Package

Conformist

Conformist v1.0.3 is an implementation of conformal prediction, specifically conformal risk control. It was written using Python 3.8.

BaseCoP contains utility functions common to all conformal predictors, such as splitting data into calibration and validation sets, and setting up runs. It is extended by FNRCoP that implements conformal risk control.

The ValidationRun class contains the data from a single run, which entails shuffling the data randomly, splitting it into calibration and validation datasets, calibrating the conformal predictor on the calibration data and creating prediction sets for the validation data.

The ValidationTrial class contains a list of runs and calculates statistics across these runs.

Installation

pip install conformist

Input file format

The input to Conformist is a CSV file with the following columns: id, known_class, predicted_class, [proba_columns]

The proba_columns should contain class-specific probability scores and correspond to the names used in the known_class and predicted_class columns.

Example:

id known_class predicted_class ClassA ClassB ClassC
Sample1 ClassB ClassA 0.70 0.25 0.05
Sample2 ClassA ClassA 0.98 0.02 0.0
Sample3 ClassC ClassC 0.01 0.01 0.98

Example implementation

from conformist import FNRCoP, PredictionDataset, ValidationTrial

CALIB_DATA_CSV = 'path/to/calib.csv'
TEST_DATA_CSV = 'path/to/test.csv'
OUTPUT_DIR = 'path/to/output'
ALPHA = 0.05 # Select a reasonable False Negative Rate

# Read in formatted predictions
calpd = PredictionDataset(predictions_csv=CALIB_DATA_CSV,
                          dataset_name='my_cal_data')
testpd = PredictionDataset(predictions_csv=TEST_DATA_CSV,
                           dataset_name='my_test_data')

# Get class counts and prediction heatmap
calpd.run_reports(OUTPUT_DIR)

# Validation trial and reports
mcp = FNRCoP(calpd, alpha=ALPHA)
trial = mcp.do_validation_trial(n_runs=10000)
trial.run_reports(OUTPUT_DIR)

# Recalibrate conformal predictor
mcp = FNRCoP(calpd, alpha=ALPHA)
mcp.calibrate()

# Predict
formatted_predictions = mcp.predict(testpd,
                                    OUTPUT_DIR)

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

conformist-1.0.3.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

conformist-1.0.3-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file conformist-1.0.3.tar.gz.

File metadata

  • Download URL: conformist-1.0.3.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for conformist-1.0.3.tar.gz
Algorithm Hash digest
SHA256 03dfe6d0fa390342ef677297ba6067713202ef08d647a82ad544beb166af43a2
MD5 c3a6a88bc560881406f481fced386e4d
BLAKE2b-256 130856880b172aaec990ccf3e7c1812dac57b56ee7973e96d9793f844270abe9

See more details on using hashes here.

Provenance

The following attestation bundles were made for conformist-1.0.3.tar.gz:

Publisher: python-publish.yml on Molmed/conformist

Attestations:

File details

Details for the file conformist-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: conformist-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for conformist-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fbb8e789c293f976125aff1842f04e4c4ef5a4b6ce9027eaa9e6e15a6075d2a2
MD5 76c7c5481e21cb29a6337e0a53c35a5f
BLAKE2b-256 fd7ea7c2ccf99edc639853f4e9afffbb8f90c21fc324612168504ca4817ea06b

See more details on using hashes here.

Provenance

The following attestation bundles were made for conformist-1.0.3-py3-none-any.whl:

Publisher: python-publish.yml on Molmed/conformist

Attestations:

Supported by

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