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.
from IPython.display import Image
Image("71PsL.png")
# Starting with defining the network structure
from pgmpy.models import BayesianModel
#TODO
cancer_model = BayesianModel(None)
# 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
# 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()
# Doing some simple queries on the network
#Check whether there is an active trail between pollution and smoker when cancer is observed
#Find the probablity of Dyspnoea given that XRay is T, Smoker is F