pyAgrum-nightly 2.2.1.9.dev202510271761405498__cp310-abi3-macosx_11_0_arm64.whl → 2.3.0.9.dev202510281761586496__cp310-abi3-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyAgrum-nightly might be problematic. Click here for more details.

Files changed (37) hide show
  1. pyagrum/_pyagrum.so +0 -0
  2. pyagrum/common.py +1 -1
  3. pyagrum/config.py +1 -0
  4. pyagrum/explain/_ComputationCausal.py +75 -0
  5. pyagrum/explain/_ComputationConditional.py +48 -0
  6. pyagrum/explain/_ComputationMarginal.py +48 -0
  7. pyagrum/explain/_CustomShapleyCache.py +110 -0
  8. pyagrum/explain/_Explainer.py +176 -0
  9. pyagrum/explain/_Explanation.py +70 -0
  10. pyagrum/explain/_FIFOCache.py +54 -0
  11. pyagrum/explain/_ShallCausalValues.py +204 -0
  12. pyagrum/explain/_ShallConditionalValues.py +155 -0
  13. pyagrum/explain/_ShallMarginalValues.py +155 -0
  14. pyagrum/explain/_ShallValues.py +296 -0
  15. pyagrum/explain/_ShapCausalValues.py +208 -0
  16. pyagrum/explain/_ShapConditionalValues.py +126 -0
  17. pyagrum/explain/_ShapMarginalValues.py +191 -0
  18. pyagrum/explain/_ShapleyValues.py +298 -0
  19. pyagrum/explain/__init__.py +81 -0
  20. pyagrum/explain/_explGeneralizedMarkovBlanket.py +152 -0
  21. pyagrum/explain/_explIndependenceListForPairs.py +146 -0
  22. pyagrum/explain/_explInformationGraph.py +264 -0
  23. pyagrum/explain/notebook/__init__.py +54 -0
  24. pyagrum/explain/notebook/_bar.py +142 -0
  25. pyagrum/explain/notebook/_beeswarm.py +174 -0
  26. pyagrum/explain/notebook/_showShapValues.py +97 -0
  27. pyagrum/explain/notebook/_waterfall.py +220 -0
  28. pyagrum/explain/shapley.py +225 -0
  29. pyagrum/lib/explain.py +11 -490
  30. pyagrum/pyagrum.py +17 -10
  31. {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/METADATA +1 -1
  32. {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/RECORD +36 -12
  33. pyagrum/lib/shapley.py +0 -661
  34. {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSE.md +0 -0
  35. {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSES/LGPL-3.0-or-later.txt +0 -0
  36. {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSES/MIT.txt +0 -0
  37. {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/WHEEL +0 -0
@@ -0,0 +1,142 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 by #
5
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
6
+ # - Christophe GONZALES(_at_AMU) #
7
+ # #
8
+ # The aGrUM/pyAgrum library is free software; you can redistribute it #
9
+ # and/or modify it under the terms of either : #
10
+ # #
11
+ # - the GNU Lesser General Public License as published by #
12
+ # the Free Software Foundation, either version 3 of the License, #
13
+ # or (at your option) any later version, #
14
+ # - the MIT license (MIT), #
15
+ # - or both in dual license, as here. #
16
+ # #
17
+ # (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
18
+ # #
19
+ # This aGrUM/pyAgrum library is distributed in the hope that it will be #
20
+ # useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
22
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
23
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
24
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
25
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
26
+ # OTHER DEALINGS IN THE SOFTWARE. #
27
+ # #
28
+ # See LICENCES for more details. #
29
+ # #
30
+ # SPDX-FileCopyrightText: Copyright 2005-2025 #
31
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
32
+ # - Christophe GONZALES(_at_AMU) #
33
+ # SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
34
+ # #
35
+ # Contact : info_at_agrum_dot_org #
36
+ # homepage : http://agrum.gitlab.io #
37
+ # gitlab : https://gitlab.com/agrumery/agrum #
38
+ # #
39
+ ############################################################################
40
+
41
+ import pyagrum as gum
42
+ from pyagrum.explain._Explanation import Explanation
43
+
44
+ import numpy as np
45
+
46
+ import matplotlib.pyplot as plt
47
+ from matplotlib.colors import LinearSegmentedColormap, to_rgb
48
+ from matplotlib.patches import Patch
49
+
50
+
51
+ def bar(explanation: Explanation, y: int = None, ax: plt.Axes = None, percentage: bool = False) -> plt.axis:
52
+ """
53
+ Plots a horizontal bar chart of the mean absolute SHAP/SHALL values for each feature in the explanation.
54
+
55
+ Parameters:
56
+ ----------
57
+ explanation : Explanation
58
+ The explanation object containing the SHAP/SHALL values.
59
+ y : int, optional
60
+ If the values type of the explanation is SHALL, then y is ignored.
61
+ Else it is the class for which to plot the SHAP values (default is None, which plots multi-bar for all classes).
62
+ ax : plt.Axes, optional
63
+ The matplotlib Axes object to plot on (default is None, which creates a new figure).
64
+ percentage: bool
65
+ if True, the importance plot is shown in percent.
66
+
67
+ Raises :
68
+ ------
69
+ TypeError : If `explanation` is not an Explanation object or if `y` is not an integer or None.
70
+ IndexError : If `y` is an integer but out of bounds for the explanation keys.
71
+ """
72
+
73
+ if not isinstance(explanation, Explanation):
74
+ raise TypeError(f"`explanation` must be an Explanation object but got {type(explanation)}")
75
+
76
+ # Determine if The explanation object is a SHALL or SHAP explanation
77
+ if explanation.values_type == "SHAP":
78
+ if not isinstance(y, int) and y is not None:
79
+ raise TypeError(f"`y` must be either a positive integer or None, but got {type(y)}")
80
+ if isinstance(y, int) and (y < min(explanation.keys()) or y > max(explanation.keys())):
81
+ raise IndexError(f"Target index y={y} is out of bounds; expected 0 <= y < {max(explanation.keys()) + 1}.")
82
+ elif explanation.values_type == "SHALL":
83
+ # We force y to be an integer, so we can use the same code after for both explanations
84
+ y = 0
85
+ else:
86
+ raise ValueError(f"Wrong values type, expected SHAP/SHALL but got {explanation.values_type}")
87
+
88
+ if ax is None:
89
+ _, ax = plt.subplots(figsize=(6, 4))
90
+
91
+ if y is not None:
92
+ importances = explanation.importances[y] if explanation.values_type == "SHAP" else explanation.importances
93
+ columns = [col for col in sorted(importances.keys(), key=importances.get)]
94
+ values = [importances[feat] for feat in columns]
95
+ if percentage:
96
+ total = sum(values)
97
+ values = [(v / total) * 100 for v in values]
98
+ ax.barh(columns, values, color=gum.config["notebook", "tensor_color_0"], height=0.5, alpha=0.8)
99
+ else:
100
+ classes = sorted(explanation.keys())
101
+ cmap = LinearSegmentedColormap.from_list(
102
+ "class_cmap", [to_rgb(gum.config["notebook", "tensor_color_0"]), to_rgb(gum.config["notebook", "tensor_color_1"])]
103
+ )
104
+ colors = [cmap(i / (len(explanation) - 1)) for i in range(len(explanation))]
105
+
106
+ n_features = len(explanation.feature_names)
107
+ values = np.array([[explanation.importances[z][feat] for feat in explanation.feature_names] for z in classes])
108
+ # Sort bars
109
+ indices = np.argsort(np.sum(values, axis=0))
110
+ values = values[:, indices]
111
+ features = [explanation.feature_names[i] for i in indices]
112
+ bottom = np.zeros(n_features)
113
+
114
+ for i, cls in enumerate(classes):
115
+ contribs = values[i]
116
+ if percentage:
117
+ total = sum(contribs)
118
+ contribs = [(v / total) * 100 for v in contribs]
119
+ ax.barh(
120
+ features, contribs, height=0.5, left=bottom, color=colors[i % len(colors)], label=f"class {cls}", alpha=0.8
121
+ )
122
+
123
+ bottom += contribs
124
+ legend_elements = [Patch(facecolor=colors[i], edgecolor="black", label=f"Class {i}") for i in range(len(colors))]
125
+ ax.legend(loc="lower right", handles=legend_elements, title="Classes")
126
+
127
+ ax.set_title("Feature Importance", fontsize=16)
128
+
129
+ msg = " in %" if percentage else ""
130
+ ax.set_xlabel(f"mean(|{explanation.values_type} value|){msg}", fontsize=12)
131
+ ax.set_ylabel("Features", fontsize=12)
132
+ ax.tick_params(axis="x", labelsize=10)
133
+ ax.tick_params(axis="y", labelsize=10)
134
+
135
+ # Removing spines
136
+ ax.grid(axis="x", linestyle=":", alpha=0.6)
137
+ ax.grid(axis="y", linestyle=":", alpha=0.3)
138
+ ax.spines["top"].set_visible(False)
139
+ ax.spines["bottom"].set_visible(False)
140
+ ax.spines["left"].set_visible(False)
141
+ ax.spines["right"].set_visible(False)
142
+ ax.figure.set_facecolor("white")
@@ -0,0 +1,174 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 by #
5
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
6
+ # - Christophe GONZALES(_at_AMU) #
7
+ # #
8
+ # The aGrUM/pyAgrum library is free software; you can redistribute it #
9
+ # and/or modify it under the terms of either : #
10
+ # #
11
+ # - the GNU Lesser General Public License as published by #
12
+ # the Free Software Foundation, either version 3 of the License, #
13
+ # or (at your option) any later version, #
14
+ # - the MIT license (MIT), #
15
+ # - or both in dual license, as here. #
16
+ # #
17
+ # (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
18
+ # #
19
+ # This aGrUM/pyAgrum library is distributed in the hope that it will be #
20
+ # useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
22
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
23
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
24
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
25
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
26
+ # OTHER DEALINGS IN THE SOFTWARE. #
27
+ # #
28
+ # See LICENCES for more details. #
29
+ # #
30
+ # SPDX-FileCopyrightText: Copyright 2005-2025 #
31
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
32
+ # - Christophe GONZALES(_at_AMU) #
33
+ # SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
34
+ # #
35
+ # Contact : info_at_agrum_dot_org #
36
+ # homepage : http://agrum.gitlab.io #
37
+ # gitlab : https://gitlab.com/agrumery/agrum #
38
+ # #
39
+ ############################################################################
40
+
41
+ import pyagrum as gum
42
+ from pyagrum.explain._Explanation import Explanation
43
+
44
+ import numpy as np
45
+
46
+ import matplotlib.pyplot as plt
47
+ import matplotlib.cm as cm
48
+ from matplotlib import colors
49
+
50
+
51
+ def beeswarm(
52
+ explanation: Explanation, y: int = 1, max_display: int = 20, color_bar: bool = True, ax=None, sort: bool = True
53
+ ):
54
+ """
55
+ Plots a beeswarm plot of the Shapley values for a given target class.
56
+ Parameters:
57
+ ----------
58
+ explanation : Explanation
59
+ The explanation object containing the SHAP/SHALL values.
60
+ y : int
61
+ If the values type of the explanation is SHALL, then y is ignored.
62
+ Else it is the class for which to plot the SHAP values.
63
+ max_display : int, optional
64
+ The maximum number of features to display in the beeswarm plot (default is 20).
65
+ color_bar : bool, optional
66
+ If True, adds a color bar to the plot (default is True).
67
+ ax : plt.Axes, optional
68
+ The matplotlib Axes object to plot on (default is None, which creates a new figure).
69
+ sort : bool, optional
70
+ If True, sorts the features by their importance before plotting (default is True).
71
+
72
+ Raises:
73
+ ------
74
+ TypeError
75
+ If `explanation` is not an Explanation object, if `y` is not an integer or if the explanation is not global (i.e., does not contain lists of contributions for each feature).
76
+ IndexError
77
+ If `y` is out of bounds for the explanation keys.
78
+ """
79
+ # Check parameters
80
+ if not isinstance(explanation, Explanation):
81
+ raise TypeError("`explanation` must be an Explanation object but got {}".format(type(explanation)))
82
+
83
+ # Determine if The explanation object is a SHALL or SHAP explanation
84
+ if explanation.values_type == "SHAP":
85
+ if not isinstance(y, int):
86
+ raise TypeError("`y` must be an integer but got {}".format(type(y)))
87
+ if y < min(explanation.keys()) or y > max(explanation.keys()):
88
+ raise IndexError(f"Target index y={y} is out of bounds; expected 0 <= y < {max(explanation.keys()) + 1}.")
89
+ contributions = explanation[y]
90
+ importances = explanation.importances[y]
91
+ elif explanation.values_type == "SHALL":
92
+ contributions = explanation
93
+ importances = explanation.importances
94
+ else:
95
+ raise ValueError(f"Wrong values type, expected SHAP/SHALL but got {explanation.values_type}")
96
+
97
+ feature_names = explanation.feature_names
98
+ if not isinstance(list(contributions.values())[0], list):
99
+ raise TypeError("For beeswarm plot, explanation must be global.")
100
+ values = np.array([contributions[k] for k in feature_names]).T
101
+ features = explanation.data
102
+
103
+ # Create the figure and axis if not provided
104
+ if ax == None:
105
+ _, ax = plt.subplots()
106
+
107
+ # Prepare the y-axis positions
108
+ y_positions = np.arange(min(max_display, len(feature_names)), 0, -1)
109
+
110
+ # Plot the beeswarm
111
+ ax.plot([0, 0], [y_positions[-1] - 0.25, y_positions[0] + 0.25], linestyle="--", color="gray")
112
+ color1 = gum.config["notebook", "tensor_color_0"]
113
+ color2 = gum.config["notebook", "tensor_color_1"]
114
+ cmap = colors.LinearSegmentedColormap.from_list("custom_red_green", [color1, color2])
115
+
116
+ if sort:
117
+ indices = [feature_names.index(feat) for feat in sorted(importances, key=importances.get, reverse=True)]
118
+ else:
119
+ indices = np.arange(min(max_display, values.shape[1]))
120
+
121
+ for k, j in enumerate(indices):
122
+ base = y_positions[k]
123
+ sequence = np.arange(values.shape[0])
124
+ np.random.shuffle(sequence)
125
+
126
+ shapes = values[sequence, j]
127
+ rounded_x = np.round(shapes, 2)
128
+ (unique, counts) = np.unique(rounded_x, return_counts=True)
129
+ density_map = dict(zip(unique, counts))
130
+ densities = np.array([density_map[val] for val in rounded_x])
131
+
132
+ sigmas = (densities / np.max(densities)) * 0.1
133
+ ords = np.random.normal(loc=base, scale=sigmas)
134
+
135
+ vals = features[sequence, j]
136
+
137
+ minimum = vals.min()
138
+ maximum = vals.max()
139
+ norm = colors.Normalize(vmin=minimum, vmax=maximum)
140
+ sm = cm.ScalarMappable(cmap=cmap, norm=norm)
141
+ sm.set_array([])
142
+
143
+ ax.scatter(
144
+ shapes,
145
+ ords,
146
+ c=vals,
147
+ cmap=cmap,
148
+ s=7,
149
+ )
150
+
151
+ ax.set_yticks(y_positions)
152
+ ax.set_yticklabels([feature_names[i] for i in indices])
153
+ if color_bar:
154
+ norm = colors.Normalize(vmin=0.0, vmax=1.0)
155
+ sm = cm.ScalarMappable(cmap=cmap, norm=norm)
156
+ sm.set_array([])
157
+ cbar = plt.colorbar(sm, ax=ax)
158
+ cbar.set_label("Feature value")
159
+ cbar.set_ticks([0, 1])
160
+ cbar.set_ticklabels(["Low", "High"])
161
+
162
+ ax.set_ylim(y_positions[-1] - 0.5, y_positions[0] + 0.5)
163
+ ax.set_xlabel("Impact on model Output", fontsize=12)
164
+ ax.set_ylabel("Features", fontsize=12)
165
+ ax.set_title(f"{explanation.values_type} value (Impact on model Output)", fontsize=16)
166
+
167
+ # Setting the style
168
+ ax.grid(axis="x", linestyle=":", alpha=0.5)
169
+ ax.grid(axis="y", linestyle=":", alpha=0.5)
170
+ ax.spines["top"].set_visible(False)
171
+ ax.spines["bottom"].set_visible(False)
172
+ ax.spines["left"].set_visible(False)
173
+ ax.spines["right"].set_visible(False)
174
+ ax.figure.set_facecolor("white")
@@ -0,0 +1,97 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 by #
5
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
6
+ # - Christophe GONZALES(_at_AMU) #
7
+ # #
8
+ # The aGrUM/pyAgrum library is free software; you can redistribute it #
9
+ # and/or modify it under the terms of either : #
10
+ # #
11
+ # - the GNU Lesser General Public License as published by #
12
+ # the Free Software Foundation, either version 3 of the License, #
13
+ # or (at your option) any later version, #
14
+ # - the MIT license (MIT), #
15
+ # - or both in dual license, as here. #
16
+ # #
17
+ # (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
18
+ # #
19
+ # This aGrUM/pyAgrum library is distributed in the hope that it will be #
20
+ # useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
22
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
23
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
24
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
25
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
26
+ # OTHER DEALINGS IN THE SOFTWARE. #
27
+ # #
28
+ # See LICENCES for more details. #
29
+ # #
30
+ # SPDX-FileCopyrightText: Copyright 2005-2025 #
31
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
32
+ # - Christophe GONZALES(_at_AMU) #
33
+ # SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
34
+ # #
35
+ # Contact : info_at_agrum_dot_org #
36
+ # homepage : http://agrum.gitlab.io #
37
+ # gitlab : https://gitlab.com/agrumery/agrum #
38
+ # #
39
+ ############################################################################
40
+
41
+ import pyagrum as gum
42
+ import pyagrum.lib.notebook as gnb
43
+ from pyagrum.lib.bn2graph import BN2dot
44
+ from pyagrum.explain import Explanation
45
+
46
+ import matplotlib.pyplot as plt
47
+
48
+
49
+ def showShapValues(bn: gum.BayesNet, expl: Explanation | dict, cmap="plasma", y=1):
50
+ """
51
+ Show the Shap values in the DAG of the BN
52
+ Parameters
53
+ ----------
54
+ bn : pyagrum.BayesNet
55
+ The Bayesian network
56
+ expl: Explanation | dict[str,float]
57
+ The Shap values to each variable
58
+ cmap: str
59
+ Name of the Matplotlib colormap used for coloring the nodes.
60
+ y: int
61
+ The target class for which the Shap values are computed (default is 1).
62
+ y is ignored if `expl` is a dict.
63
+
64
+ Raises
65
+ ------
66
+ TypeError
67
+ If bn is not a gum.BayesNet, if expl is neither an Explanation nor a dict, or if expl is an Explanation and y is not an integer.
68
+ IndexError
69
+ If expl is an Explanation and y is outside the valid class range.
70
+ """
71
+
72
+ if not isinstance(bn, gum.BayesNet):
73
+ raise TypeError(f"The parameter bn must be a gum.BayesNet but got {type(bn)}")
74
+ if isinstance(expl, Explanation):
75
+ if isinstance(y, int):
76
+ if y < min(expl.keys()) or y > max(expl.keys()):
77
+ raise IndexError(
78
+ f"Target index y={y} is out of bounds; expected {min(expl.keys())} <= y < {max(expl.keys()) + 1}."
79
+ )
80
+ else:
81
+ raise TypeError("`y`must be an integer but got {}".format(y))
82
+ importances = expl.importances[y]
83
+
84
+ elif isinstance(expl, dict):
85
+ importances = expl
86
+
87
+ else:
88
+ raise TypeError(f"The parameter expl must be either an Explanation object or a dict but got {type(expl)}")
89
+
90
+ norm_color = {}
91
+ raw = list(importances.values())
92
+ norm = [float(i) / sum(raw) for i in raw]
93
+ for i, feat in enumerate(list(importances.keys())):
94
+ norm_color[feat] = norm[i]
95
+ cm = plt.get_cmap(cmap)
96
+ g = BN2dot(bn, nodeColor=norm_color, cmapNode=cm)
97
+ gnb.showGraph(g)
@@ -0,0 +1,220 @@
1
+ ############################################################################
2
+ # This file is part of the aGrUM/pyAgrum library. #
3
+ # #
4
+ # Copyright (c) 2005-2025 by #
5
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
6
+ # - Christophe GONZALES(_at_AMU) #
7
+ # #
8
+ # The aGrUM/pyAgrum library is free software; you can redistribute it #
9
+ # and/or modify it under the terms of either : #
10
+ # #
11
+ # - the GNU Lesser General Public License as published by #
12
+ # the Free Software Foundation, either version 3 of the License, #
13
+ # or (at your option) any later version, #
14
+ # - the MIT license (MIT), #
15
+ # - or both in dual license, as here. #
16
+ # #
17
+ # (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) #
18
+ # #
19
+ # This aGrUM/pyAgrum library is distributed in the hope that it will be #
20
+ # useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS #
22
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
23
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
24
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, #
25
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
26
+ # OTHER DEALINGS IN THE SOFTWARE. #
27
+ # #
28
+ # See LICENCES for more details. #
29
+ # #
30
+ # SPDX-FileCopyrightText: Copyright 2005-2025 #
31
+ # - Pierre-Henri WUILLEMIN(_at_LIP6) #
32
+ # - Christophe GONZALES(_at_AMU) #
33
+ # SPDX-License-Identifier: LGPL-3.0-or-later OR MIT #
34
+ # #
35
+ # Contact : info_at_agrum_dot_org #
36
+ # homepage : http://agrum.gitlab.io #
37
+ # gitlab : https://gitlab.com/agrumery/agrum #
38
+ # #
39
+ ############################################################################
40
+
41
+ import pyagrum as gum
42
+ from pyagrum.explain._Explanation import Explanation
43
+ from typing import Callable, Dict
44
+ import numpy as np
45
+
46
+ import matplotlib.pyplot as plt
47
+ from matplotlib.patches import Polygon
48
+
49
+
50
+ def _POSTERIOR(y: int, func: str) -> str:
51
+ return f"logit($p(y={y} \\mid x)$)" if func == "_logit" else f"$p(y={y} \\mid x)$"
52
+
53
+
54
+ def _JOIN(func: str) -> str:
55
+ return "log($p(x \\mid \\theta)$)" if func == "_log" else "$p(x \\mid \\theta)$"
56
+
57
+
58
+ def _FMT(func: str) -> str:
59
+ return ".2e" if func == "_identity" else ".2f"
60
+
61
+
62
+ def waterfall(explanation: Explanation, y: int = 1, ax=None, real_values: Dict = None):
63
+ """
64
+ Plots a waterfall chart of the SHAP/SHALL values.
65
+
66
+ Parameters:
67
+ ----------
68
+ explanation : Explanation
69
+ The explanation object containing the SHAP/SHALL values.
70
+ y : int, optional
71
+ If the values type of the explanation is SHALL, then y is ignored.
72
+ Else it is the class for which to plot the SHAP values.
73
+ ax : matplotlib.Axes, optional
74
+ The matplotlib Axes object to plot on (default is None, which creates a new figure).
75
+ real_values : Dict, optional
76
+ Dictionary used to display custum values for each feature.
77
+ For example, useful when continuous values have been discretized but you still want to show the original continuous values from the database.
78
+ The keys of the dictionary must match the keys in the Explanation object, and the values are the values you want to display on the plot.
79
+
80
+ Raises :
81
+ ------
82
+ TypeError : If `explanation` is not an Explanation object or if `y` is not an integer.
83
+ IndexError : If `y` is an integer but out of bounds for the explanation keys.
84
+ """
85
+
86
+ if not isinstance(explanation, Explanation):
87
+ raise TypeError("`explanation` must be an Explanation object but got {}".format(type(explanation)))
88
+ if explanation.values_type == "SHAP":
89
+ if isinstance(y, int):
90
+ if y < min(explanation.keys()) or y > max(explanation.keys()):
91
+ raise IndexError(f"Target index y={y} is out of bounds; expected 0 <= y < {max(explanation.keys()) + 1}.")
92
+ else:
93
+ raise TypeError("`y`must be an integer but got {}".format(y))
94
+ values = explanation[y]
95
+ baseline = explanation.baseline[y]
96
+ elif explanation.values_type == "SHALL":
97
+ values = explanation._values
98
+ baseline = explanation.baseline
99
+ else:
100
+ raise ValueError(f"Wrong values type, expected SHAP/SHALL but got {explanation.values_type}")
101
+
102
+ # Computing arrow width
103
+ arrow_width_base = 0.08 * np.max(np.abs(np.array(list(values.values()))))
104
+
105
+ # Tri des SHAP values par importance décroissante
106
+ features = [feature for feature in sorted(values.keys(), key=lambda x: abs(values.get(x)), reverse=True)]
107
+ y_positions = np.arange(len(values) * 0.25, 0, -0.25)
108
+
109
+ # Create the figure and axis if not provided
110
+ if ax == None:
111
+ _, ax = plt.subplots()
112
+
113
+ # Ligne de base :
114
+ ax.plot([baseline, baseline], [y_positions[-1] - 0.25, y_positions[0] + 0.25], linestyle="--", color="gray")
115
+ if explanation.values_type == "SHAP":
116
+ ax.text(
117
+ baseline,
118
+ y_positions[0] + 0.5,
119
+ f"E({_POSTERIOR(y, explanation.func)}) = {baseline:{_FMT(explanation.func)}}",
120
+ ha="center",
121
+ va="bottom",
122
+ color="gray",
123
+ )
124
+ else:
125
+ ax.text(
126
+ baseline,
127
+ y_positions[0] + 0.5,
128
+ f"E({_JOIN(explanation.func)}) = {baseline:{_FMT(explanation.func)}}",
129
+ ha="center",
130
+ va="bottom",
131
+ color="gray",
132
+ )
133
+
134
+ # Lignes de shapes-values
135
+ current_x = min_x = max_x = baseline
136
+
137
+ for i, feature in enumerate(features):
138
+ delta = values[feature]
139
+ x_start = current_x
140
+ x_end = current_x + delta
141
+ z = y_positions[i]
142
+ height = 0.2
143
+ arrow_width = min(0.4 * abs(delta), arrow_width_base)
144
+ facecolor, edgecolor, alpha = (
145
+ (gum.config["notebook", "tensor_color_0"], "#D98383", 1)
146
+ if values[feature] <= 0
147
+ else (gum.config["notebook", "tensor_color_1"], "#82D882", -1)
148
+ )
149
+
150
+ # Dessin du polygon
151
+ polygon = Polygon(
152
+ [
153
+ (x_end + alpha * arrow_width, z - height / 2),
154
+ (x_start, z - height / 2),
155
+ (x_start, z + height / 2),
156
+ (x_end + alpha * arrow_width, z + height / 2),
157
+ (x_end, z), # pointe
158
+ ],
159
+ closed=True,
160
+ facecolor=facecolor,
161
+ edgecolor=edgecolor,
162
+ alpha=0.8,
163
+ linewidth=2,
164
+ )
165
+
166
+ ax.add_patch(polygon)
167
+
168
+ current_x = x_end
169
+ min_x = min(min_x, current_x)
170
+ max_x = max(max_x, current_x)
171
+
172
+ # Ligne de sortie du modèle
173
+ ax.plot([current_x, current_x], [y_positions[-1] - 0.25, y_positions[0] + 0.25], linestyle="--", color="Black")
174
+ if explanation.func == "_logit":
175
+ ax.text(
176
+ current_x,
177
+ y_positions[-1] - 0.5,
178
+ f"{_POSTERIOR(y, explanation.func)} = {current_x:{_FMT(explanation.func)}}",
179
+ ha="center",
180
+ va="bottom",
181
+ color="Black",
182
+ )
183
+ else:
184
+ ax.text(
185
+ current_x,
186
+ y_positions[-1] - 0.5,
187
+ f"{_JOIN(explanation.func)} = {current_x:{_FMT(explanation.func)}}",
188
+ ha="center",
189
+ va="bottom",
190
+ color="Black",
191
+ )
192
+
193
+ y_tickslabels = []
194
+
195
+ for feature in features:
196
+ feat_shap_value = values[feature]
197
+ if real_values is not None:
198
+ value = real_values[feature]
199
+ if isinstance(real_values[feature], float):
200
+ value = round(value, 2)
201
+ else:
202
+ value = explanation.data[explanation.feature_names.index(feature)]
203
+
204
+ y_tickslabels.append(f"{feature} = {value} [{feat_shap_value:{_FMT(explanation.func)}}]")
205
+
206
+ ax.set_yticks(y_positions)
207
+ ax.set_yticklabels(y_tickslabels)
208
+
209
+ # Setting the style
210
+ ax.grid(axis="x", linestyle=":", alpha=0.5)
211
+ ax.grid(axis="y", alpha=0.5)
212
+ ax.spines["top"].set_visible(False)
213
+ ax.spines["bottom"].set_visible(False)
214
+ ax.spines["left"].set_visible(False)
215
+ ax.spines["right"].set_visible(False)
216
+ ax.figure.set_facecolor("White")
217
+
218
+ plt.ylim(min(y_positions) - 1, max(y_positions) + 1)
219
+ delta = max_x - min_x
220
+ plt.xlim(min_x - 0.05 * delta, max_x + 0.05 * delta)