Prediction Profiles by Feature and Class

This example demonstrates how to visualize prediction set profiles across a numeric feature, split by predicted or true class.

This visualization helps understand how conformal prediction behavior varies across different feature values and between classes.

Setup

First, we create synthetic data and train a conformal classifier.

Code
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

from conformalpy.classifier import ConformalClassifier
from conformalpy.nonconformity.classification import lac_nonconformity
from conformalpy.plots import plot_prediction_profiles_by_feature_class

# Generate synthetic data with a meaningful feature
np.random.seed(42)
X, y = make_classification(
    n_samples=2000,
    n_features=5,
    n_informative=3,
    n_redundant=1,
    n_classes=2,
    weights=[0.6, 0.4],
    random_state=42,
)

# Split data
X_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 model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Create and calibrate conformal classifier
conf_clf = ConformalClassifier(
    model=model,
    alpha=0.1,
    nonconformity_function=lac_nonconformity,
)
conf_clf.calibrate(X_cal, y_cal)

# Get prediction sets
prediction_sets = conf_clf.predict(X_test)

# Use first feature for visualization
feature_values = X_test[:, 0]

print(f"Test samples: {len(y_test)}")
print(f"Coverage: {np.mean([y in ps for y, ps in zip(y_test, prediction_sets)]):.3f}")
Test samples: 400
Coverage: 0.890

Split by True Class

Visualize how prediction set profiles vary across the feature, with separate subplots for each true class (y=0 vs y=1).

Code
plot_prediction_profiles_by_feature_class(
    feature_values,
    prediction_sets,
    y_test,
    bins=10,
    split_by="true",
    title="Prediction Profiles by Feature — Split by True Class",
)
(<Figure size 1344x480 with 4 Axes>,
 array([<Axes: title={'center': 'True Class = 0'}, xlabel='Feature Value (binned)', ylabel='Fraction'>,
        <Axes: title={'center': 'True Class = 1'}, xlabel='Feature Value (binned)', ylabel='Fraction'>],
       dtype=object))

Split by Predicted Class

Same visualization but split by predicted class instead. Two-class prediction sets appear in both subplots.

Code
plot_prediction_profiles_by_feature_class(
    feature_values,
    prediction_sets,
    y_test,
    bins=10,
    split_by="predicted",
    title="Prediction Profiles by Feature — Split by Predicted Class",
)
(<Figure size 1344x480 with 4 Axes>,
 array([<Axes: title={'center': 'Predicted = 0'}, xlabel='Feature Value (binned)', ylabel='Fraction'>,
        <Axes: title={'center': 'Predicted = 1'}, xlabel='Feature Value (binned)', ylabel='Fraction'>],
       dtype=object))

Without Histogram Background

The background histogram can be disabled for a cleaner view.

Code
plot_prediction_profiles_by_feature_class(
    feature_values,
    prediction_sets,
    y_test,
    bins=15,
    split_by="true",
    show_histogram=False,
    title="Prediction Profiles (No Histogram)",
)
(<Figure size 1344x480 with 2 Axes>,
 array([<Axes: title={'center': 'True Class = 0'}, xlabel='Feature Value (binned)', ylabel='Fraction'>,
        <Axes: title={'center': 'True Class = 1'}, xlabel='Feature Value (binned)', ylabel='Fraction'>],
       dtype=object))

Interpretation

The plots show six categories with distinct styles:

  • One Correct (solid green): Singleton sets with correct prediction
  • One Incorrect (solid red): Singleton sets with wrong prediction
  • Two-Class (dashed): Two-class sets (green if aligned with subplot class)
  • Empty (dotted gray): Empty prediction sets

Key insights:

  1. Look for regions where “One Incorrect” (red) is high — the model is confidently wrong in those feature ranges
  2. High “Two-Class” (dashed) indicates uncertainty regions
  3. Compare patterns between classes to identify class-specific behaviors