No description has been provided for this image

No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image No description has been provided for this image


Bnlearn for Python¶

Welcome to the notebook of bnlearn. bnlearn is Python package for learning the graphical structure of Bayesian networks, parameter learning, inference and sampling methods. Because probabilistic graphical models can be difficult in usage, Bnlearn for python (this package) is build on the pgmpy package and contains the most-wanted pipelines. Navigate to API documentations for more detailed information.

The core functionalities are:
* Causal Discovery
* Structure Learning
* Parameter Learning
* Inferences using do-calculus


Read the Medium blogs.¶

1. A Step-by-Step Guide in detecting causal relationships using Bayesian Structure Learning in Python
2. A step-by-step guide in designing knowledge-driven models using Bayesian theorem.
3. The Power of Bayesian Causal Inference: A Comparative Analysis of Libraries to Reveal Hidden Causality in Your Dataset.
4. Chat with Your Dataset using Bayesian Inferences.

Support¶

This library is for free but it runs on coffee! :)

You can support in various ways, have a look at the sponser page. Report bugs, issues and feature extensions at github page.

No description has been provided for this image Follow me on Medium

In [ ]:
# Package installeren
!pip install -U bnlearn
!pip install datazets
Requirement already satisfied: bnlearn in /usr/local/lib/python3.10/dist-packages (0.8.7)
Collecting bnlearn
  Downloading bnlearn-0.8.8-py3-none-any.whl (70 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 70.3/70.3 kB 899.8 kB/s eta 0:00:00
Requirement already satisfied: pgmpy>=0.1.18 in /usr/local/lib/python3.10/dist-packages (from bnlearn) (0.1.25)
Requirement already satisfied: networkx>=2.7.1 in /usr/local/lib/python3.10/dist-packages (from bnlearn) (3.3)
Requirement already satisfied: matplotlib>=3.3.4 in /usr/local/lib/python3.10/dist-packages (from bnlearn) (3.7.1)
Requirement already satisfied: numpy>=1.24.1 in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.25.2)
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.5.3)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from bnlearn) (4.66.4)
Requirement already satisfied: ismember in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.0.4)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.2.2)
Requirement already satisfied: funcsigs in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.0.2)
Requirement already satisfied: statsmodels in /usr/local/lib/python3.10/dist-packages (from bnlearn) (0.14.2)
Requirement already satisfied: python-louvain in /usr/local/lib/python3.10/dist-packages (from bnlearn) (0.16)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from bnlearn) (24.0)
Requirement already satisfied: df2onehot in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.0.7)
Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from bnlearn) (2023.6.0)
Requirement already satisfied: pypickle in /usr/local/lib/python3.10/dist-packages (from bnlearn) (1.1.0)
Requirement already satisfied: tabulate in /usr/local/lib/python3.10/dist-packages (from bnlearn) (0.9.0)
Requirement already satisfied: ipywidgets in /usr/local/lib/python3.10/dist-packages (from bnlearn) (7.7.1)
Requirement already satisfied: datazets in /usr/local/lib/python3.10/dist-packages (from bnlearn) (0.1.9)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (1.2.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (4.51.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (1.4.5)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3.4->bnlearn) (2.8.2)
Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from pgmpy>=0.1.18->bnlearn) (1.11.4)
Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from pgmpy>=0.1.18->bnlearn) (2.3.0+cu121)
Requirement already satisfied: joblib in /usr/local/lib/python3.10/dist-packages (from pgmpy>=0.1.18->bnlearn) (1.4.2)
Requirement already satisfied: opt-einsum in /usr/local/lib/python3.10/dist-packages (from pgmpy>=0.1.18->bnlearn) (3.3.0)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from datazets->bnlearn) (2.31.0)
Requirement already satisfied: ipykernel>=4.5.1 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->bnlearn) (5.5.6)
Requirement already satisfied: ipython-genutils~=0.2.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->bnlearn) (0.2.0)
Requirement already satisfied: traitlets>=4.3.1 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->bnlearn) (5.7.1)
Requirement already satisfied: widgetsnbextension~=3.6.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->bnlearn) (3.6.6)
Requirement already satisfied: ipython>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->bnlearn) (7.34.0)
Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets->bnlearn) (3.0.10)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->bnlearn) (2023.4)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->bnlearn) (3.5.0)
Requirement already satisfied: patsy>=0.5.6 in /usr/local/lib/python3.10/dist-packages (from statsmodels->bnlearn) (0.5.6)
Requirement already satisfied: jupyter-client in /usr/local/lib/python3.10/dist-packages (from ipykernel>=4.5.1->ipywidgets->bnlearn) (6.1.12)
Requirement already satisfied: tornado>=4.2 in /usr/local/lib/python3.10/dist-packages (from ipykernel>=4.5.1->ipywidgets->bnlearn) (6.3.3)
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (67.7.2)
Requirement already satisfied: jedi>=0.16 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (0.19.1)
Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (4.4.2)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (0.7.5)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (3.0.43)
Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (2.16.1)
Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (0.2.0)
Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (0.1.7)
Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython>=4.0.0->ipywidgets->bnlearn) (4.9.0)
Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from patsy>=0.5.6->statsmodels->bnlearn) (1.16.0)
Requirement already satisfied: notebook>=4.4.1 in /usr/local/lib/python3.10/dist-packages (from widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (6.5.5)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->datazets->bnlearn) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->datazets->bnlearn) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->datazets->bnlearn) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->datazets->bnlearn) (2024.2.2)
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (3.14.0)
Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (4.11.0)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (1.12)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (3.1.4)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (8.9.2.26)
Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.3.1)
Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (11.0.2.54)
Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (10.3.2.106)
Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (11.4.5.107)
Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.0.106)
Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (2.20.5)
Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (12.1.105)
Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch->pgmpy>=0.1.18->bnlearn) (2.3.0)
Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch->pgmpy>=0.1.18->bnlearn) (12.5.40)
Requirement already satisfied: parso<0.9.0,>=0.8.3 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets->bnlearn) (0.8.4)
Requirement already satisfied: pyzmq<25,>=17 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (24.0.1)
Requirement already satisfied: argon2-cffi in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (23.1.0)
Requirement already satisfied: jupyter-core>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (5.7.2)
Requirement already satisfied: nbformat in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (5.10.4)
Requirement already satisfied: nbconvert>=5 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (6.5.4)
Requirement already satisfied: nest-asyncio>=1.5 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.6.0)
Requirement already satisfied: Send2Trash>=1.8.0 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.8.3)
Requirement already satisfied: terminado>=0.8.3 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.18.1)
Requirement already satisfied: prometheus-client in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.20.0)
Requirement already satisfied: nbclassic>=0.4.7 in /usr/local/lib/python3.10/dist-packages (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.0.0)
Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets->bnlearn) (0.7.0)
Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets->bnlearn) (0.2.13)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->pgmpy>=0.1.18->bnlearn) (2.1.5)
Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch->pgmpy>=0.1.18->bnlearn) (1.3.0)
Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.10/dist-packages (from jupyter-core>=4.6.1->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (4.2.2)
Requirement already satisfied: jupyter-server>=1.8 in /usr/local/lib/python3.10/dist-packages (from nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.24.0)
Requirement already satisfied: notebook-shim>=0.2.3 in /usr/local/lib/python3.10/dist-packages (from nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.2.4)
Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (4.9.4)
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (4.12.3)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (6.1.0)
Requirement already satisfied: defusedxml in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.7.1)
Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.4)
Requirement already satisfied: jupyterlab-pygments in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.3.0)
Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.8.4)
Requirement already satisfied: nbclient>=0.5.0 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.10.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.5.1)
Requirement already satisfied: tinycss2 in /usr/local/lib/python3.10/dist-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.3.0)
Requirement already satisfied: fastjsonschema>=2.15 in /usr/local/lib/python3.10/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (2.19.1)
Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.10/dist-packages (from nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (4.19.2)
Requirement already satisfied: argon2-cffi-bindings in /usr/local/lib/python3.10/dist-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (21.2.0)
Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (23.2.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (2023.12.1)
Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.35.1)
Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.18.1)
Requirement already satisfied: anyio<4,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (3.7.1)
Requirement already satisfied: websocket-client in /usr/local/lib/python3.10/dist-packages (from jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.8.0)
Requirement already satisfied: cffi>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.16.0)
Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (2.5)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (0.5.1)
Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.1.0->jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.3.1)
Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.1.0->jupyter-server>=1.8->nbclassic>=0.4.7->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (1.2.1)
Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets->bnlearn) (2.22)
Installing collected packages: bnlearn
  Attempting uninstall: bnlearn
    Found existing installation: bnlearn 0.8.7
    Uninstalling bnlearn-0.8.7:
      Successfully uninstalled bnlearn-0.8.7
