Narrative Template Customization¶
This guide explains how to customize the narrative templates used by the to_narrative() method in Calibrated Explanations.
Overview¶
Narrative templates define how explanations are rendered into human-readable text. They use a YAML or JSON format with placeholders that get filled with actual values.
Template Structure¶
Templates are organized hierarchically:
narrative_templates:
<problem_type>:
<explanation_type>:
<expertise_level>: |
Template text with {placeholders}...
Problem Types¶
binary_classification- Two-class classificationmulticlass_classification- Multi-class classificationregression- Standard regression with intervalsprobabilistic_regression- Regression with probability thresholds
Explanation Types¶
factual- Explains why the current prediction was madealternative- Shows how to change the prediction
Expertise Levels¶
beginner- Simple language, minimal detailsintermediate- Includes weights and prediction intervalsadvanced- Full weight intervals and explanation envelopes
Available Placeholders¶
Global Context¶
Placeholder |
Description |
|---|---|
|
Predicted class label |
|
Positive class label (binary classification) |
|
Calibrated prediction value |
|
Lower bound of prediction interval |
|
Upper bound of prediction interval |
|
Threshold value (scalar) |
|
Full threshold condition (e.g., “target <= 150” or “100 < target <= 200”) |
|
Lower threshold (for tuple thresholds) |
|
Upper threshold (for tuple thresholds) |
|
Second-most likely class (multiclass) |
|
Margin to runner-up class |
|
Width of prediction interval |
Per-Feature Placeholders¶
Placeholder |
Description |
|---|---|
|
Name of the feature |
|
Actual value of the feature |
|
Feature condition (e.g., “> 5.0”) |
|
Feature’s contribution weight |
|
Lower bound of weight interval |
|
Upper bound of weight interval |
|
Predicted value for this rule |
|
Lower bound of prediction |
|
Upper bound of prediction |
Conjunctive Rules¶
When add_conjunctions() is called on explanations, multiple feature conditions are combined. In narratives, these appear with a configurable separator:
# Default: " AND "
explanations.to_narrative(conjunction_separator=" AND ")
# Result: "(Glucose > 120 AND BMI > 28)"
# Custom separator
explanations.to_narrative(conjunction_separator=" & ")
# Result: "(Glucose > 120 & BMI > 28)"
Conjunctive rules are automatically wrapped in parentheses for clarity.
Weight Alignment¶
By default, narratives vertically align the weight columns for better readability:
# With alignment (default)
explanations.to_narrative(align_weights=True)
# Result:
# * Glucose (145.0) > 120 — weight ≈ 0.150
# * BMI (28.5) > 25 — weight ≈ 0.082
# * Age (55) > 40 — weight ≈ 0.045
# Without alignment
explanations.to_narrative(align_weights=False)
# Result:
# * Glucose (145.0) > 120 — weight ≈ 0.150
# * BMI (28.5) > 25 — weight ≈ 0.082
# * Age (55) > 40 — weight ≈ 0.045
Interval Probabilistic Regression¶
For probabilistic regression with interval thresholds, use tuple thresholds:
# Scalar threshold: P(y <= 150)
explanations = explainer.explain_factual(x_test, threshold=150)
# Interval threshold: P(100 < y <= 200)
explanations = explainer.explain_factual(x_test, threshold=(100, 200))
The {threshold_condition} placeholder automatically adapts:
Scalar: “target <= 150.000”
Interval: “100.000 < target <= 200.000”
Output Formats¶
The to_narrative() method supports multiple output formats:
Format |
Description |
|---|---|
|
pandas DataFrame (default) |
|
Plain text with headers |
|
HTML table |
|
List of dictionaries |
|
Markdown with code blocks |
Creating Custom Templates¶
Copy the default template from
src/calibrated_explanations/templates/explain_template.jsonModify the templates as needed
Pass your template path to
to_narrative():
narratives = explanations.to_narrative(
template_path="my_template.yaml",
expertise_level="advanced",
output_format="text"
)
Example Custom Template¶
narrative_templates:
binary_classification:
factual:
beginner: |
The model predicts: {label}
Confidence: {calibrated_pred}
Main reasons:
* {feature_name} = {feature_actual_value}
Best Practices¶
Use appropriate expertise levels: Beginners need simple explanations; experts want uncertainty details.
Test with real data: Verify placeholders are filled correctly for your use case.
Keep templates readable: Use line breaks and clear structure.
Handle edge cases: Consider what happens when there are few features or missing values.