0% found this document useful (0 votes)
9 views5 pages

Bayesian Network for Heart Disease Analysis

Demonstrate and Analyse the results sets obtained from the Bayesian belief network principle.

Uploaded by

Deepak D
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Bayesian Network for Heart Disease Analysis

Demonstrate and Analyse the results sets obtained from the Bayesian belief network principle.

Uploaded by

Deepak D
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MACHINE LEARNING LABORATORY

6 Demonstrate and Analyse the results sets obtained from Bayesian belief
Aim network Principle.
Write a program to construct a Bayesian network considering medical data.
Program Use this model to demonstrate the diagnosis of heart patients using standard
Heart Disease Data Set. You can use Python ML library classes/API.

CONCEPT –

A Bayesian network is a directed acyclic graph in which each edge corresponds to a conditional
dependency, and each node corresponds to a unique random variable.

Bayesian network consists of two major parts: a directed acyclic graph and a set of conditional
probability distributions
• The directed acyclic graph is a set of random variables represented by nodes.
• The conditional probability distribution of a node (random variable) is defined for every
possible outcome of the preceding causal node(s).

For illustration, consider the following example. Suppose we attempt to turn on our computer,
but the computer does not start (observation/evidence). We would like to know which of the
possible causes of computer failure is more likely. In this simplified illustration, we assume
only two possible causes of this misfortune: electricity failure and computer malfunction.
The corresponding directed acyclic graph is depicted in below figure.

Fig: Directed acyclic graph representing two independent possible causes of a computer failure.

The goal is to calculate the posterior conditional probability distribution of each of the possible
unobserved causes given the observed evidence, i.e. P [Cause | Evidence].

Deepak D, Assistant Professor, Dept. of AI & ML, Canara Engineering College, Mangaluru 1
MACHINE LEARNING LABORATORY

Training Instances: (The below data is saved as [Link] file)

Heart Disease Databases


The Cleveland database contains 76 attributes, but all published experiments refer to using a
subset of 14 of them. In particular, the Cleveland database is the only one that has been used
by ML researchers to this date. The "Heartdisease" field refers to the presence of heart disease
in the patient. It is integer valued from 0 (no presence) to 4.

Database 0 1 2 3 4 Total
Cleveland 165 55 36 35 13 303

Some instance from the dataset:


age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal Heartdisease
63 1 1 145 233 1 2 150 0 2.3 3 0 6 0
67 1 4 160 286 0 2 108 1 1.5 2 3 3 2
67 1 4 120 229 0 2 129 1 2.6 2 2 7 1
41 0 2 130 204 0 2 172 0 1.4 1 0 3 0
62 0 4 140 268 0 2 160 0 3.6 3 2 3 3
60 1 4 130 206 0 2 132 1 2.4 2 2 7 4

Attribute Information
1. age: age in years
2. sex: sex (1 = male; 0 = female)
3. cp: chest pain type
• Value 1: typical angina
• Value 2: atypical angina
• Value 3: non-anginal pain
• Value 4: asymptomatic
4. trestbps: resting blood pressure (in mm Hg on admission to the hospital)
5. chol: serum cholestoral in mg/dl
6. fbs: (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
7. restecg: resting electrocardiographic results
• Value 0: normal
• Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation
or depression of > 0.05 mV)
• Value 2: showing probable or definite left ventricular hypertrophy by Estes'
criteria
8. thalach: maximum heart rate achieved
9. exang: exercise induced angina (1 = yes; 0 = no)
10. oldpeak = ST depression induced by exercise relative to rest

Deepak D, Assistant Professor, Dept. of AI & ML, Canara Engineering College, Mangaluru 2
MACHINE LEARNING LABORATORY

11. slope: the slope of the peak exercise ST segment


