calibrated_explanations.explanations.FastExplanation¶
- class calibrated_explanations.explanations.FastExplanation(calibrated_explanations, index, x, feature_weights, feature_predict, prediction, y_threshold=None, instance_bin=None, condition_source='prediction')[source]¶
Bases:
CalibratedExplanationClass representing fast explanations.
Represents fast, SHAP-like explanations, enabling efficient interpretation of model behavior for large datasets.
Initialize a FastExplanation instance.
- Parameters:
calibrated_explanations (CalibratedExplanations) – The parent CalibratedExplanations object.
index (int) – The index of the instance being explained.
x (array-like) – The test dataset containing the instances to be explained.
feature_weights (dict) – A mapping of feature weights.
feature_predict (dict) – A mapping of feature predictions.
prediction (dict) – A mapping containing the prediction results.
y_threshold (float or tuple, optional) – The threshold for binary classification or regression explanations.
instance_bin (int, optional) – The bin index of the instance.
condition_source (str, default="prediction") – The source of the conditions for the explanation.
- Attributes:
predictGet the prediction from the prediction dictionary.
prediction_intervalGet the prediction interval from the prediction dictionary.
Methods
add_conjunctions([n_top_features, max_rule_size])Warn that conjunctions are not supported for
FastExplanationand perform no work.add_new_rule_condition(feature, rule_boundary)Create a new rule condition for a numerical feature.
build_condition_payload(feature_index, ...)Expose condition payload building for external use.
build_instance_uncertainty()Expose the instance uncertainty payload builder.
build_interval(low, high)Public helper exposing interval construction.
Reuse the factual payload structure for fast explanations.
build_uncertainty_payload(*, value, low, ...)Create a structured uncertainty payload.
compute_confidence_level(percentiles)Compute confidence level from decimal percentiles.
convert_condition_value(raw_value, fallback)Convert textual condition payloads to structured values.
copy()Create a shallow copy of the explanation object.
Define the rule conditions for the fast explanation.
filter_features(*[, exclude_features, ...])Filter rules by feature inclusion or exclusion.
filter_rule_sizes(*[, rule_sizes, ...])Filter rules by conjunctive rule size.
get_class_labels()Return the class labels.
get_explainer()Return the explainer object.
get_mode()Return the mode of the explanation ('classification' or 'regression').
get_percentiles()Return the decoded percentile configuration.
Create fast explanation rules.
ignored_features_for_instance()Return the set of feature indices ignored for this instance.
is_multiclass()Determine if the explanation is multiclass.
is_one_sided()Test if a regression explanation is one-sided.
is_probabilistic()Check if the explanation is probabilistic.
is_regression()Check if the explanation is for regression.
is_thresholded()Check if the explanation is thresholded.
normalize_percentile_value(value)Normalise percentile inputs to decimal fractions.
normalize_threshold_value()Normalise threshold metadata to telemetry-friendly structure.
parse_condition(feature_name, rule_text)Return the parsed operator/value pair for a rule.
plot([filter_top])Plot the fast explanation.
predict_conjunction_tuple(rule_value_set, ...)Calculate the prediction for a conjunctive rule using batched inference.
predict_conjunctive(rule_value_set, ...[, ...])Public wrapper around the conjunctive prediction helper.
rank_features([feature_weights, width, ...])Rank the features based on their weights.
remove_conjunctions()Remove any conjunctive rules.
reset()Reset the explanation to its original state.
safe_feature_name(feature_index)Return the human-readable name for a feature index.
to_dataframe(*args, **kwargs)Return the narrative output as a pandas DataFrame.
to_narrative([template_path, ...])Generate narrative explanation for this single instance.
to_python_number(value)Convert numpy/scalar values to native Python types suitable for telemetry.
to_telemetry()Return telemetry payload for this explanation instance.
- build_rules_payload() Dict[str, Any][source]¶
Reuse the factual payload structure for fast explanations.
- add_conjunctions(n_top_features=5, max_rule_size=2, **kwargs)[source]¶
Warn that conjunctions are not supported for
FastExplanationand perform no work.- Parameters:
n_top_features (int) – The number of top features to consider for conjunctions. Default is 5.
max_rule_size (int) – The maximum size of the conjunctive rules. Default is 2.
Warning
This method is not supported for
FastExplanationand will not alter the explanation.
- add_new_rule_condition(feature, rule_boundary)[source]¶
Create a new rule condition for a numerical feature.
Warning
This method is not supported for
FastExplanationand will not alter the explanation.
- get_rules()[source]¶
Create fast explanation rules.
- Returns:
A dictionary containing the fast explanation rules.
- Return type:
dict
- define_conditions()[source]¶
Define the rule conditions for the fast explanation.
- Returns:
A list of conditions for each feature.
- Return type:
list[str]
- plot(filter_top=None, **kwargs)[source]¶
Plot the fast explanation.
- Parameters:
filter_top (int, optional) – The number of top features to display.
**kwargs (dict) –
Additional plotting arguments, such as:
- showbool, default=True if filename is empty, False otherwise
A boolean parameter that determines whether the plot should be displayed or not. If set to True, the plot will be displayed. If set to False, the plot will not be displayed.
- filenamestr, default=’’
The filename parameter is a string that represents the full path and filename of the plot image file that will be saved. If this parameter is not provided or is an empty string, the plot will not be saved as an image file.
- uncertaintybool, default=False
The uncertainty parameter is a boolean flag that determines whether to plot the uncertainty intervals for the feature weights. If uncertainty is set to True, the plot will show the envelope of possible boundary shifts based on the lower and upper bounds of the uncertainty intervals. If uncertainty is set to False, the plot will only show the feature weights
- stylestr, default=’regular’
The style parameter is a string that determines the style of the plot. Possible styles are for
FastExplanation:’regular’ - a regular plot with feature weights and uncertainty intervals (if applicable)
- rnk_metricstr, default=’feature_weight’
The metric used to rank the features. Supported metrics are ‘ensured’, ‘feature_weight’, and ‘uncertainty’.
- rnk_weightfloat, default=0.5
The weight of the uncertainty in the ranking. Used with the ‘ensured’ ranking metric.