BAYESIAN NETWORK

 

Ex No 4                                          Implement Bayesian Networks


CLICK HERE >> PROGRAM

Aim

Write a python program to implement the Bayesian Network .

Algorithm

1. Start the program.

2. Install the necessary libraries including pgmpy, numpy, csv, pandas, and google.colab.

3. Import the required modules including BayesianModel, MaximumLikelihoodEstimator,

VariableElimination, and drive from pgmpy, pgmpy.estimators, pgmpy.inference, and google.colab

respectively.

4. Mount the Google Drive to access the dataset.

5. Load the Cleveland Heart Disease dataset using pandas and replace any missing values with NaN.

6. Display a few examples from the dataset using the head() method.

7. Define the structure of the Bayesian Network using BayesianModel from pgmpy.

8. Learn the Conditional Probability Distributions (CPDs) using Maximum Likelihood Estimators.

9. Perform inferencing using VariableElimination from pgmpy.

10.Compute the probability of heart disease given age and cholesterol.

11. Print the results of inferencing to the console.

Output

age sex cp trestbps chol fbs restecg thalach exang oldpeak slope \

0 63 1 1 145 233 1 2 150 0 2.3 3

1 67 1 4 160 286 0 2 108 1 1.5 2

2 67 1 4 120 229 0 2 129 1 2.6 2

3 37 1 3 130 250 0 0 187 0 3.5 3

4 41 0 2 130 204 0 2 172 0 1.4 1

ca thal heartdisease

0 0 6 0

1 3 3 2

2 2 7 1

3 0 3 0

4 0 3 0

Learning CPD using Maximum likelihood estimators

Inferencing with Bayesian Network:

1. Probability of HeartDisease given Age=30

+-----------------+---------------------+

| heartdisease | phi(heartdisease) |

+=================+=====================+

| heartdisease(0) | 0.5155 |

+-----------------+---------------------+

| heartdisease(1) | 0.2755 |

+-----------------+---------------------+

| heartdisease(2) | 0.0691 |

+-----------------+---------------------+

| heartdisease(3) | 0.0957 |


+-----------------+---------------------+

| heartdisease(4) | 0.0443 |

+-----------------+---------------------+


2. Probability of HeartDisease given cholesterol= 233

+-----------------+---------------------+

| heartdisease | phi(heartdisease) |

+=================+=====================+

| heartdisease(0) | 0.5451 |

+-----------------+---------------------+

| heartdisease(1) | 0.2358 |

+-----------------+---------------------+

| heartdisease(2) | 0.1085 |

+-----------------+---------------------+

| heartdisease(3) | 0.1105 |

+-----------------+---------------------+

| heartdisease(4) | 0.0000 |

Result

Thus the implentation of Bayesian Network has been executed successfully python program.

No comments:

Post a Comment