This example demonstrates plot_conformal_by_true_class, which creates a side-by-side comparison of conformal prediction performance for each true class separately.
Key features:
Two panels : One for each true class (0 and 1)
Within-class normalization : Each panel shows proportions relative to that class
Easy comparison : See how the model behaves differently for each class
Sample sizes displayed : Each panel shows the number of samples (n)
This visualization is ideal when you want to:
Compare conformal prediction behavior across classes
Identify class-specific issues (e.g., one class has more uncertainty)
Use custom class names for domain-specific interpretation
Setup and Data Loading
We use the breast cancer dataset for this binary classification example.
Code
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from conformalpy.classifier import ConformalClassifier
from conformalpy.nonconformity.classification import lac_nonconformity
from conformalpy.plots.classification import plot_conformal_by_true_class
# Load data
X, y = load_breast_cancer(return_X_y= True )
target_names = load_breast_cancer().target_names # ['malignant', 'benign']
print (f"Classes: { target_names} " )
print (f"Class distribution: { dict (zip (target_names, np.bincount(y)))} " )
Classes: ['malignant' 'benign']
Class distribution: {np.str_('malignant'): np.int64(212), np.str_('benign'): np.int64(357)}
Train-Calibration-Test Split
Code
X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size= 0.4 , random_state= 42 , stratify= y)
X_calib, X_test, y_calib, y_test = train_test_split(X_temp, y_temp, test_size= 0.5 , random_state= 42 , stratify= y_temp)
print (f"Train: { len (y_train)} , Calibration: { len (y_calib)} , Test: { len (y_test)} " )
Train: 341, Calibration: 114, Test: 114
Basic Usage: Side-by-Side Class Comparison
The default view shows generic class names (Class 0, Class 1).
Code
plot_conformal_by_true_class(
calibration_scores= calibration_scores,
probs_test= probs_test,
y_test= y_test,
alphas= np.arange(0.01 , 1.0 , 0.01 ),
cutoff_confidence= 0.9 ,
)
(<Figure size 1152x480 with 2 Axes>,
array([<Axes: title={'center': 'Class 0 (n=43)'}, xlabel='Confidence (1 - α)', ylabel='Proportion within class'>,
<Axes: title={'center': 'Class 1 (n=71)'}, xlabel='Confidence (1 - α)'>],
dtype=object))
Custom Class Names: Domain-Specific Labels
For better interpretability, use meaningful class names.
Code
plot_conformal_by_true_class(
calibration_scores= calibration_scores,
probs_test= probs_test,
y_test= y_test,
alphas= np.arange(0.01 , 1.0 , 0.01 ),
cutoff_confidence= 0.9 ,
class_names= ("Malignant" , "Benign" ),
)
(<Figure size 1152x480 with 2 Axes>,
array([<Axes: title={'center': 'Malignant (n=43)'}, xlabel='Confidence (1 - α)', ylabel='Proportion within class'>,
<Axes: title={'center': 'Benign (n=71)'}, xlabel='Confidence (1 - α)'>],
dtype=object))
Interpretation
Each panel shows:
Green (One_Correct) : Confident and correct single-class predictions
Red (One_Incorrect) : Confident but wrong single-class predictions
Blue (Two_Set) : Uncertain predictions (both classes included)
Gray (No_Set) : Empty prediction sets
Black line : Within-class coverage
Red dashed : Ideal coverage line
Comparing the two panels reveals:
Which class is easier for the model to predict confidently
Whether coverage guarantees hold equally for both classes
At what confidence level each class transitions from uncertain to confident
Comparison with Other Plots
plot_conformal_coverage_by_class
Overall view, all classes in one plot
plot_balanced_conformal_coverage
Imbalanced data, fair class comparison
plot_conformal_by_true_class
Detailed per-class analysis, side-by-side
Use plot_conformal_by_true_class when you need to:
Present results to stakeholders who think in terms of specific classes
Analyze why one class performs differently than another
Use domain-specific terminology (e.g., “Malignant” vs “Benign”)