Clustered conformal classification (many classes)¶
Class-cluster-conditional prediction sets when you have many classes and rare labels have too few calibration examples for per-class Mondrian thresholds.
| Use it when | You have dozens or hundreds of classes and SplitConformalClassifier(class_conditional=True) returns trivial all-class sets for rare classes. |
| Guarantee | Exact finite-sample cluster-conditional coverage (and marginal coverage) \(\ge 1 - \alpha\) via disjoint clustering/calibration splits. |
| In the library | ClusteredConformalClassifier |
| Source | Ding, Angelopoulos, Bates, Jordan and Tibshirani (NeurIPS 2023) |
| Example needs | pyconformal |
Finite-sample guarantee
By splitting calibration data into a clustering split and a disjoint calibration split, ClusteredConformalClassifier achieves exact finite-sample cluster-conditional coverage \(\mathbb{P}(Y_{n+1} \in \hat{C}(X_{n+1}) \mid Y_{n+1} \in \mathcal{G}_c) \ge 1 - \alpha\) for every cluster \(\mathcal{G}_c\) while keeping rare-class sets compact.
Minimal example¶
Copy, paste, run.
import numpy as np
from pyconformal import ClusteredConformalClassifier
rng = np.random.default_rng(0)
K = 25
y_cal = np.r_[np.repeat(np.arange(15), 40), np.repeat(np.arange(15, 25), 3)]
y_test = rng.integers(0, K, size=500)
def sim_proba(y):
logits = rng.normal(0.0, 1.0, size=(y.size, K))
logits[np.arange(y.size), y] += np.where(y < 8, 4.0, 2.3)
p = np.exp(logits - logits.max(axis=1, keepdims=True))
return p / p.sum(axis=1, keepdims=True)
clf = ClusteredConformalClassifier(alpha=0.1, score="aps", n_clusters="auto", random_state=0)
clf.calibrate(proba=sim_proba(y_cal), y=y_cal)
sets = clf.predict_set(proba=sim_proba(y_test))
print(f"coverage: {sets[np.arange(500), y_test].mean():.3f} mean set size: {sets.sum(axis=1).mean():.2f}")
More¶
- Full argument list: API reference
- All methods: what's inside