EXP13
EXP13
Write a Python program to construct a Bayesian network considering medical data. Use this
model to demonstrate the diagnosis of heart patients using standard Heart Disease Data Set
Bayesian Network can be used for building models from data and experts
opinions, and it consists of two parts:
Note: The Bayesian network graph does not contain any cyclic graph. Hence, it is known
as a directed acyclic graph or DAG.
o Causal Component
o Actual numbers
P[x1, x2, x3,....., xn], it can be written as the following way in terms of the
joint probability distribution.
In general for each variable Xi, we can write the equation as:
Problem:
Solution:
o The Bayesian network for the above problem is given below. The
network structure is showing that burglary and earthquake is the
parent node of the alarm and directly affecting the probability of
alarm's going off, but David and Sophia's calls depend on alarm
probability.
o The network is representing that our assumptions do not directly
perceive the burglary and also do not notice the minor earthquake,
and they also not confer before calling.
o The conditional distributions for each node are given as conditional
probabilities table or CPT.
o Each row in the CPT must be sum to 1 because all the entries in the
table represent an exhaustive set of cases for the variable.
o In CPT, a boolean variable with k boolean parents contains
2K probabilities. Hence, if there are two parents, then CPT will
contain 4 probability values
o Burglary (B)
o Earthquake(E)
o Alarm(A)
o David Calls(D)
o Sophia calls(S)
From the formula of joint distribution, we can write the problem statement
in the form of probability distribution:
= 0.00068045.
There are two ways to understand the semantics of the Bayesian network,
which is given below:
"""EXp13.ipynb
import numpy as np
import pandas as pd
import csv
heartDisease = pd.read_csv('/content/data7_heart.csv')
heartDisease
heartDisease = heartDisease.replace('?',np.nan)
heartDisease
print(heartDisease.head())
print(heartDisease.dtypes)
model = BayesianModel([('age','heartdisease'),('sex','heartdisease'),(
'exang','heartdisease'),('cp','heartdisease'),('heartdisease',
'restecg'),('heartdisease','chol')])
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
HeartDiseasetest_infer = VariableElimination(model)
q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'r
estecg':1})
print(q1)
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'c
p':2})
print(q2)