Note
Go to the end to download the full example code.
Bayesian Networs
import bnlearn as bn
from utils import prepare_data, print_version_info
**********Tensorflow models could not be imported **********
print_version_info()
python 3.12.7 (main, Nov 5 2024, 16:16:58) [GCC 11.4.0]
os posix
ai4water 1.07
xgboost 2.1.3
easy_mpl 0.21.4
SeqMetrics 2.0.0
torch 2.5.1+cu124
numpy 1.26.4
pandas 1.5.3
matplotlib 3.8.4
sklearn 1.3.1
xarray 2024.3.0
netCDF4 1.7.2
seaborn 0.13.2
bnlearn 0.10.2
Script Executed on: Wed Jan 1 06:31:08 2025
tot_cpus 2
avail_cpus 2
mem_gib 7.612831115722656
data, *_ = prepare_data()
# Structure learning
# model = bn.structure_learning.fit(dfnum, methodtype='cl',
# black_list=['Embarked','Parch','Name'], root_node='Survived', bw_list_method='nodes')
model = bn.structure_learning.fit(data)
# Plot
G = bn.plot(model, interactive=False)

[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
[bnlearn] >Compute structure scores for model comparison (higher is better).
[bnlearn] >Set node properties.
[bnlearn] >Set edge properties.
[bnlearn] >Plot based on Bayesian model
[bnlearn] >Warning: [graphviz_layout] layout not found. The layout [spring_layout] is used instead.
Compute edge strength with the chi_square test statistic
model = bn.independence_test(model, data, test='chi_square', prune=True)
[bnlearn] >Compute edge strength with [chi_square]
# Plot
bn.plot(model, interactive=False, pos=G['pos'])

[bnlearn] >Set node properties.
[bnlearn]> Set edge weights based on the [chi_square] test statistic.
[bnlearn] >Converting source-target into adjacency matrix..
[bnlearn] >Making the matrix symmetric..
[bnlearn] >Converting source-target into adjacency matrix..
[bnlearn] >Making the matrix symmetric..
[bnlearn] >Set edge properties.
[bnlearn] >Plot based on Bayesian model
[bnlearn] >Existing coordinates from <pos> are used.
{'fig': <Figure size 2000x2000 with 1 Axes>, 'ax': <Figure size 2000x2000 with 1 Axes>, 'pos': {'Catalyst type': array([ 0.26696415, -0.24575961]), 'Surface area': array([ 0.26044175, -0.34923117]), 'Pore volume': array([ 0.6236698 , -0.59485537]), 'BandGap (eV)': array([-0.1017978, -0.0790936]), 'Fe': array([ 0.01457606, -0.09410166]), 'Au': array([0.06378086, 0.66055828]), 'solution pH': array([0.02479034, 0.54021307]), 'Bi': array([-0.04120939, -0.84926904]), 'O': array([ 0.06208691, -0.94096045]), 'Catalyst loading (g/L)': array([-0.67988509, 0.20893727]), 'Anions': array([-1. , 0.24778032]), 'Light intensity (W)': array([0.94647807, 0.49918651]), 'Ci (mg/L)': array([-0.43989564, 0.99659544])}, 'G': <networkx.classes.digraph.DiGraph object at 0x7f2d6d2bfa70>, 'node_properties': {'Catalyst type': {'node_color': '#ADD8E6', 'node_size': 800}, 'Surface area': {'node_color': '#ADD8E6', 'node_size': 800}, 'Pore volume': {'node_color': '#ADD8E6', 'node_size': 800}, 'BandGap (eV)': {'node_color': '#ADD8E6', 'node_size': 800}, 'Au': {'node_color': '#ADD8E6', 'node_size': 800}, 'Bi': {'node_color': '#ADD8E6', 'node_size': 800}, 'Fe': {'node_color': '#ADD8E6', 'node_size': 800}, 'O': {'node_color': '#ADD8E6', 'node_size': 800}, 'Catalyst loading (g/L)': {'node_color': '#ADD8E6', 'node_size': 800}, 'Light intensity (W)': {'node_color': '#ADD8E6', 'node_size': 800}, 'time (min)': {'node_color': '#ADD8E6', 'node_size': 800}, 'solution pH': {'node_color': '#ADD8E6', 'node_size': 800}, 'Anions': {'node_color': '#ADD8E6', 'node_size': 800}, 'Ci (mg/L)': {'node_color': '#ADD8E6', 'node_size': 800}, 'Efficiency (%)': {'node_color': '#ADD8E6', 'node_size': 800}}, 'edge_properties': {('Au', 'solution pH'): {'color': '#000000', 'weight': 1.5714427344833095, 'pvalue': 44.146059795672215, 'value': 1.0}, ('BandGap (eV)', 'Fe'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}, ('Bi', 'O'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}, ('Catalyst loading (g/L)', 'Anions'): {'color': '#000000', 'weight': 3.5976383206270603, 'pvalue': 159.0405499493875, 'value': 1.0}, ('Fe', 'Au'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}, ('Fe', 'Bi'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}, ('Surface area', 'BandGap (eV)'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}, ('Surface area', 'Pore volume'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}, ('solution pH', 'Catalyst loading (g/L)'): {'color': '#000000', 'weight': 3.4671832298527754, 'pvalue': 151.64315394035052, 'value': 1.0}, ('solution pH', 'Ci (mg/L)'): {'color': '#000000', 'weight': 1.2209844933186966, 'pvalue': 24.27348618326514, 'value': 1.0}, ('solution pH', 'Light intensity (W)'): {'color': '#000000', 'weight': 1.0, 'pvalue': 11.742661979837312, 'value': 1.0}, ('Catalyst type', 'Surface area'): {'color': '#000000', 'weight': 5.0, 'pvalue': 238.56082492408126, 'value': 1.0}}}
# Parameter learning
model = bn.parameter_learning.fit(model, data)
[bnlearn] >Parameter learning> Computing parameters using [bayes]
[bnlearn] >Converting [<class 'pgmpy.base.DAG.DAG'>] to BayesianNetwork model.
[bnlearn] >Converting adjmat to BayesianNetwork.
[bnlearn] >CPD of Catalyst type:
+------------------+-----------+
| Catalyst type(0) | 0.0875035 |
+------------------+-----------+
| Catalyst type(1) | 0.0875035 |
+------------------+-----------+
| Catalyst type(2) | 0.474979 |
+------------------+-----------+
| Catalyst type(3) | 0.0875035 |
+------------------+-----------+
| Catalyst type(4) | 0.0875035 |
+------------------+-----------+
| Catalyst type(5) | 0.0875035 |
+------------------+-----------+
| Catalyst type(6) | 0.0875035 |
+------------------+-----------+
[bnlearn] >CPD of Surface area:
+--------------------+-----+---------------------+
| Catalyst type | ... | Catalyst type(6) |
+--------------------+-----+---------------------+
| Surface area(0.0) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| Surface area(11.2) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| Surface area(14.5) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| Surface area(16.8) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| Surface area(18.8) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| Surface area(21.6) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| Surface area(45.0) | ... | 0.3153811045184848 |
+--------------------+-----+---------------------+
[bnlearn] >CPD of Pore volume:
+---------------------+-----+---------------------+
| Surface area | ... | Surface area(45.0) |
+---------------------+-----+---------------------+
| Pore volume(0.0) | ... | 0.11410314924691922 |
+---------------------+-----+---------------------+
| Pore volume(0.0028) | ... | 0.11410314924691922 |
+---------------------+-----+---------------------+
| Pore volume(0.0032) | ... | 0.11410314924691922 |
+---------------------+-----+---------------------+
| Pore volume(0.0035) | ... | 0.11410314924691922 |
+---------------------+-----+---------------------+
| Pore volume(0.0038) | ... | 0.11410314924691922 |
+---------------------+-----+---------------------+
| Pore volume(0.0043) | ... | 0.11410314924691922 |
+---------------------+-----+---------------------+
| Pore volume(0.0049) | ... | 0.3153811045184848 |
+---------------------+-----+---------------------+
[bnlearn] >CPD of BandGap (eV):
+--------------------+-----+---------------------+
| Surface area | ... | Surface area(45.0) |
+--------------------+-----+---------------------+
| BandGap (eV)(0.0) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| BandGap (eV)(2.35) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| BandGap (eV)(2.37) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| BandGap (eV)(2.42) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| BandGap (eV)(2.47) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| BandGap (eV)(2.6) | ... | 0.11410314924691922 |
+--------------------+-----+---------------------+
| BandGap (eV)(3.2) | ... | 0.3153811045184848 |
+--------------------+-----+---------------------+
[bnlearn] >CPD of Fe:
+--------------+-----+---------------------+
| BandGap (eV) | ... | BandGap (eV)(3.2) |
+--------------+-----+---------------------+
| Fe(0.0) | ... | 0.33439829605963795 |
+--------------+-----+---------------------+
| Fe(13.02) | ... | 0.13312034078807244 |
+--------------+-----+---------------------+
| Fe(13.07) | ... | 0.13312034078807244 |
+--------------+-----+---------------------+
| Fe(13.1) | ... | 0.13312034078807244 |
+--------------+-----+---------------------+
| Fe(13.6) | ... | 0.13312034078807244 |
+--------------+-----+---------------------+
| Fe(13.68) | ... | 0.13312034078807244 |
+--------------+-----+---------------------+
[bnlearn] >CPD of Au:
+----------+---------------------+-----+---------------------+
| Fe | Fe(0.0) | ... | Fe(13.68) |
+----------+---------------------+-----+---------------------+
| Au(0.0) | 0.441340782122905 | ... | 0.03351206434316354 |
+----------+---------------------+-----+---------------------+
| Au(0.25) | 0.13966480446927373 | ... | 0.03351206434316354 |
+----------+---------------------+-----+---------------------+
| Au(0.51) | 0.13966480446927373 | ... | 0.03351206434316354 |
+----------+---------------------+-----+---------------------+
| Au(1.0) | 0.13966480446927373 | ... | 0.8659517426273459 |
+----------+---------------------+-----+---------------------+
| Au(1.98) | 0.13966480446927373 | ... | 0.03351206434316354 |
+----------+---------------------+-----+---------------------+
[bnlearn] >CPD of solution pH:
+------------------+-----+--------------------+
| Au | ... | Au(1.98) |
+------------------+-----+--------------------+
| solution pH(3.0) | ... | 0.1694915254237288 |
+------------------+-----+--------------------+
| solution pH(5.0) | ... | 0.1694915254237288 |
+------------------+-----+--------------------+
| solution pH(5.4) | ... | 0.3220338983050847 |
+------------------+-----+--------------------+
| solution pH(7.0) | ... | 0.1694915254237288 |
+------------------+-----+--------------------+
| solution pH(9.0) | ... | 0.1694915254237288 |
+------------------+-----+--------------------+
[bnlearn] >CPD of Bi:
+-----------+---------------------+-----+----------------------+
| Fe | Fe(0.0) | ... | Fe(13.68) |
+-----------+---------------------+-----+----------------------+
| Bi(0.0) | 0.41806331471135944 | ... | 0.027926720285969615 |
+-----------+---------------------+-----+----------------------+
| Bi(55.11) | 0.11638733705772814 | ... | 0.027926720285969615 |
+-----------+---------------------+-----+----------------------+
| Bi(55.2) | 0.11638733705772814 | ... | 0.027926720285969615 |
+-----------+---------------------+-----+----------------------+
| Bi(55.3) | 0.11638733705772814 | ... | 0.027926720285969615 |
+-----------+---------------------+-----+----------------------+
| Bi(55.79) | 0.11638733705772814 | ... | 0.860366398570152 |
+-----------+---------------------+-----+----------------------+
| Bi(55.92) | 0.11638733705772814 | ... | 0.027926720285969615 |
+-----------+---------------------+-----+----------------------+
[bnlearn] >CPD of O:
+----------+---------------------+-----+---------------------+
| Bi | Bi(0.0) | ... | Bi(55.92) |
+----------+---------------------+-----+---------------------+
| O(0.0) | 0.41806331471135944 | ... | 0.13706140350877194 |
+----------+---------------------+-----+---------------------+
| O(28.5) | 0.11638733705772814 | ... | 0.31469298245614036 |
+----------+---------------------+-----+---------------------+
| O(29.52) | 0.11638733705772814 | ... | 0.13706140350877194 |
+----------+---------------------+-----+---------------------+
| O(31.36) | 0.11638733705772814 | ... | 0.13706140350877194 |
+----------+---------------------+-----+---------------------+
| O(31.48) | 0.11638733705772814 | ... | 0.13706140350877194 |
+----------+---------------------+-----+---------------------+
| O(31.6) | 0.11638733705772814 | ... | 0.13706140350877194 |
+----------+---------------------+-----+---------------------+
[bnlearn] >CPD of Catalyst loading (g/L):
+-----------------------------+-----+---------------------+
| solution pH | ... | solution pH(9.0) |
+-----------------------------+-----+---------------------+
| Catalyst loading (g/L)(0.0) | ... | 0.14124293785310735 |
+-----------------------------+-----+---------------------+
| Catalyst loading (g/L)(0.5) | ... | 0.14124293785310735 |
+-----------------------------+-----+---------------------+
| Catalyst loading (g/L)(1.0) | ... | 0.2937853107344633 |
+-----------------------------+-----+---------------------+
| Catalyst loading (g/L)(1.5) | ... | 0.14124293785310735 |
+-----------------------------+-----+---------------------+
| Catalyst loading (g/L)(2.0) | ... | 0.14124293785310735 |
+-----------------------------+-----+---------------------+
| Catalyst loading (g/L)(2.5) | ... | 0.14124293785310735 |
+-----------------------------+-----+---------------------+
[bnlearn] >CPD of Anions:
+------------------------+-----+-----------------------------+
| Catalyst loading (g/L) | ... | Catalyst loading (g/L)(2.5) |
+------------------------+-----+-----------------------------+
| Anions(0) | ... | 0.13706140350877194 |
+------------------------+-----+-----------------------------+
| Anions(1) | ... | 0.13706140350877194 |
+------------------------+-----+-----------------------------+
| Anions(2) | ... | 0.13706140350877194 |
+------------------------+-----+-----------------------------+
| Anions(3) | ... | 0.13706140350877194 |
+------------------------+-----+-----------------------------+
| Anions(4) | ... | 0.13706140350877194 |
+------------------------+-----+-----------------------------+
| Anions(5) | ... | 0.31469298245614036 |
+------------------------+-----+-----------------------------+
[bnlearn] >CPD of Light intensity (W):
+--------------------------+-----+---------------------+
| solution pH | ... | solution pH(9.0) |
+--------------------------+-----+---------------------+
| Light intensity (W)(25) | ... | 0.2824858757062147 |
+--------------------------+-----+---------------------+
| Light intensity (W)(55) | ... | 0.2824858757062147 |
+--------------------------+-----+---------------------+
| Light intensity (W)(105) | ... | 0.43502824858757067 |
+--------------------------+-----+---------------------+
[bnlearn] >CPD of Ci (mg/L):
+---------------+-----+--------------------+
| solution pH | ... | solution pH(9.0) |
+---------------+-----+--------------------+
| Ci (mg/L)(5) | ... | 0.3220338983050847 |
+---------------+-----+--------------------+
| Ci (mg/L)(10) | ... | 0.1694915254237288 |
+---------------+-----+--------------------+
| Ci (mg/L)(20) | ... | 0.1694915254237288 |
+---------------+-----+--------------------+
| Ci (mg/L)(40) | ... | 0.1694915254237288 |
+---------------+-----+--------------------+
| Ci (mg/L)(80) | ... | 0.1694915254237288 |
+---------------+-----+--------------------+
[bnlearn] >Compute structure scores for model comparison (higher is better).
[bnlearn] >WARNING> Skipping computing structure score for [k2].
# Make inference
query = bn.inference.fit(
model,
variables=['Catalyst type'],
evidence={'solution pH':3.0,
'Ci (mg/L)':5.0})
[bnlearn] >Variable Elimination.
[bnlearn] >Warning: variable(s) [None] does not exists in DAG.
[bnlearn] >Data is stored in [query.df]
+----+-----------------+-----------+
| | Catalyst type | p |
+====+=================+===========+
| 0 | 0 | 0.0965713 |
+----+-----------------+-----------+
| 1 | 1 | 0.0965713 |
+----+-----------------+-----------+
| 2 | 2 | 0.420722 |
+----+-----------------+-----------+
| 3 | 3 | 0.0965713 |
+----+-----------------+-----------+
| 4 | 4 | 0.0965181 |
+----+-----------------+-----------+
| 5 | 5 | 0.0965278 |
+----+-----------------+-----------+
| 6 | 6 | 0.0965181 |
+----+-----------------+-----------+
print(query)
+------------------+----------------------+
| Catalyst type | phi(Catalyst type) |
+==================+======================+
| Catalyst type(0) | 0.0966 |
+------------------+----------------------+
| Catalyst type(1) | 0.0966 |
+------------------+----------------------+
| Catalyst type(2) | 0.4207 |
+------------------+----------------------+
| Catalyst type(3) | 0.0966 |
+------------------+----------------------+
| Catalyst type(4) | 0.0965 |
+------------------+----------------------+
| Catalyst type(5) | 0.0965 |
+------------------+----------------------+
| Catalyst type(6) | 0.0965 |
+------------------+----------------------+
Total running time of the script: (0 minutes 14.608 seconds)