In this example we will try to create the cancer (http://www.bnlearn.com/bnrepository/#cancer) bayesian network using pgmpy and do some simple queries on the network.

In pgmpy, the general flow of defining a network is to first define the network and then add the parameters to it.

In [1]:
from IPython.display import Image
In [2]:
Image("71PsL.png")
Out[2]:
In [3]:
# Starting with defining the network structure
from pgmpy.models import BayesianModel
#TODO
cancer_model = BayesianModel(None)
In [4]:
# Now defining the parameters.
from pgmpy.factors.discrete import TabularCPD

cpd_poll = None
cpd_smoke = None
cpd_cancer = None
cpd_xray = None
cpd_dysp = None
In [ ]:
# Associating the parameters with the model structure.
cancer_model.add_cpds(cpd_poll, cpd_smoke, cpd_cancer, cpd_xray, cpd_dysp)

# Checking if the cpds are valid for the model.
cancer_model.check_model()
In [ ]:
# Doing some simple queries on the network
#Check whether there is an active trail between pollution and smoker when cancer is observed 
In [14]:
#Find the probablity of Dyspnoea given that XRay is T, Smoker is F