CausalShapValues.compute
- CausalShapValues.compute(data, N=100)
Computes the Shapley values for the target node based on the provided data.
- Parameters:
data (
pandas.DataFrame | pandas.Series | dict | tuple | None) – Data to explain. A plain DataFrame/Series/dict is treated as (data, True) (labels assumed). A tuple allows explicit control: (data, with_labels). If None, a random sample of size N is generated.N (
int) – The number of samples to generate if data is None.
- Returns:
An Explanation object containing the Shapley values and importances for the target node.
- Return type:
- Raises:
TypeError – If the first element of data is not a pd.DataFrame, pd.Series or dict, or if N is not an integer when data is None.
ValueError – If N is less than 2 when data is None.
Example
import pyagrum as gum
import pyagrum.explain as expl
gum.initRandom(42)
bn = gum.fastBN("A->B->Y;C->Y")
df, _ = gum.generateSample(bn, 200)
explainer = expl.CausalShapValues(bn, "Y", (df, True)) # df as background data
explanation = explainer.compute(df.head(5)) # explains the first 5 rows
sorted(explanation.keys()) # one explanation per value of Y
# [0, 1]
{v: round(imp, 4) for v, imp in sorted(explanation.importances[1].items())}
# {'A': 0.1681, 'B': 0.557, 'C': 0.1415}
Class