pyAgrum-nightly 2.2.1.9.dev202510261761405498__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.dev202510261761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/METADATA +1 -1
  32. {pyagrum_nightly-2.2.1.9.dev202510261761405498.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.dev202510261761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSE.md +0 -0
  35. {pyagrum_nightly-2.2.1.9.dev202510261761405498.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.dev202510261761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSES/MIT.txt +0 -0
  37. {pyagrum_nightly-2.2.1.9.dev202510261761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/WHEEL +0 -0
pyagrum/_pyagrum.so CHANGED
Binary file
pyagrum/common.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = '2.2.1.9'
1
+ __version__ = '2.3.0.9'
2
2
  __license__ = __doc__
3
3
  __project_url__ = 'https://agrum.org'
4
4
  __project_name__ = 'pyAgrum'
pyagrum/config.py CHANGED
@@ -63,6 +63,7 @@ class PyAgrumConfiguration(metaclass=GumSingleton):
63
63
 
64
64
  Examples
65
65
  --------
66
+ >>> import pyagrum as gum
66
67
  >>> gum.config["dynamicBN", "default_graph_size"] = 10
67
68
  >>> gum.config["dynamicBN", "default_graph_size"]
68
69
  "10"
@@ -0,0 +1,75 @@
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
+
43
+ # Calculations
44
+ import numpy as np
45
+
46
+
47
+ class CausalComputation:
48
+ @staticmethod
49
+ def _outOfCoalition(tau: list[int], inter: list[int]) -> list[int]:
50
+ # Returns the intersection of the parameter inter and the complement of the coalition tau.
51
+ return [i for i in inter if i not in tau]
52
+
53
+ @staticmethod
54
+ def _doCalculus(bn: gum.BayesNet, tau: list[int]) -> gum.BayesNet:
55
+ # Creates a new Bayesian Network by removing incoming arcs to the nodes in tau.
56
+ doNetTemplate = gum.BayesNet(bn)
57
+ for i in tau:
58
+ parents = doNetTemplate.parents(i)
59
+ for j in parents:
60
+ doNetTemplate.eraseArc(j, i)
61
+ return doNetTemplate
62
+
63
+ @staticmethod
64
+ def _chgCpt(doNetTemplate: gum.BayesNet, tau: list[int], alpha: list[int]) -> None:
65
+ # Changes the conditional probability tables (CPTs) of the nodes in tau to reflect the values in alpha.
66
+ for i, j in zip(tau, alpha):
67
+ doNetTemplate.cpt(i).fillWith(0.0)
68
+ doNetTemplate.cpt(i)[int(j)] = 1.0
69
+
70
+ @staticmethod
71
+ def _weight(evidces: dict[int, int], count: int, doLazy: gum.LazyPropagation) -> np.ndarray:
72
+ # Returns the evidces probability.
73
+ # The signature must be : Dict[int, int], int, **kwargs
74
+ doLazy.updateEvidence(evidces)
75
+ return doLazy.evidenceProbability() * count
@@ -0,0 +1,48 @@
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 numpy as np
42
+
43
+
44
+ class ConditionalComputation:
45
+ @staticmethod
46
+ def _weight(evidces: dict[int, int], count: int) -> np.ndarray:
47
+ # The signature must be : Dict[int, int], int, **kwargs
48
+ return count
@@ -0,0 +1,48 @@
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 numpy as np
42
+
43
+
44
+ class MarginalComputation:
45
+ @staticmethod
46
+ def _weight(evidces: dict[int, int], count: int) -> np.ndarray:
47
+ # The signature must be : Dict[int, int], int, **kwargs
48
+ return count
@@ -0,0 +1,110 @@
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
+
42
+ class CustomShapleyCache:
43
+ def __init__(self, max_capacity: int = 1000):
44
+ if max_capacity <= 0:
45
+ raise ValueError("max_capacity must be a positive integer.")
46
+ self._cache = {} # Structure: {longueur_str: { (entier, str): valeur }}
47
+ self._current_k_index = -1 # Pour suivre l'indice k actuel
48
+ self._max_capacity = max_capacity # Capacité maximale du cache en nombre d'éléments
49
+ self._current_size = 0 # Taille actuelle du cache
50
+
51
+ @staticmethod
52
+ def generate_keys(bn, target, feat, nodes):
53
+ key1 = tuple(nodes) # Key1 represents the coalition.
54
+ key2 = tuple(
55
+ n for n in nodes if n != feat
56
+ ) # Key2 represents the coalition without the feature which is being evaluated.
57
+ if target is None: # For SHALL Values
58
+ return key1, key2, None
59
+ # Key3 is the coalition without ONE ! node which is not in the minimal conditional set.
60
+ minimal = bn.minimalCondSet(
61
+ target, nodes
62
+ ) # Calculates the minimal conditional set for the target node given the nodes.
63
+ diff = next((n for n in nodes if n not in minimal), None)
64
+ if diff is not None:
65
+ key3 = tuple(n for n in nodes if n != diff)
66
+ else:
67
+ key3 = key1
68
+ return key1, key2, key3
69
+
70
+ def get(self, int_key: int, tuple_key: tuple):
71
+ tuple_len = len(tuple_key)
72
+ return self._cache[tuple_len].get((int_key, tuple_key), None)
73
+
74
+ def set(self, int_key: int, tuple_key: tuple, value):
75
+ tuple_len = len(tuple_key)
76
+ if tuple_len not in self._cache:
77
+ self._cache[tuple_len] = {}
78
+ self._current_k_index = tuple_len
79
+
80
+ # If it is the first time we add an entry of this length, we initialize the cache for this length.
81
+ if (int_key, tuple_key) not in self._cache[tuple_len]:
82
+ self._current_size += 1
83
+ self._cache[tuple_len][(int_key, tuple_key)] = value
84
+
85
+ # Check if we need to purge the cache
86
+ if self._current_size > self._max_capacity:
87
+ self._perform_purge()
88
+
89
+ def _perform_purge(self):
90
+ if self._current_k_index < 2:
91
+ return
92
+
93
+ str_len_to_purge = self._current_k_index - 2
94
+
95
+ # Purge all entries with length <= str_len_to_purge
96
+ for length in list(self._cache.keys()):
97
+ if length <= str_len_to_purge:
98
+ self._current_size -= len(self._cache[length])
99
+ del self._cache[length]
100
+
101
+ def __len__(self):
102
+ return self._current_size
103
+
104
+ def __str__(self):
105
+ # Affiche le cache par longueur de clé pour une meilleure visibilité
106
+ items_by_length = {k: len(v) for k, v in self._cache.items()}
107
+ return (
108
+ f"Cache (k={self._current_k_index}, size={self._current_size}/{self._max_capacity}): "
109
+ f"Contents by length: {items_by_length}"
110
+ )
@@ -0,0 +1,176 @@
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
+ from abc import ABC, abstractmethod
42
+ import pyagrum as gum
43
+ from pyagrum.explain._FIFOCache import FIFOCache
44
+
45
+ # Calculations
46
+ import numpy as np
47
+ import math
48
+ from itertools import combinations
49
+ from typing import Callable
50
+
51
+
52
+ class Explainer(ABC):
53
+ """
54
+ ___Documentation___
55
+ """
56
+
57
+ def __init__(self, bn: gum.BayesNet):
58
+ """
59
+ Parameters:
60
+ ------
61
+ bn : pyagrum.BayesNet
62
+ The Bayesian Network.
63
+
64
+ Raises:
65
+ ------
66
+ TypeError : If bn is not a gum.BayesNet.
67
+ """
68
+ super().__init__()
69
+ if not isinstance(bn, gum.BayesNet):
70
+ raise TypeError("bn must be a gum.BayesNet instance, but got {}".format(type(bn)))
71
+ self.bn = bn
72
+ self.M = len(bn.nodes()) # Total number of nodes in the Bayesian Network.
73
+ self.feat_names = np.empty(self.M, dtype=object) # Array to store feature names by their node ID.
74
+ for name in self.bn.names():
75
+ self.feat_names[self.bn.idFromName(name)] = name
76
+
77
+ @staticmethod
78
+ def _log(p: float):
79
+ # Applies the log function to the probability.
80
+ return np.log(p) if p > 0 else -np.inf
81
+
82
+ @staticmethod
83
+ def _logit(p: any):
84
+ # Applies the logit transformation to the probabilities.
85
+ p = np.asarray(p) # Guarantee p is a numpy array.
86
+ with np.errstate(divide="ignore", invalid="ignore"):
87
+ result = np.log(p / (1 - p))
88
+ result = np.where(p == 0, 0.0, result)
89
+ result = np.where(p == 1, 0.0, result)
90
+ return result
91
+
92
+ @staticmethod
93
+ def _identity(x: any):
94
+ # Returns the input as is (identity function).
95
+ return x
96
+
97
+ def _labelToPos_row(self, x: np.ndarray, elements: list[int]) -> np.ndarray:
98
+ # Converts labels to positions for a single instance.
99
+ y = np.empty(shape=x.shape, dtype=int)
100
+ for i in elements:
101
+ try:
102
+ val = self.bn.variable(i).posLabel(x[i])
103
+ except:
104
+ val = int(x[i])
105
+ y[i] = val
106
+ return y
107
+
108
+ def _labelToPos_df(self, x: np.ndarray, elements: list[int]) -> np.ndarray:
109
+ # Converts labels to positions for multiple instances.
110
+ y = np.empty(shape=x.shape, dtype=int) # Initialisation
111
+ posLabelVect = np.vectorize(lambda i, j: self.bn.variable(int(j)).posLabel(i))
112
+ for j in elements:
113
+ try:
114
+ self.bn.variable(j).posLabel(x[0, j])
115
+ y[:, j] = posLabelVect(x[:, j], j)
116
+ except NotImplementedError:
117
+ y[:, j] = x[:, j].astype(int)
118
+ return y
119
+
120
+ @staticmethod
121
+ def _extract(data: np.ndarray, tau: list[int], alpha: np.ndarray) -> np.ndarray:
122
+ # Extracts the data given the values in alpha for the nodes in tau.
123
+ mask = np.all(data[:, tau] == alpha, axis=1)
124
+ idx = np.nonzero(mask)[0]
125
+ return idx
126
+
127
+ @staticmethod
128
+ def _coalitions(elements_for_coalitions):
129
+ # Generates all possible coalitions from the given elements.
130
+ all_coalitions = []
131
+ for r in range(1, len(elements_for_coalitions) + 1):
132
+ for combo in combinations(elements_for_coalitions, r):
133
+ all_coalitions.append(list(combo))
134
+ return all_coalitions
135
+
136
+ @staticmethod
137
+ def _invcoeff_shap(m, s):
138
+ # Computes the inverse coefficient for the Shapley value formula.
139
+ return (m - s) * math.comb(m, s)
140
+
141
+ @staticmethod
142
+ def _shap_term(prob_with, prob_without, m, s):
143
+ return (prob_with - prob_without) / Explainer._invcoeff_shap(m, s)
144
+
145
+ def _value(
146
+ self, data: np.ndarray, counts: list[int], elements: list[int], sigma: list[int], cache: FIFOCache, **kwargs
147
+ ) -> float | np.ndarray:
148
+ # -- #
149
+ length: int = len(data)
150
+ func1: Callable = kwargs["func1"]
151
+ func2: Callable = kwargs["func2"]
152
+ params1: dict[str, any] = kwargs["params1"]
153
+ params2: dict[str, any] = kwargs["params2"]
154
+ # -- #
155
+
156
+ val = 0.0
157
+ norm = 0.0
158
+ for i in range(length):
159
+ evidces1 = {self.feat_names[key]: int(data[i, key]) for key in elements}
160
+ evidces2 = {self.feat_names[key]: int(data[i, key]) for key in sigma}
161
+ term1 = cache.get(tuple(data[i, elements]), None)
162
+ if term1 is None:
163
+ term1 = func1(evidces1, **params1)
164
+ cache[tuple(data[i, elements])] = term1
165
+
166
+ term2 = func2(evidces2, counts[i], **params2)
167
+ val += term1 * term2
168
+ norm += term2
169
+
170
+ return 0.0 if norm == 0.0 else val / norm
171
+
172
+ @abstractmethod
173
+ def compute(self, data: tuple | None, N=100):
174
+ # Computes the Shapley values for the target node based on the provided data.
175
+ # This method must be implemented in subclasses
176
+ raise NotImplementedError("This method must be implemented in subclasses.")
@@ -0,0 +1,70 @@
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
+ from collections.abc import MutableMapping
42
+
43
+
44
+ class Explanation(MutableMapping):
45
+ def __init__(self, values, importances, feature_names, data, baseline, func, values_type) -> None:
46
+ self._values = values
47
+ self.importances = importances
48
+ self.feature_names = feature_names
49
+ self.data = data
50
+ self.baseline = baseline
51
+ self.func = func
52
+ self.values_type = values_type
53
+
54
+ def __getitem__(self, key):
55
+ return self._values[key]
56
+
57
+ def __setitem__(self, key, value):
58
+ self._values[key] = value
59
+
60
+ def __delitem__(self, key):
61
+ del self._values[key]
62
+
63
+ def __iter__(self):
64
+ return iter(self._values)
65
+
66
+ def __len__(self):
67
+ return len(self._values)
68
+
69
+ def __repr__(self):
70
+ return f"Explanation(values={self._values})"
@@ -0,0 +1,54 @@
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
+ from collections import OrderedDict
42
+
43
+
44
+ class FIFOCache(OrderedDict):
45
+ def __init__(self, maxlen, *args, **kwargs):
46
+ self.maxlen = maxlen
47
+ super().__init__(*args, **kwargs)
48
+
49
+ def __setitem__(self, key, value):
50
+ if key in self:
51
+ del self[key] # pour mettre à jour l'ordre
52
+ elif len(self) >= self.maxlen:
53
+ self.popitem(last=False) # retire le plus ancien
54
+ super().__setitem__(key, value)