Overview
Concrete tool for generating anchor-based explanations for tabular/structured data models using the Alibi library provided by the KServe sample code.
Description
AnchorTabular extends ExplainerWrapper and wraps the Alibi AnchorTabular explainer. During construction, it receives a prediction function and a pre-built Alibi AnchorTabular explainer instance. The explain() method converts input data to a NumPy array, checks whether the predictor returns class labels or probabilities (applying ArgmaxTransformer if probabilities are returned), updates both the explainer's main predictor and its internal sampler predictor, and then calls the Alibi anchor tabular explanation algorithm on the first instance in the batch. The result produces human-readable rules (e.g., "age > 25 AND income > 50K") that explain individual predictions.
Usage
Use this class as part of the Alibi explainer server pipeline when you need to generate rule-based explanations for structured/tabular data predictions such as credit scoring or classification tasks.
Code Reference
Source Location
Signature
class AnchorTabular(ExplainerWrapper):
def __init__(
self,
predict_fn: Callable,
explainer=Optional[alibi.explainers.AnchorTabular],
**kwargs,
):
...
def explain(self, inputs: List, headers: Dict[str, str] = None) -> Explanation:
...
Import
from alibiexplainer.anchor_tabular import AnchorTabular
I/O Contract
Inputs
Constructor
| Name |
Type |
Required |
Description
|
| predict_fn |
Callable |
Yes |
Prediction function that accepts input arrays and returns predictions
|
| explainer |
Optional[alibi.explainers.AnchorTabular] |
Yes |
A pre-built Alibi AnchorTabular explainer instance (must not be None)
|
| **kwargs |
dict |
No |
Additional keyword arguments passed to the Alibi explain call
|
explain()
| Name |
Type |
Required |
Description
|
| inputs |
List |
Yes |
List of tabular data rows to explain (first row is used)
|
| headers |
Dict[str, str] |
No |
Optional HTTP headers
|
Outputs
explain()
| Name |
Type |
Description
|
| anchor_exp |
Explanation |
Alibi Explanation object containing anchor rules and metadata
|
Usage Examples
Basic Usage
from alibiexplainer.anchor_tabular import AnchorTabular
import alibi
import numpy as np
# Assume a pre-built AnchorTabular explainer and a predict function
def my_predict_fn(data):
return np.array([0, 1])
# Build the explainer with training data
anchor_tab_explainer = alibi.explainers.AnchorTabular(
predictor=my_predict_fn,
feature_names=["age", "income", "score"]
)
anchor_tab_explainer.fit(training_data)
wrapper = AnchorTabular(predict_fn=my_predict_fn, explainer=anchor_tab_explainer)
# Generate an explanation for a tabular instance
sample_row = [[25, 50000, 0.8]]
explanation = wrapper.explain(sample_row)
print(explanation.anchor) # e.g., ["age > 20", "income > 40000"]
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.