This example demonstrates how to use the accuracy-coverage curve to analyze the trade-off between automation rate and prediction accuracy in conformal prediction-based selective prediction systems.
Selective Prediction refers to systems where predictions are only made when the model is sufficiently confident. For conformal prediction, this means only acting on singleton prediction sets (predictions containing exactly one class).
The key trade-off is:
Coverage (automation rate): Proportion of samples receiving a singleton
Accuracy: Correctness of predictions among the singletons only
Setup
Create synthetic data and train a conformal classifier.
Code
import matplotlib.pyplot as pltimport numpy as npfrom sklearn.datasets import make_classificationfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import train_test_splitfrom conformalpy.classifier import ConformalClassifierfrom conformalpy.evaluation import ( compute_accuracy_coverage_curve, compute_accuracy_coverage_from_sets, plot_accuracy_coverage_curve,)from conformalpy.nonconformity.classification import lac_nonconformity# Generate synthetic data with some class imbalancenp.random.seed(42)X, y = make_classification( n_samples=2000, n_features=10, n_informative=5, n_classes=2, weights=[0.65, 0.35], random_state=42,)# Split: train / calibration / testX_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4, random_state=42)X_cal, X_test, y_cal, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)# Train base modelmodel = LogisticRegression(random_state=42, max_iter=1000)model.fit(X_train, y_train)# Create conformal classifierconf_clf = ConformalClassifier( model=model, alpha=0.10, # 90% confidence level nonconformity_function=lac_nonconformity,)# Get baseline accuracyy_pred_baseline = model.predict(X_test)baseline_acc = np.mean(y_pred_baseline == y_test)print(f"Test samples: {len(y_test)}")print(f"Baseline model accuracy (full automation): {baseline_acc:.1%}")
Test samples: 400
Baseline model accuracy (full automation): 74.5%
Compute Accuracy-Coverage Curve
The curve shows how accuracy varies with the automation rate (coverage) as we change the confidence level (alpha).
Baseline point (red star): Full automation using point predictions
Curve points: Performance at different confidence levels
Lower coverage (left): More selective, typically higher accuracy
Higher coverage (right): Less selective, approaches baseline
A good conformal predictor should show accuracy above the baseline at all coverage levels.
Code
# Show specific operating pointsprint("\nOperating Points:")print("-"*60)print(f"{'Alpha':>8}{'Coverage':>12}{'Accuracy':>12}{'n_singletons':>12}")print("-"*60)for i in [0, 9, 19, 29]: # Sample a few pointsif i <len(curve["alpha"]): alpha = curve["alpha"][i] cov = curve["coverage"][i] acc = curve["accuracy"][i] n_sing = curve["n_singletons"][i]print(f"{alpha:8.2f}{cov:12.1%}{acc:12.1%}{n_sing:12d}")print("-"*60)print(f"{'Baseline':>8}{1.0:12.1%}{baseline_acc:12.1%}{len(y_test):12d}")