• Value 1: upsloping
• Value 2: flat
• Value 3: downsloping
12. ca: number of major vessels (0-3) colored by fluoroscopy
• 0: No major vessels visible
• 1: One major vessel visible
• 2: Two major vessels visible
• 3: Three major vessels visible
13. thal: 3 = normal; 6 = fixed defect; 7 = reversable defect
14. Heartdisease: It is integer valued from 0 (no presence) to 4.
• 0: No heart disease
• 1: Mild heart disease
• 2: Moderate heart disease
• 3: Severe heart disease
• 4: Very severe heart disease

Deepak D, Assistant Professor, Dept. of AI & ML, Canara Engineering College, Mangaluru 3
MACHINE LEARNING LABORATORY

Program:

import numpy as np
import pandas as pd
from [Link] import MaximumLikelihoodEstimator # Probabilistic Graphical Models
from [Link] import BayesianNetwork
from [Link] import VariableElimination

heartDisease = pd.read_csv("[Link]")
heartDisease = [Link]('?',[Link])

print('Sample instances from the dataset are given below')


print([Link]())

model= BayesianNetwork([('age','heartdisease'), ('sex','heartdisease'),


('exang','heartdisease'), ('cp','heartdisease'), ('heartdisease','restecg'),
('heartdisease','chol')])

print('\n Learning CPD using Maximum likelihood estimators')


[Link](heartDisease,estimator=MaximumLikelihoodEstimator)

print('\n Inferencing with Bayesian Network:')


HeartDiseasetest_infer = VariableElimination(model)

print('\n 1. Probability of HeartDisease given evidence= restecg')


q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)

print('\n 2. Probability of HeartDisease given evidence= cp ')


q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})
print(q2)

Deepak D, Assistant Professor, Dept. of AI & ML, Canara Engineering College, Mangaluru 4
MACHINE LEARNING LABORATORY

Output:

Sample instances from the dataset are given below

age sex cp trestbps chol ... oldpeak slope ca thal heartdisease


0 63 1 1 145 233 ... 2.3 3 0 6 0
1 67 1 4 160 286 ... 1.5 2 3 3 2
2 67 1 4 120 229 ... 2.6 2 2 7 1
3 37 1 3 130 250 ... 3.5 3 0 3 0
4 41 0 2 130 204 ... 1.4 1 0 3 0

[5 rows x 14 columns]

Learning CPD using Maximum likelihood estimators

Inferencing with Bayesian Network:

1. Probability of HeartDisease given evidence= restecg


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.1016 |
+-----------------+---------------------+
| heartdisease(1) | 0.0000 |
+-----------------+---------------------+
| heartdisease(2) | 0.2361 |
+-----------------+---------------------+
| heartdisease(3) | 0.2017 |
+-----------------+---------------------+
| heartdisease(4) | 0.4605 |
+-----------------+---------------------+

2. Probability of HeartDisease given evidence= cp


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.3742 |
+-----------------+---------------------+
| heartdisease(1) | 0.2018 |
+-----------------+---------------------+
| heartdisease(2) | 0.1375 |
+-----------------+---------------------+
| heartdisease(3) | 0.1541 |
+-----------------+---------------------+
| heartdisease(4) | 0.1323 |
+-----------------+---------------------+

Deepak D, Assistant Professor, Dept. of AI & ML, Canara Engineering College, Mangaluru 5

Common questions

Powered by AI

The pgmpy library provides significant advantages for implementing Bayesian Networks in heart disease diagnosis by offering tools for structure representation, model fitting, and probabilistic inference. It simplifies the construction of directed acyclic graphs and the estimation of parameters using data-driven approaches like Maximum Likelihood Estimation. pgmpy's inference techniques, such as Variable Elimination, are efficient for computing posterior distributions over network variables, thus making it a powerful resource for developing robust diagnostic models that can handle complex medical data interactions .

In a Bayesian Network for predicting heart disease, exercise-induced angina is an important marker that influences the probability predictions. Its presence (e.g., 'exang' = 1) is a critical factor within the network that may indicate a higher likelihood of heart disease due to the additional stress it represents on the heart. This variable, as a node in the network, adjusts the conditional probabilities, presenting a stronger likelihood of moderate to severe heart conditions when angina is exercise-induced, thereby enhancing the sensitivity of the network in diagnosing conditions correlated with physical exertion .

