0% found this document useful (0 votes)
27 views

Bayesian Network Notes

The document describes a Bayesian network program that analyzes a heart disease dataset from Kaggle. It loads the dataset, defines the structure of the Bayesian network with heart disease as the target variable, learns the conditional probability distributions using maximum likelihood estimation, and performs inference on the network to calculate the probability of heart disease given evidence about restecg and cp values.

Uploaded by

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

Bayesian Network Notes

The document describes a Bayesian network program that analyzes a heart disease dataset from Kaggle. It loads the dataset, defines the structure of the Bayesian network with heart disease as the target variable, learns the conditional probability distributions using maximum likelihood estimation, and performs inference on the network to calculate the probability of heart disease given evidence about restecg and cp values.

Uploaded by

bravejaya2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bayesian network

Program
import numpy as np
import pandas as pd
import csv
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination
heartDisease = pd.read_csv(r'C:\heart.csv')
heartDisease = heartDisease.replace('?',np.nan)
print('Sample instances from the dataset are given below')
print(heartDisease.head())
print('\n Attributes and datatypes')
print(heartDisease.dtypes)
model=BayesianNetwork([('age','heartdisease'),
('sex','heartdisease'),('exang','heartdisease'),('cp','heartdisease'),
('heartdisease','restecg'),('heartdisease','chol')])
print('\nLearning CPD using Maximum likelihood estimators')
model.fit(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'],evidenc
e={'restecg':1})
print(q1)
print('\n 2. Probability of HeartDisease given evidence= cp ')
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidenc
e={'cp':2})
print(q2)

dataset

heart disease from Kaggle

output

========================================================
========== RESTART: C:/MLJP/bayes network.py
========================================================
==========
Sample instances from the dataset are given below
age sex cp trestbps chol ... oldpeak slope ca thal
heartdisease
0 52 1 0 125 212 ... 1.0 2 2 3 0
1 53 1 0 140 203 ... 3.1 0 0 3 0
2 70 1 0 145 174 ... 2.6 0 0 3 0
3 61 1 0 148 203 ... 0.0 2 1 3 0
4 62 0 0 138 294 ... 1.9 1 3 2 0
[5 rows x 14 columns]

Attributes and datatypes


age int64
sex int64
cp int64
trestbps int64
chol int64
fbs int64
restecg int64
thalach int64
exang int64
oldpeak float64
slope int64
ca int64
thal int64
heartdisease int64
dtype: object

Learning CPD using Maximum likelihood estimators

Inferencing with Bayesian Network:

1. Probability of HeartDisease given evidence= restecg


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.4354 |
+-----------------+---------------------+
| heartdisease(1) | 0.5646 |
+-----------------+---------------------+

2. Probability of HeartDisease given evidence= cp


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.3832 |
+-----------------+---------------------+
| heartdisease(1) | 0.6168 |
+-----------------+---------------------+

You might also like