Classification quickstart¶
Run calibrated explanations for binary or multiclass classification.
Prerequisites¶
pip install calibrated-explanations scikit-learn
Classification semantics note¶
Calibration prerequisites: fit on
x_proper, y_properand calibrate on held-outx_cal, y_cal.Mode-specific guarantees: Venn-Abers provides calibrated probability intervals for class predictions.
Assumptions: calibration and deployment data are exchangeable or distribution-matched.
Explicit non-guarantees: no guarantee under drift or regime shift.
Explanation-envelope limits: feature-level intervals are model-behavior summaries, not causal guarantees.
Formal semantics: Calibrated interval semantics.
1. Load data and split sets¶
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
dataset = load_breast_cancer()
x = dataset.data
y = dataset.target
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.2, stratify=y, random_state=0
)
x_proper, x_cal, y_proper, y_cal = train_test_split(
x_train, y_train, test_size=0.25, stratify=y_train, random_state=0
)
2. Fit and calibrate the explainer¶
from sklearn.ensemble import RandomForestClassifier
from calibrated_explanations import WrapCalibratedExplainer
explainer = WrapCalibratedExplainer(RandomForestClassifier(random_state=0))
explainer.fit(x_proper, y_proper)
explainer.calibrate(x_cal, y_cal, feature_names=dataset.feature_names)
3. Generate calibrated factual explanations¶
factual = explainer.explain_factual(x_test[:5])
For multiclass all-class explanations:
multi_factual = explainer.explain_factual(x_test[:5], multi_labels_enabled=True)
4. Explore calibrated alternatives¶
alternatives = explainer.explore_alternatives(x_test[:2])
Next steps:
Entry-point tier: Tier 2.