pyAgrum.lib.explain

The purpose of pyAgrum.lib.explain is to give tools to explain and interpret the structure and parameters of a Bayesian network.

Dealing with independence

pyAgrum.lib.explain.independenceListForPairs(bn, filename, target=None, plot=True, alphabetic=False)

get the p-values of the chi2 test of a (as simple as possible) independence proposition for every non arc.

Parameters
  • bn (gum.BayesNet) – the Bayesian network

  • filename (str) – the name of the csv database

  • alphabetic (bool) – if True, the list is alphabetically sorted else it is sorted by the p-value

  • target ((optional) str or int) – the name or id of the target variable

  • plot (bool) – if True, plot the result

Return type

the list

Dealing with mutual information and entropy

pyAgrum.lib.explain.getInformation(bn, evs=None, size=None, cmap=<matplotlib.colors.LinearSegmentedColormap object>)

get a HTML string for a bn annotated with results from inference : entropy and mutual information

Parameters
  • bn – the BN

  • evs – map of evidence

  • size – size of the graph

  • cmap – colour map used

Returns

the HTML string

pyAgrum.lib.explain.showInformation(bn, evs=None, target=None, size=None, cmap=<matplotlib.colors.LinearSegmentedColormap object>)

show a bn annotated with results from inference : entropy and mutual information

Parameters
  • bn – the BN

  • evs – map of evidence

  • target – (optional) the name or id of the target variable

  • size – size of the graph

  • cmap – colour map used

Returns

the graph

Dealing with ShapValues

Shap-Values in pyAgrum
class pyAgrum.lib.explain.ShapValues(bn, target)

Bases: object

The ShapValue class implements the calculation of Shap values in Bayesian networks.

The main implementation is based on Conditional Shap values 3, but the Interventional calculation method proposed in 2 is also present. In addition, a new causal method, based on 1, is implemented which is well suited for Bayesian networks.

1

Heskes, T., Sijben, E., Bucur, I., & Claassen, T. (2020). Causal Shapley Values: Exploiting Causal Knowledge. 34th Conference on Neural Information Processing Systems. Vancouver, Canada.

2

Janzing, D., Minorics, L., & Blöbaum, P. (2019). Feature relevance quantification in explainable AI: A causality problem. arXiv: Machine Learning. Retrieved 6 24, 2021, from https://arxiv.org/abs/1910.13413

3

Lundberg, S. M., & Su-In, L. (2017). A Unified Approach to Interpreting Model. 31st Conference on Neural Information Processing Systems. Long Beach, CA, USA.

causal(train, plot=False, plot_importance=False, percentage=False)

Compute the causal Shap Values for each variables.

Parameters
  • train (pandas.DataFrame) – the database

  • plot (bool) – if True, plot the violin graph of the shap values

  • plot_importance (bool) – if True, plot the importance plot

  • percentage (bool) – if True, the importance plot is shown in percent.

Return type

a dictionary Dict[str,float]

conditional(train, plot=False, plot_importance=False, percentage=False)

Compute the conditional Shap Values for each variables.

Parameters
  • train (pandas.DataFrame) – the database

  • plot (bool) – if True, plot the violin graph of the shap values

  • plot_importance (bool) – if True, plot the importance plot

  • percentage (bool) – if True, the importance plot is shown in percent.

Return type

a dictionary Dict[str,float]

marginal(train, sample_size=200, plot=False, plot_importance=False, percentage=False)

Compute the marginal Shap Values for each variables.

Parameters
  • train (pandas.DataFrame) – the database

  • sample_size (int) – The computation of marginal ShapValue is very slow. The parameter allow to compute only on a fragment of the database.

  • plot (bool) – if True, plot the violin graph of the shap values

  • plot_importance (bool) – if True, plot the importance plot

  • percentage (bool) – if True, the importance plot is shown in percent.

Return type

a dictionary Dict[str,float]

showShapValues(results, cmap='plasma')
Parameters
  • results (dict[str,float]) – The (Shap) values associates to each variable

  • cmap (Matplotlib.ColorMap) – The colormap used for colouring the nodes

Return type

a pydot.graph