A Bayesian network models conditional dependencies among random variables using a directed acyclic graph (DAG). In medical diagnosis, each node represents a unique random variable, such as symptoms or possible diseases, and the edges represent conditional dependencies between these variables. For example, if we consider diagnosing heart disease, nodes could include factors like age, chest pain type, and cholesterol level. The edges define how these variables are conditionally dependent, based on observed evidence like test results. The goal is to compute the posterior probabilities of the possible unobserved conditions (diseases) given the evidence (symptoms).

In a Bayesian Network, misrepresenting an edge in the directed acyclic graph implies erroneous conditional dependencies among the variables. In medical data analysis, such an error could lead to incorrect inferences about the relationships between symptoms and diseases. For example, if an edge falsely suggests a direct dependency between unrelated symptoms, this might skew diagnostic probabilities and lead to incorrect or biased medical conclusions. It's crucial to accurately represent dependencies to ensure the network provides reliable diagnostic insights, reflecting true causal or correlational relationships .

Analyzing conditional probability distributions in Bayesian Networks enhances understanding and prediction of heart disease by explicitly modeling the interrelated probabilities between risk factors and the disease outcome. By doing so, it allows for more informed estimates of heart disease presence given multiple co-occurring symptoms and conditions. These distributions help quantify how particular variables, such as age or cholesterol levels, modify the risk under differing conditions. This enhances diagnostic precision and supports clinical decision-making through a structured probabilistic framework, accommodating uncertainty and variability inherent in medical assessments .

In the Bayesian Network model, 'thalach' (maximum heart rate achieved) and 'oldpeak' (ST depression induced by exercise) are crucial for assessing heart disease risk. 'Thalach' indicates how well the heart responds to exercise, where lower values could suggest cardiac issues. On the other hand, 'oldpeak' measures prognostic information about heart's performance under stress; higher values often imply more severe myocardial ischemia. Together, these attributes help in cross-validating conditions like angina and myocardial infarction, impacting the probability distributions used for disease prediction .

In the 'phi(heartdisease)' table inferred from Bayesian Networks, the coefficients represent the posterior probabilities of different severities of heart disease given evidence such as chest pain. For example, specific probabilities are assigned to each level of heart disease severity (0 to 4) when chest pain is observed. Higher probabilities in this table for certain severities indicate a strong conditional dependency between the presence of that type of chest pain and the likelihood of having a corresponding level of heart disease. This allows medical practitioners to assess risks accurately and tailor diagnostic processes accordingly .

Replacing missing values with NaN in medical datasets is a preprocessing step that facilitates more robust handling of data. NaN acts as a placeholder that impedes erroneous data imputation or aggregation, enabling cleaner data manipulation and analysis. Using NaN allows for the application of advanced cleaning methods such as imputation or removal of incomplete entries based on more sophisticated criteria. It prevents misleading results during model training by acknowledging the uncertainty or absence of data rather than arbitrarily assuming values .

The Maximum Likelihood Estimator (MLE) is used in fitting a Bayesian Network model to estimate its parameters (conditional probability distributions) from observed data. In the context of medical data such as heart disease, MLE helps in accurately capturing the dependencies and conditional probabilities among variables like age, sex, and indicators of heart disease. This statistical approach enhances the model's predictive accuracy and reliability in reflecting real-world medical phenomena, allowing more accurate diagnosis .

The Cleveland Heart Disease Database is specifically used in machine learning research for diagnosing heart conditions because it has been extensively validated and contains a comprehensive set of 76 attributes relevant to heart disease. Among these, a subset of 14 attributes is typically used for experiments, ensuring consistent benchmarks and comparability across studies. This focus on a smaller but meaningful subset allows researchers to build models that can generalize well to real-world conditions. Moreover, its history of use means there is a wealth of baseline data to compare new methodologies against .

You might also like