Skip to content

Commit 82108a6

Browse files
authoredNov 20, 2019
Create 7.py
Write a 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. You can use Java/Python ML library classes/API.
1 parent cf8ef97 commit 82108a6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 

‎7.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
import csv
3+
import pandas as pd
4+
from pgmpy.models import BayesianModel
5+
from pgmpy.estimators import MaximumLikelihoodEstimator
6+
from pgmpy.inference import VariableElimination
7+
heartDisease = pd.read_csv('heart.csv')
8+
heartDisease = heartDisease.replace('?',np.nan)
9+
print('Few examples from the dataset are given below')
10+
print(heartDisease.head())
11+
model = BayesianModel([('age','trestbps'),('age','fbs'),('sex','trestbps'),('exang','trestbps'),('trestbps','heartdisease'),('fbs','heartdisease'),('heartdisease','restecg'),('heartdisease','thalach'),('heartdisease','chol')])
12+
print('\nLearning CPD using Maximum likelihood estimators')
13+
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
14+
print('\n Inferencing with Bayesian Network:')
15+
HeartDisease_infer = VariableElimination(model)
16+
print('\n 1. Probability of HeartDisease given Age=28')
17+
q=HeartDisease_infer.query(variables=['heartdisease'],evidence={'age':28})
18+
print(q['heartdisease'])
19+
print('\n 2. Probability of HeartDisease given cholesterol=100')
20+
q=HeartDisease_infer.query(variables=['heartdisease'],evidence={'chol':100})
21+
print(q['heartdisease'])

0 commit comments

Comments
 (0)
Please sign in to comment.