pyAgrum-nightly 2.2.1.9.dev202510271761405498__cp310-abi3-win_amd64.whl → 2.3.0.9.dev202510281761586496__cp310-abi3-win_amd64.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.
- pyagrum/_pyagrum.pyd +0 -0
- pyagrum/common.py +1 -1
- pyagrum/config.py +1 -0
- pyagrum/explain/_ComputationCausal.py +75 -0
- pyagrum/explain/_ComputationConditional.py +48 -0
- pyagrum/explain/_ComputationMarginal.py +48 -0
- pyagrum/explain/_CustomShapleyCache.py +110 -0
- pyagrum/explain/_Explainer.py +176 -0
- pyagrum/explain/_Explanation.py +70 -0
- pyagrum/explain/_FIFOCache.py +54 -0
- pyagrum/explain/_ShallCausalValues.py +204 -0
- pyagrum/explain/_ShallConditionalValues.py +155 -0
- pyagrum/explain/_ShallMarginalValues.py +155 -0
- pyagrum/explain/_ShallValues.py +296 -0
- pyagrum/explain/_ShapCausalValues.py +208 -0
- pyagrum/explain/_ShapConditionalValues.py +126 -0
- pyagrum/explain/_ShapMarginalValues.py +191 -0
- pyagrum/explain/_ShapleyValues.py +298 -0
- pyagrum/explain/__init__.py +81 -0
- pyagrum/explain/_explGeneralizedMarkovBlanket.py +152 -0
- pyagrum/explain/_explIndependenceListForPairs.py +146 -0
- pyagrum/explain/_explInformationGraph.py +264 -0
- pyagrum/explain/notebook/__init__.py +54 -0
- pyagrum/explain/notebook/_bar.py +142 -0
- pyagrum/explain/notebook/_beeswarm.py +174 -0
- pyagrum/explain/notebook/_showShapValues.py +97 -0
- pyagrum/explain/notebook/_waterfall.py +220 -0
- pyagrum/explain/shapley.py +225 -0
- pyagrum/lib/explain.py +11 -490
- pyagrum/pyagrum.py +17 -10
- {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/METADATA +1 -1
- {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/RECORD +36 -12
- pyagrum/lib/shapley.py +0 -661
- {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSE.md +0 -0
- {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
- {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/LICENSES/MIT.txt +0 -0
- {pyagrum_nightly-2.2.1.9.dev202510271761405498.dist-info → pyagrum_nightly-2.3.0.9.dev202510281761586496.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,204 @@
|
|
|
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._ShallValues import ShallValues
|
|
43
|
+
from pyagrum.explain._CustomShapleyCache import CustomShapleyCache
|
|
44
|
+
from pyagrum.explain._ComputationCausal import CausalComputation
|
|
45
|
+
from pyagrum.explain._FIFOCache import FIFOCache
|
|
46
|
+
|
|
47
|
+
import numpy as np
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class CausalShallValues(ShallValues, CausalComputation):
|
|
51
|
+
"""
|
|
52
|
+
The CausalShallValues class computes the Causal Shall values in a Bayesian Network.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, bn: gum.BayesNet, background: tuple | None, sample_size: int = 1000, log: bool = True):
|
|
56
|
+
"""
|
|
57
|
+
Note 1 : All rows in the background data that contain NaN values in columns corresponding to variables in the Bayesian Network will be dropped.
|
|
58
|
+
Note 2 : In comparison to Marginal and Conditional Shall values it is impossible to calculate empirical probabilities 'true to the data'.
|
|
59
|
+
We are forced to calculate probabilités 'true to the model'.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
Parameters:
|
|
63
|
+
------
|
|
64
|
+
bn : pyagrum.BayesNet
|
|
65
|
+
The Bayesian Network.
|
|
66
|
+
background : tuple[pandas.DataFrame, bool] | None
|
|
67
|
+
A tuple containing a pandas DataFrame and a boolean indicating whether the DataFrame includes labels or positional values.
|
|
68
|
+
sample_size : int
|
|
69
|
+
The size of the background sample to generate if `background` is None.
|
|
70
|
+
log : bool
|
|
71
|
+
If True, applies a logarithmic transformation to the probabilities.
|
|
72
|
+
|
|
73
|
+
Raises
|
|
74
|
+
------
|
|
75
|
+
TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
|
|
76
|
+
ValueError : If background data does not contain all variables present in the Bayesian Network or if
|
|
77
|
+
background data is empty after rows with NaNs were dropped.
|
|
78
|
+
|
|
79
|
+
Raises:
|
|
80
|
+
------
|
|
81
|
+
TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
|
|
82
|
+
ValueError : If background data does not contain all variables present in the Bayesian Network or if
|
|
83
|
+
background data is empty after rows with NaNs were dropped.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
super().__init__(bn, background, sample_size, log)
|
|
87
|
+
self.baseline = self._value(
|
|
88
|
+
data=self._data,
|
|
89
|
+
counts=self.counts,
|
|
90
|
+
elements=self.vars_ids,
|
|
91
|
+
sigma=self.vars_ids,
|
|
92
|
+
cache=FIFOCache(100),
|
|
93
|
+
func1=self._joint,
|
|
94
|
+
params1={},
|
|
95
|
+
func2=self._weight,
|
|
96
|
+
params2={"doLazy": gum.LazyPropagation(self.bn)},
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def _coalition_contribution(self, posterior_prob_with, posterior_prob_without, m, s):
|
|
100
|
+
return (posterior_prob_with - posterior_prob_without) / self._invcoeff_shap(m, s)
|
|
101
|
+
|
|
102
|
+
def _shall_1dim(self, x):
|
|
103
|
+
contributions = np.zeros(self.M) # M : number of nodes in BN
|
|
104
|
+
|
|
105
|
+
# Caches
|
|
106
|
+
custom_cache = CustomShapleyCache(5000)
|
|
107
|
+
fifo_cache = FIFOCache(1000)
|
|
108
|
+
# Sets the baseline probability in the cache.
|
|
109
|
+
custom_cache.set(0, (), self.baseline)
|
|
110
|
+
# Compute the coalitions
|
|
111
|
+
coalitions = self._coalitions(self.vars_ids)
|
|
112
|
+
|
|
113
|
+
for tau in coalitions:
|
|
114
|
+
# self.ie.eraseAllEvidence()
|
|
115
|
+
doNet = self._doCalculus(self.bn, tau) # new BN
|
|
116
|
+
sigma = self._outOfCoalition(tau, self.vars_ids) # all nodes \ tau
|
|
117
|
+
|
|
118
|
+
doInst = gum.Instantiation()
|
|
119
|
+
for var in doNet.ids(self.feat_names):
|
|
120
|
+
doInst.add(doNet.variable(var))
|
|
121
|
+
|
|
122
|
+
# Instanciation of tau
|
|
123
|
+
alpha = x[tau] # extract columns in tau
|
|
124
|
+
if sigma != []:
|
|
125
|
+
self._chgCpt(doNet, tau, alpha)
|
|
126
|
+
doLazy = gum.LazyPropagation(doNet)
|
|
127
|
+
doLazy.addTarget(tau[0]) # see if target should be added for optimization
|
|
128
|
+
idx = self._extract(self._data, tau, alpha)
|
|
129
|
+
# Compute the value for this coalition.
|
|
130
|
+
joint_with = self._value(
|
|
131
|
+
data=self._data[idx],
|
|
132
|
+
counts=self.counts[idx],
|
|
133
|
+
elements=self.vars_ids,
|
|
134
|
+
sigma=sigma,
|
|
135
|
+
cache=fifo_cache,
|
|
136
|
+
func1=self._joint,
|
|
137
|
+
params1={},
|
|
138
|
+
func2=self._weight,
|
|
139
|
+
params2={"doLazy": doLazy},
|
|
140
|
+
)
|
|
141
|
+
else:
|
|
142
|
+
self.inst.fromdict({self.feat_names[key]: int(val) for key, val in zip(tau, alpha)})
|
|
143
|
+
joint = self.bn.jointProbability(self.inst)
|
|
144
|
+
joint_with = self.func(joint)
|
|
145
|
+
|
|
146
|
+
custom_cache.set(0, tuple(tau), joint_with)
|
|
147
|
+
# Contribution of each feature
|
|
148
|
+
for t in tau:
|
|
149
|
+
key = tuple((f for f in tau if f != t))
|
|
150
|
+
joint_without = custom_cache.get(0, key)
|
|
151
|
+
contributions[t] += self._coalition_contribution(joint_with, joint_without, len(self.vars_ids), len(tau) - 1)
|
|
152
|
+
return contributions
|
|
153
|
+
|
|
154
|
+
def _shall_ndim(self, x):
|
|
155
|
+
# Initialisation
|
|
156
|
+
contributions = np.zeros((self.M, len(x)))
|
|
157
|
+
|
|
158
|
+
# Caches
|
|
159
|
+
custom_cache = CustomShapleyCache(5000)
|
|
160
|
+
fifo_cache = FIFOCache(1000)
|
|
161
|
+
# Sets the baseline probability in the cache.
|
|
162
|
+
custom_cache.set(0, (), self.baseline)
|
|
163
|
+
# Compute the coalitions
|
|
164
|
+
coalitions = self._coalitions(self.vars_ids)
|
|
165
|
+
|
|
166
|
+
for tau in coalitions:
|
|
167
|
+
doNet = self._doCalculus(self.bn, tau)
|
|
168
|
+
sigma = self._outOfCoalition(tau, self.vars_ids)
|
|
169
|
+
|
|
170
|
+
for i in range(len(x)):
|
|
171
|
+
alpha = x[i, tau]
|
|
172
|
+
if sigma != []:
|
|
173
|
+
# Instanciation of tau
|
|
174
|
+
self._chgCpt(doNet, tau, alpha) # BN knowing alpha
|
|
175
|
+
doLazy = gum.LazyPropagation(doNet)
|
|
176
|
+
doLazy.addTarget(tau[0]) # just to speed up the calculation
|
|
177
|
+
idx = self._extract(self._data, tau, alpha)
|
|
178
|
+
# Compute the value for this coalition.
|
|
179
|
+
joint_with = self._value(
|
|
180
|
+
data=self._data[idx],
|
|
181
|
+
counts=self.counts[idx],
|
|
182
|
+
elements=self.vars_ids,
|
|
183
|
+
sigma=sigma,
|
|
184
|
+
cache=fifo_cache,
|
|
185
|
+
func1=self._joint,
|
|
186
|
+
params1={},
|
|
187
|
+
func2=self._weight,
|
|
188
|
+
params2={"doLazy": doLazy},
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
else:
|
|
192
|
+
self.inst.fromdict({self.feat_names[key]: int(val) for key, val in zip(tau, alpha)})
|
|
193
|
+
joint = self.bn.jointProbability(self.inst)
|
|
194
|
+
joint_with = self.func(joint)
|
|
195
|
+
|
|
196
|
+
custom_cache.set(i, tuple(tau), joint_with)
|
|
197
|
+
# Contribution of each feature
|
|
198
|
+
for t in tau:
|
|
199
|
+
key = tuple((f for f in tau if f != t))
|
|
200
|
+
joint_without = custom_cache.get(i, key) if len(key) > 0 else custom_cache.get(0, ())
|
|
201
|
+
contributions[t, i] += self._coalition_contribution(
|
|
202
|
+
joint_with, joint_without, len(self.vars_ids), len(tau) - 1
|
|
203
|
+
)
|
|
204
|
+
return contributions
|
|
@@ -0,0 +1,155 @@
|
|
|
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
|
+
from pyagrum.explain._ShallValues import ShallValues
|
|
44
|
+
from pyagrum.explain._CustomShapleyCache import CustomShapleyCache
|
|
45
|
+
from pyagrum.explain._FIFOCache import FIFOCache
|
|
46
|
+
from pyagrum.explain._ComputationConditional import ConditionalComputation
|
|
47
|
+
|
|
48
|
+
import numpy as np
|
|
49
|
+
from warnings import warn
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ConditionalShallValues(ShallValues, ConditionalComputation):
|
|
53
|
+
"""
|
|
54
|
+
The ConditionalShallValues class computes the conditional Shall values in a Bayesian Network.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
def __init__(self, bn: gum.BayesNet, background: tuple | None, sample_size: int = 1000, log: bool = True):
|
|
58
|
+
"""
|
|
59
|
+
Note 1 : All rows in the background data that contain NaN values in columns corresponding to variables in the Bayesian Network will be dropped.
|
|
60
|
+
Note 2 : For small databases SHALL values can be incorrect.
|
|
61
|
+
|
|
62
|
+
Parameters:
|
|
63
|
+
------
|
|
64
|
+
bn : pyagrum.BayesNet
|
|
65
|
+
The Bayesian Network.
|
|
66
|
+
background : tuple[pandas.DataFrame, bool] | None
|
|
67
|
+
A tuple containing a pandas DataFrame and a boolean indicating whether the DataFrame includes labels or positional values.
|
|
68
|
+
sample_size : int
|
|
69
|
+
The size of the background sample to generate if `background` is None.
|
|
70
|
+
log : bool
|
|
71
|
+
If True, applies a logarithmic transformation to the probabilities.
|
|
72
|
+
|
|
73
|
+
Raises
|
|
74
|
+
------
|
|
75
|
+
TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
|
|
76
|
+
ValueError : If background data does not contain all variables present in the Bayesian Network or if
|
|
77
|
+
background data is empty after rows with NaNs were dropped.
|
|
78
|
+
|
|
79
|
+
Raises:
|
|
80
|
+
------
|
|
81
|
+
TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
|
|
82
|
+
ValueError : If background data does not contain all variables present in the Bayesian Network or if
|
|
83
|
+
background data is empty after rows with NaNs were dropped.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
super().__init__(bn, background, sample_size, log) # Initializes the ShapleyValues class.
|
|
87
|
+
self.baseline = self._value(
|
|
88
|
+
data=self._data,
|
|
89
|
+
counts=self.counts,
|
|
90
|
+
elements=self.vars_ids,
|
|
91
|
+
sigma=[],
|
|
92
|
+
cache=FIFOCache(100),
|
|
93
|
+
func1=self._joint,
|
|
94
|
+
params1={},
|
|
95
|
+
func2=self._weight,
|
|
96
|
+
params2={},
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def _coalition_contribution(self, k, ex, feature, nodes_id, nodes_vals, cache, fifo_cache):
|
|
100
|
+
# key2 is always set since subsets are sorted by length
|
|
101
|
+
key1, key2, _ = cache.generate_keys(self.bn, None, feature, nodes_id)
|
|
102
|
+
# key1 : nodes_id, key2 : nodes id without feature
|
|
103
|
+
if k == 0:
|
|
104
|
+
idx = self._extract(self._data, nodes_id, nodes_vals)
|
|
105
|
+
# warn(f"Extracted database is empty ({self.feat_names[nodes_id]} = {nodes_vals}). Conditional SHALL values may be incorrect. ")
|
|
106
|
+
cache.set(
|
|
107
|
+
ex,
|
|
108
|
+
key1,
|
|
109
|
+
self._value(
|
|
110
|
+
data=self._data[idx],
|
|
111
|
+
counts=self.counts[idx],
|
|
112
|
+
elements=self.vars_ids,
|
|
113
|
+
sigma=[],
|
|
114
|
+
cache=fifo_cache,
|
|
115
|
+
func1=self._joint,
|
|
116
|
+
params1={},
|
|
117
|
+
func2=self._weight,
|
|
118
|
+
params2={},
|
|
119
|
+
),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
joint_prob_with = cache.get(ex, key1) # with feature
|
|
123
|
+
joint_prob_without = cache.get(ex, key2) if len(key1) > 1 else cache.get(-1, "") # without feature
|
|
124
|
+
return (joint_prob_with - joint_prob_without) / self._invcoeff_shap(len(self.vars_ids), len(nodes_id) - 1)
|
|
125
|
+
|
|
126
|
+
def _shall_1dim(self, x) -> np.ndarray:
|
|
127
|
+
contributions = np.zeros((self.M)) # Initializes contributions array.
|
|
128
|
+
fifo_cache = FIFOCache(2000)
|
|
129
|
+
cache = CustomShapleyCache(5000) # Initializes the custom cache.
|
|
130
|
+
cache.set(-1, "", self.baseline) # Sets the baseline probability in the cache.
|
|
131
|
+
coalitions = self._coalitions(self.vars_ids) # Generates coalitions.
|
|
132
|
+
for nodes_id in coalitions:
|
|
133
|
+
nodes_vals = x[nodes_id] # Gets the values of the nodes in the coalition.
|
|
134
|
+
for k, feature in enumerate(nodes_id):
|
|
135
|
+
# Accumulates the contribution for each feature.
|
|
136
|
+
contributions[feature] += self._coalition_contribution(
|
|
137
|
+
k, 0, int(feature), nodes_id, nodes_vals, cache, fifo_cache
|
|
138
|
+
)
|
|
139
|
+
return contributions
|
|
140
|
+
|
|
141
|
+
def _shall_ndim(self, x) -> np.ndarray:
|
|
142
|
+
contributions = np.zeros((self.M, len(x))) # Initializes contributions array.
|
|
143
|
+
fifo_cache = FIFOCache(2000)
|
|
144
|
+
cache = CustomShapleyCache(5000) # Initializes the custom cache.
|
|
145
|
+
cache.set(-1, "", self.baseline) # Sets the baseline probability in the cache.
|
|
146
|
+
|
|
147
|
+
coalitions = self._coalitions(self.vars_ids) # Generates coalitions.
|
|
148
|
+
for i, nodes_id in enumerate(coalitions):
|
|
149
|
+
data_vals = x[:, nodes_id] # Gets the values of the nodes in the coalition.
|
|
150
|
+
for ex, nodes_vals in enumerate(data_vals):
|
|
151
|
+
for k, feature in enumerate(nodes_id):
|
|
152
|
+
contributions[feature, ex] += self._coalition_contribution(
|
|
153
|
+
k, ex, int(feature), nodes_id, nodes_vals, cache, fifo_cache
|
|
154
|
+
)
|
|
155
|
+
return contributions
|
|
@@ -0,0 +1,155 @@
|
|
|
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._ShallValues import ShallValues
|
|
43
|
+
from pyagrum.explain._ComputationMarginal import MarginalComputation
|
|
44
|
+
from pyagrum.explain._CustomShapleyCache import CustomShapleyCache
|
|
45
|
+
from pyagrum.explain._FIFOCache import FIFOCache
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
import numpy as np
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class MarginalShallValues(ShallValues, MarginalComputation):
|
|
52
|
+
"""
|
|
53
|
+
The MarginalShallValues class computes the Marginal Shall values in a Bayesian Network.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(self, bn: gum.BayesNet, background: tuple | None, sample_size: int = 1000, log: bool = True):
|
|
57
|
+
"""
|
|
58
|
+
Note: All rows in the background data that contain NaN values in columns corresponding to variables in the Bayesian Network will be dropped.
|
|
59
|
+
|
|
60
|
+
Parameters:
|
|
61
|
+
------
|
|
62
|
+
bn : pyagrum.BayesNet
|
|
63
|
+
The Bayesian Network.
|
|
64
|
+
background : tuple[pandas.DataFrame, bool] | None
|
|
65
|
+
A tuple containing a pandas DataFrame and a boolean indicating whether the DataFrame includes labels or positional values.
|
|
66
|
+
sample_size : int
|
|
67
|
+
The size of the background sample to generate if `background` is None.
|
|
68
|
+
log : bool
|
|
69
|
+
If True, applies a logarithmic transformation to the probabilities.
|
|
70
|
+
|
|
71
|
+
Raises
|
|
72
|
+
------
|
|
73
|
+
TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
|
|
74
|
+
ValueError : If background data does not contain all variables present in the Bayesian Network or if
|
|
75
|
+
background data is empty after rows with NaNs were dropped.
|
|
76
|
+
|
|
77
|
+
Raises:
|
|
78
|
+
------
|
|
79
|
+
TypeError : If bn is not a gum.BayesNet instance, background is not a tuple.
|
|
80
|
+
ValueError : If background data does not contain all variables present in the Bayesian Network or if
|
|
81
|
+
background data is empty after rows with NaNs were dropped.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
super().__init__(bn, background, sample_size, log)
|
|
85
|
+
|
|
86
|
+
self.baseline = self._value(
|
|
87
|
+
data=self._data,
|
|
88
|
+
counts=self.counts,
|
|
89
|
+
elements=self.vars_ids,
|
|
90
|
+
sigma=[],
|
|
91
|
+
cache=FIFOCache(100),
|
|
92
|
+
func1=self._joint,
|
|
93
|
+
params1={},
|
|
94
|
+
func2=self._weight,
|
|
95
|
+
params2={},
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
def _coalition_contribution(self, k, ex, feature, fifo_cache, nodes_id, nodes_vals, cache):
|
|
99
|
+
key1, key2, _ = cache.generate_keys(self.bn, None, feature, nodes_id)
|
|
100
|
+
if k == 0:
|
|
101
|
+
interv = self._data.copy()
|
|
102
|
+
interv[:, nodes_id] = nodes_vals
|
|
103
|
+
cache.set(
|
|
104
|
+
ex,
|
|
105
|
+
key1,
|
|
106
|
+
self._value(
|
|
107
|
+
data=interv,
|
|
108
|
+
counts=self.counts,
|
|
109
|
+
elements=self.vars_ids,
|
|
110
|
+
sigma=[],
|
|
111
|
+
cache=fifo_cache,
|
|
112
|
+
func1=self._joint,
|
|
113
|
+
params1={},
|
|
114
|
+
func2=self._weight,
|
|
115
|
+
params2={},
|
|
116
|
+
),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
joint_prob_with = cache.get(ex, key1)
|
|
120
|
+
joint_prob_without = cache.get(ex, key2) if len(key1) > 1 else cache.get(-1, ())
|
|
121
|
+
return (joint_prob_with - joint_prob_without) / self._invcoeff_shap(len(self.vars_ids), len(nodes_id) - 1)
|
|
122
|
+
|
|
123
|
+
def _shall_1dim(self, x):
|
|
124
|
+
# Result initialisation.
|
|
125
|
+
contributions = np.zeros(self.M)
|
|
126
|
+
# Cache management.
|
|
127
|
+
fifo_cache = FIFOCache(2000)
|
|
128
|
+
custom_cache = CustomShapleyCache(5000)
|
|
129
|
+
# Sets the baseline probability in the cache.
|
|
130
|
+
custom_cache.set(-1, (), self.baseline)
|
|
131
|
+
coalitions = self._coalitions(self.vars_ids)
|
|
132
|
+
for nodes_id in coalitions:
|
|
133
|
+
nodes_vals = x[nodes_id]
|
|
134
|
+
for k, feature in enumerate(nodes_id):
|
|
135
|
+
contributions[feature] += self._coalition_contribution(
|
|
136
|
+
k, 0, int(feature), fifo_cache, nodes_id, nodes_vals, custom_cache
|
|
137
|
+
)
|
|
138
|
+
return contributions
|
|
139
|
+
|
|
140
|
+
def _shall_ndim(self, x):
|
|
141
|
+
# Result initialisation.
|
|
142
|
+
contributions = np.zeros((self.M, len(x)))
|
|
143
|
+
# Cache management.
|
|
144
|
+
fifo_cache = FIFOCache(2000)
|
|
145
|
+
custom_cache = CustomShapleyCache(5000)
|
|
146
|
+
# Sets the baseline probability in the cache.
|
|
147
|
+
custom_cache.set(-1, (), self.baseline)
|
|
148
|
+
coalitions = self._coalitions(self.vars_ids)
|
|
149
|
+
for nodes_id in coalitions:
|
|
150
|
+
for ex, nodes_values in enumerate(x[:, nodes_id]):
|
|
151
|
+
for k, feature in enumerate(nodes_id):
|
|
152
|
+
contributions[feature, ex] += self._coalition_contribution(
|
|
153
|
+
k, ex, int(feature), fifo_cache, nodes_id, nodes_values, custom_cache
|
|
154
|
+
)
|
|
155
|
+
return contributions
|