Successfully installed bnlearn-0.8.8
In [ ]:
# Importeer de package
import bnlearn as bn
print(bn.__version__)
0.8.8
In [ ]:
import bnlearn as bn
from datazets import datazets

df = datazets.get(url=r'https://raw.githubusercontent.com/washingtonpost/data-police-shootings/master/v2/fatal-police-shootings-data.csv', overwrite=True)

del df['id']
del df['name']
del df['county']
del df['state']
del df['date']
del df['agency_ids']
del df['latitude']
del df['longitude']
del df['race_source']
[datazets] >INFO> Import dataset [fatal-police-shootings-data.csv]
[datazets] >INFO> Downloading [fatal-police-shootings-data.csv] dataset from github source..
In [ ]:
# Learn the DAG in data using Bayesian structure learning:
DAG = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic') # hillclimbsearch

# Constrained based
# DAG = bn.structure_learning.fit(df, methodtype='cs')

# Set class node (endpoint)
# df = df.dropna()
# DAG = bn.structure_learning.fit(df, methodtype='tan', class_node='threat_type')
[bnlearn] >Computing best DAG using [hc]
[bnlearn] >Set scoring type at [bic]
[bnlearn] >Compute structure scores for model comparison (higher is better).
In [ ]:
# print adjacency matrix
print(DAG['adjmat'])
target                      threat_type  flee_status  armed_with   city  \
source                                                                    
threat_type                       False        False       False  False   
flee_status                       False        False        True  False   
armed_with                         True        False       False  False   
city                              False        False       False  False   
location_precision                False        False       False  False   
age                               False        False       False  False   
gender                            False        False       False  False   
race                              False        False       False  False   
was_mental_illness_related        False        False       False  False   
body_camera                       False        False       False  False   

target                      location_precision    age  gender   race  \
source                                                                 
threat_type                              False  False   False  False   
flee_status                               True   True    True   True   
armed_with                               False  False   False  False   
city                                     False  False   False  False   
location_precision                       False  False   False  False   
age                                      False  False   False  False   
gender                                   False  False   False  False   
race                                     False  False   False  False   
was_mental_illness_related               False  False   False  False   
body_camera                              False  False   False  False   

target                      was_mental_illness_related  body_camera  
source                                                               
threat_type                                      False        False  
flee_status                                       True         True  
armed_with                                       False        False  
city                                             False        False  
location_precision                                True         True  
age                                              False        False  
gender                                           False        False  
race                                             False        False  
was_mental_illness_related                       False        False  
body_camera                                      False        False  
In [ ]:
# Plot
G = bn.plot(DAG, params_static={'figsize': (10,5), 'layout':'planar_layout'})
[bnlearn] >Set node properties.
[bnlearn] >Set edge properties.
[bnlearn] >Plot based on Bayesian model
No description has been provided for this image
In [ ]:
# Structure learning
DAG = bn.independence_test(DAG, df, prune=True)
[bnlearn] >Compute edge strength with [chi_square]
In [ ]:
# Plot
G = bn.plot(DAG)
[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] >Set edge properties.
[bnlearn] >Plot based on Bayesian model
No description has been provided for this image
In [ ]:
# Parameter learning
model = bn.parameter_learning.fit(DAG, df)
[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 flee_status:
+--------------------+-----------+
| flee_status(car)   | 0.18993   |
+--------------------+-----------+
| flee_status(foot)  | 0.16165   |
+--------------------+-----------+
| flee_status(not)   | 0.582967  |
+--------------------+-----------+
| flee_status(other) | 0.0654526 |
+--------------------+-----------+
[bnlearn] >CPD of armed_with:
+---------------------------------------+-----+----------------------+
| flee_status                           | ... | flee_status(other)   |
+---------------------------------------+-----+----------------------+
| armed_with(blunt_object)              | ... | 0.02717991169977924  |
+---------------------------------------+-----+----------------------+
| armed_with(blunt_object;blunt_object) | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(blunt_object;knife)        | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(gun)                       | ... | 0.42453090507726265  |
+---------------------------------------+-----+----------------------+
| armed_with(gun;knife)                 | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(gun;vehicle)               | ... | 0.03380242825607063  |
+---------------------------------------+-----+----------------------+
| armed_with(knife)                     | ... | 0.04539183222958056  |
+---------------------------------------+-----+----------------------+
| armed_with(knife;blunt_object)        | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(knife;knife)               | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(knife;vehicle)             | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(other)                     | ... | 0.025524282560706397 |
+---------------------------------------+-----+----------------------+
| armed_with(other;blunt_object;knife)  | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(other;gun)                 | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(other;knife)               | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(replica)                   | ... | 0.023868653421633548 |
+---------------------------------------+-----+----------------------+
| armed_with(replica;knife)             | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(replica;vehicle)           | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(unarmed)                   | ... | 0.06360375275938188  |
+---------------------------------------+-----+----------------------+
| armed_with(undetermined)              | ... | 0.04704746136865341  |
+---------------------------------------+-----+----------------------+
| armed_with(unknown)                   | ... | 0.02883554083885209  |
+---------------------------------------+-----+----------------------+
| armed_with(vehicle)                   | ... | 0.037113686534216324 |
+---------------------------------------+-----+----------------------+
| armed_with(vehicle;gun)               | ... | 0.018901766004415007 |
+---------------------------------------+-----+----------------------+
| armed_with(vehicle;knife)             | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
| armed_with(vehicle;knife;other)       | ... | 0.017246136865342158 |
+---------------------------------------+-----+----------------------+
[bnlearn] >CPD of location_precision:
+-----------------------------------+-----+----------------------+---------------------+
| flee_status                       | ... | flee_status(not)     | flee_status(other)  |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(address)       | ... | 0.05682919874536642  | 0.08326967150496563 |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(block)         | ... | 0.06960364984317081  | 0.09039979628214923 |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(intersection)  | ... | 0.03187909894496722  | 0.09574738986503692 |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(not_available) | ... | 0.8143142286854863   | 0.5271199388846448  |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(poi_large)     | ... | 0.008525805531793558 | 0.06900942195059842 |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(poi_small)     | ... | 0.007927003136583977 | 0.06722689075630252 |
+-----------------------------------+-----+----------------------+---------------------+
| location_precision(road)          | ... | 0.01092101511263188  | 0.06722689075630252 |
+-----------------------------------+-----+----------------------+---------------------+
[bnlearn] >CPD of age:
+-------------+-----------------------+-----+-----------------------+
| flee_status | flee_status(car)      | ... | flee_status(other)    |
+-------------+-----------------------+-----+-----------------------+
| age(2.0)    | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(4.0)    | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(6.0)    | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(8.0)    | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(12.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(13.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(14.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(15.0)   | 0.004696394016864645  | ... | 0.008367359253374092  |
+-------------+-----------------------+-----+-----------------------+
| age(16.0)   | 0.007627226373253858  | ... | 0.008367359253374092  |
+-------------+-----------------------+-----+-----------------------+
| age(17.0)   | 0.011144225200920914  | ... | 0.01838405374419212   |
+-------------+-----------------------+-----+-----------------------+
| age(18.0)   | 0.013488891086032287  | ... | 0.016714604662389113  |
+-------------+-----------------------+-----+-----------------------+
| age(19.0)   | 0.027556886396700515  | ... | 0.016714604662389113  |
+-------------+-----------------------+-----+-----------------------+
| age(20.0)   | 0.017592056384977187  | ... | 0.016714604662389113  |
+-------------+-----------------------+-----+-----------------------+
| age(21.0)   | 0.021109055212644244  | ... | 0.016714604662389113  |
+-------------+-----------------------+-----+-----------------------+
| age(22.0)   | 0.017592056384977187  | ... | 0.02339240098960113   |
+-------------+-----------------------+-----+-----------------------+
| age(23.0)   | 0.0164197234424215    | ... | 0.02339240098960113   |
+-------------+-----------------------+-----+-----------------------+
| age(24.0)   | 0.029315385810534042  | ... | 0.016714604662389113  |
+-------------+-----------------------+-----+-----------------------+
| age(25.0)   | 0.03166005169564541   | ... | 0.021722951907798126  |
+-------------+-----------------------+-----+-----------------------+
| age(26.0)   | 0.027556886396700515  | ... | 0.025061850071404133  |
+-------------+-----------------------+-----+-----------------------+
| age(27.0)   | 0.0328323846382011    | ... | 0.02840074823501014   |
+-------------+-----------------------+-----+-----------------------+
| age(28.0)   | 0.03459088405203463   | ... | 0.02840074823501014   |
+-------------+-----------------------+-----+-----------------------+
| age(29.0)   | 0.025212220511589143  | ... | 0.025061850071404133  |
+-------------+-----------------------+-----+-----------------------+
| age(30.0)   | 0.03869404935097953   | ... | 0.02339240098960113   |
+-------------+-----------------------+-----+-----------------------+
| age(31.0)   | 0.028143052867978358  | ... | 0.035078544562222154  |
+-------------+-----------------------+-----+-----------------------+
| age(32.0)   | 0.03459088405203463   | ... | 0.026731299153207135  |
+-------------+-----------------------+-----+-----------------------+
| age(33.0)   | 0.03166005169564541   | ... | 0.026731299153207135  |
+-------------+-----------------------+-----+-----------------------+
| age(34.0)   | 0.029901552281811884  | ... | 0.02339240098960113   |
+-------------+-----------------------+-----+-----------------------+
| age(35.0)   | 0.0287292193392562    | ... | 0.035078544562222154  |
+-------------+-----------------------+-----+-----------------------+
| age(36.0)   | 0.03166005169564541   | ... | 0.04008689180763117   |
+-------------+-----------------------+-----+-----------------------+
| age(37.0)   | 0.026384553454144827  | ... | 0.025061850071404133  |
+-------------+-----------------------+-----+-----------------------+
| age(38.0)   | 0.036349383465868156  | ... | 0.01504515558058611   |
+-------------+-----------------------+-----+-----------------------+
| age(39.0)   | 0.03224621816692325   | ... | 0.025061850071404133  |
+-------------+-----------------------+-----+-----------------------+
| age(40.0)   | 0.028143052867978358  | ... | 0.013375706498783104  |
+-------------+-----------------------+-----+-----------------------+
| age(41.0)   | 0.029315385810534042  | ... | 0.01504515558058611   |
+-------------+-----------------------+-----+-----------------------+
| age(42.0)   | 0.01700588991369934   | ... | 0.02339240098960113   |
+-------------+-----------------------+-----+-----------------------+
| age(43.0)   | 0.019936722270088556  | ... | 0.013375706498783104  |
+-------------+-----------------------+-----+-----------------------+
| age(44.0)   | 0.015833556971143657  | ... | 0.01504515558058611   |
+-------------+-----------------------+-----+-----------------------+
| age(45.0)   | 0.019936722270088556  | ... | 0.013375706498783104  |
+-------------+-----------------------+-----+-----------------------+
| age(46.0)   | 0.014661224028587971  | ... | 0.010036808335177097  |
+-------------+-----------------------+-----+-----------------------+
| age(47.0)   | 0.014661224028587971  | ... | 0.013375706498783104  |
+-------------+-----------------------+-----+-----------------------+
| age(48.0)   | 0.015247390499865815  | ... | 0.011706257416980101  |
+-------------+-----------------------+-----+-----------------------+
| age(49.0)   | 0.009971892258365228  | ... | 0.011706257416980101  |
+-------------+-----------------------+-----+-----------------------+
| age(50.0)   | 0.010558058729643072  | ... | 0.01504515558058611   |
+-------------+-----------------------+-----+-----------------------+
| age(51.0)   | 0.015833556971143657  | ... | 0.011706257416980101  |
+-------------+-----------------------+-----+-----------------------+
| age(52.0)   | 0.006454893430698173  | ... | 0.011706257416980101  |
+-------------+-----------------------+-----+-----------------------+
| age(53.0)   | 0.009971892258365228  | ... | 0.010036808335177097  |
+-------------+-----------------------+-----+-----------------------+
| age(54.0)   | 0.007627226373253858  | ... | 0.011706257416980101  |
+-------------+-----------------------+-----+-----------------------+
| age(55.0)   | 0.007627226373253858  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(56.0)   | 0.004696394016864645  | ... | 0.0066979101715710886 |
+-------------+-----------------------+-----+-----------------------+
| age(57.0)   | 0.007041059901976016  | ... | 0.0066979101715710886 |
+-------------+-----------------------+-----+-----------------------+
| age(58.0)   | 0.007041059901976016  | ... | 0.008367359253374092  |
+-------------+-----------------------+-----+-----------------------+
| age(59.0)   | 0.00586872695942033   | ... | 0.010036808335177097  |
+-------------+-----------------------+-----+-----------------------+
| age(60.0)   | 0.006454893430698173  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(61.0)   | 0.004110227545586802  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(62.0)   | 0.005282560488142487  | ... | 0.0066979101715710886 |
+-------------+-----------------------+-----+-----------------------+
| age(63.0)   | 0.004696394016864645  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(64.0)   | 0.004696394016864645  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(65.0)   | 0.005282560488142487  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(66.0)   | 0.002937894603031116  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(67.0)   | 0.002937894603031116  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(68.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(69.0)   | 0.002937894603031116  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(70.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(71.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(72.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(73.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(74.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(75.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(76.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(77.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(78.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(79.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(80.0)   | 0.002351728131753273  | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(81.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(82.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(83.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(84.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(85.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(86.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(88.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(89.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(91.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
| age(92.0)   | 0.0017655616604754303 | ... | 0.005028461089768085  |
+-------------+-----------------------+-----+-----------------------+
[bnlearn] >CPD of gender:
+--------------------+---------------------+-----+----------------------+---------------------+
| flee_status        | flee_status(car)    | ... | flee_status(not)     | flee_status(other)  |
+--------------------+---------------------+-----+----------------------+---------------------+
| gender(female)     | 0.09233088373845864 | ... | 0.06351541904236406  | 0.15111838516093834 |
+--------------------+---------------------+-----+----------------------+---------------------+
| gender(male)       | 0.8605615225174299  | ... | 0.9209735761142788   | 0.7124931805782869  |
+--------------------+---------------------+-----+----------------------+---------------------+
| gender(non-binary) | 0.04710759374411155 | ... | 0.015511004843357245 | 0.13638843426077468 |
+--------------------+---------------------+-----+----------------------+---------------------+
[bnlearn] >CPD of race:
+-------------+----------------------+-----+---------------------+
| flee_status | flee_status(car)     | ... | flee_status(other)  |
+-------------+----------------------+-----+---------------------+
| race(A)     | 0.026687865538583835 | ... | 0.04292929292929292 |
+-------------+----------------------+-----+---------------------+
| race(B)     | 0.2384305263755607   | ... | 0.19049873737373732 |
+-------------+----------------------+-----+---------------------+
| race(B;H)   | 0.01419567315882119  | ... | 0.0394570707070707  |
+-------------+----------------------+-----+---------------------+
| race(H)     | 0.16972346828686616  | ... | 0.18355429292929287 |
+-------------+----------------------+-----+---------------------+
| race(N)     | 0.023564817443643173 | ... | 0.04813762626262625 |
+-------------+----------------------+-----+---------------------+
| race(N;H)   | 0.01419567315882119  | ... | 0.04119318181818181 |
+-------------+----------------------+-----+---------------------+
| race(O)     | 0.016069502015785588 | ... | 0.04292929292929292 |
+-------------+----------------------+-----+---------------------+
| race(W)     | 0.4545454545454545   | ... | 0.2929292929292929  |
+-------------+----------------------+-----+---------------------+
| race(W;B)   | 0.01419567315882119  | ... | 0.0394570707070707  |
+-------------+----------------------+-----+---------------------+
| race(W;B;N) | 0.01419567315882119  | ... | 0.0394570707070707  |
+-------------+----------------------+-----+---------------------+
| race(W;H)   | 0.01419567315882119  | ... | 0.0394570707070707  |
+-------------+----------------------+-----+---------------------+
[bnlearn] >CPD of was_mental_illness_related:
+-----------------------------------+-----+--------------------------+
| flee_status                       | ... | flee_status(other)       |
+-----------------------------------+-----+--------------------------+
| location_precision                | ... | location_precision(road) |
+-----------------------------------+-----+--------------------------+
| was_mental_illness_related(False) | ... | 0.5265151515151515       |
+-----------------------------------+-----+--------------------------+
| was_mental_illness_related(True)  | ... | 0.4734848484848485       |
+-----------------------------------+-----+--------------------------+
[bnlearn] >CPD of body_camera:
+--------------------+-----+--------------------------+
| flee_status        | ... | flee_status(other)       |
+--------------------+-----+--------------------------+
| location_precision | ... | location_precision(road) |
+--------------------+-----+--------------------------+
| body_camera(False) | ... | 0.5265151515151515       |
+--------------------+-----+--------------------------+
| body_camera(True)  | ... | 0.4734848484848485       |
+--------------------+-----+--------------------------+
[bnlearn] >CPD of threat_type:
+---------------------------+-----+---------------------------------+
| armed_with                | ... | armed_with(vehicle;knife;other) |
+---------------------------+-----+---------------------------------+
| threat_type(accident)     | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
| threat_type(attack)       | ... | 0.14550781249999997             |
+---------------------------+-----+---------------------------------+
| threat_type(flee)         | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
| threat_type(move)         | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
| threat_type(point)        | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
| threat_type(shoot)        | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
| threat_type(threat)       | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
| threat_type(undetermined) | ... | 0.12207031249999999             |
+---------------------------+-----+---------------------------------+
[bnlearn] >Compute structure scores for model comparison (higher is better).
[bnlearn] >WARNING> Skipping computing structure score for [k2].

Inferences on the dataset.¶

Making inferences requires the Bayesian network to have two main components: A Directed Acyclic Graph (DAG) that describes the structure of the data and Conditional Probability Tables (CPT) that describe the statistical relationship between each node and its parents. At this point you have the dataset, you computed the DAG using structure learning and estimated the CPTs using parameter learning. You can now make inferences!

With inferences, we marginalize variables in a procedure that is called variable elimination. Variable elimination is an exact inference algorithm. It can also be used to figure out the state of the network that has maximum probability by simply exchanging the sums by max functions. Its downside is that for large BNs it might be computationally intractable. Approximate inference algorithms such as Gibbs sampling or rejection sampling might be used in these cases [7].

With bnlearn we can make inferences as follow:

In [ ]:
# Make inference
q1 = bn.inference.fit(model, variables=['threat_type'], evidence={'flee_status': 'foot'})
q1 = bn.inference.fit(model, variables=['gender'], evidence={'threat_type': 'accident', 'armed_with': 'gun'})

# No connection in the DAG, thus the evidence should not influence the outcome
q1 = bn.inference.fit(model, variables=['body_camera'], evidence={'gender': 'male'})
q1 = bn.inference.fit(model, variables=['body_camera'], evidence={'armed_with': 'gun'})

print(q1)
print(q1.df)
# bn.print_CPD(model)
[bnlearn] >Variable Elimination.
[bnlearn] >Warning: variable(s) [None] does not exists in DAG.
[bnlearn] >Data is stored in [query.df]
+----+---------------+-----------+
|    | threat_type   |         p |
+====+===============+===========+
|  0 | accident      | 0.023455  |
+----+---------------+-----------+
|  1 | attack        | 0.117813  |
+----+---------------+-----------+
|  2 | flee          | 0.0341373 |
+----+---------------+-----------+
|  3 | move          | 0.060333  |
+----+---------------+-----------+
|  4 | point         | 0.191787  |
+----+---------------+-----------+
|  5 | shoot         | 0.289917  |
+----+---------------+-----------+
|  6 | threat        | 0.231409  |
+----+---------------+-----------+
|  7 | undetermined  | 0.0511491 |
+----+---------------+-----------+
[bnlearn] >Variable Elimination.
[bnlearn] >Warning: variable(s) [None] does not exists in DAG.
[bnlearn] >Data is stored in [query.df]
+----+------------+-----------+
|    | gender     |         p |
+====+============+===========+
|  0 | female     | 0.0743379 |
+----+------------+-----------+
|  1 | male       | 0.891501  |
+----+------------+-----------+
|  2 | non-binary | 0.034161  |
+----+------------+-----------+
[bnlearn] >Variable Elimination.
[bnlearn] >Warning: variable(s) [None] does not exists in DAG.
[bnlearn] >Data is stored in [query.df]
+----+---------------+----------+
|    | body_camera   |        p |
+====+===============+==========+
|  0 | False         | 0.792266 |
+----+---------------+----------+
|  1 | True          | 0.207734 |
+----+---------------+----------+
[bnlearn] >Variable Elimination.
[bnlearn] >Warning: variable(s) [None] does not exists in DAG.
[bnlearn] >Data is stored in [query.df]
+----+---------------+---------+
|    | body_camera   |       p |
+====+===============+=========+
|  0 | False         | 0.79138 |
+----+---------------+---------+
|  1 | True          | 0.20862 |
+----+---------------+---------+
+--------------------+--------------------+
| body_camera        |   phi(body_camera) |
+====================+====================+
| body_camera(False) |             0.7914 |
+--------------------+--------------------+
| body_camera(True)  |             0.2086 |
+--------------------+--------------------+
   body_camera        p
0        False  0.79138
1         True  0.20862

How do I know my causal model is right?¶

If you solely used data to compute the causal diagram, it is hard to fully verify the validity and completeness of your causal diagram. However, some solutions can help to get more trust in the causal diagram. For example, it may be possible to empirically test certain conditional independence or dependence relationships between sets of variables. If they are not in the data, it is an indication of the correctness of the causal model [8]. Alternatively, prior expert knowledge can be added, such as a DAG or CPTs, to get more trust in the model when making inferences.

A weakness of Bayesian networks is that finding the optimum DAG is computationally expensive since an exhaustive search over all the possible structures must be performed. The limit of nodes for exhaustive search can already be around 15 nodes but also depends on the number of states. If you have more nodes, alternative methods with a scoring function and search algorithm are required. Nevertheless, to deal with problems with hundreds or maybe even thousands of variables, a different approach, such as tree-based or constraint-based approaches is necessary with the use of black/whitelisting of variables. Such an approach first determines the order and then finds the optimal BN structure for that ordering. This implies working on the search space of the possible orderings, which is convenient as it is smaller than the space of network structures.


Support¶

This library is for free but it runs on coffee! :)

You can support in various ways, have a look at the sponser page. Report bugs, issues and feature extensions at github page.

No description has been provided for this image Follow me on Medium