0% found this document useful (0 votes)
10 views2 pages

Exp 13

The document provides a Python program that uses the BayesPy library to analyze heart disease data from a CSV file. It defines various parameters related to age, gender, family history, diet, lifestyle, and cholesterol, then builds a Bayesian network to predict the probability of heart disease based on user input. The program allows for interactive testing where users can input their data and receive the probability of having heart disease.

Uploaded by

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

Exp 13

The document provides a Python program that uses the BayesPy library to analyze heart disease data from a CSV file. It defines various parameters related to age, gender, family history, diet, lifestyle, and cholesterol, then builds a Bayesian network to predict the probability of heart disease based on user input. The program allows for interactive testing where users can input their data and receive the probability of having heart disease.

Uploaded by

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

Before Run the code, Need to run the "pip install bayespy" at your command prompt

Program:
import bayespy as bp
import numpy as np
import csv
from colorama import init, Fore, Back, Style
init()

# Define Parameter Enum values


# Age
ageEnum = {'SuperSeniorCitizen': 0, 'SeniorCitizen': 1, 'MiddleAged': 2, 'Youth':
3, 'Teen': 4}
# Gender
genderEnum = {'Male': 0, 'Female': 1}
# FamilyHistory
familyHistoryEnum = {'Yes': 0, 'No': 1}
# Diet (Calorie Intake)
dietEnum = {'High': 0, 'Medium': 1, 'Low': 2}
# LifeStyle
lifeStyleEnum = {'Athlete': 0, 'Active': 1, 'Moderate': 2, 'Sedentary': 3}
# Cholesterol
cholesterolEnum = {'High': 0, 'BorderLine': 1, 'Normal': 2}
# HeartDisease
heartDiseaseEnum = {'Yes': 0, 'No': 1}

# Load heart disease data from CSV


with open('heart_disease_data.csv') as csvfile:
lines = csv.reader(csvfile)
dataset = list(lines)
data = []
for x in dataset:
data.append([ageEnum[x[0]], genderEnum[x[1]], familyHistoryEnum[x[2]],
dietEnum[x[3]],
lifeStyleEnum[x[4]], cholesterolEnum[x[5]],
heartDiseaseEnum[x[6]]])

# Training data for machine learning


data = np.array(data)
N = len(data)

# Input data column assignment


p_age = bp.nodes.Dirichlet(1.0 * np.ones(5))
age = bp.nodes.Categorical(p_age, plates=(N,))
age.observe(data[:, 0])

p_gender = bp.nodes.Dirichlet(1.0 * np.ones(2))


gender = bp.nodes.Categorical(p_gender, plates=(N,))
gender.observe(data[:, 1])

p_familyhistory = bp.nodes.Dirichlet(1.0 * np.ones(2))


familyhistory = bp.nodes.Categorical(p_familyhistory, plates=(N,))
familyhistory.observe(data[:, 2])

p_diet = bp.nodes.Dirichlet(1.0 * np.ones(3))


diet = bp.nodes.Categorical(p_diet, plates=(N,))
diet.observe(data[:, 3])

p_lifestyle = bp.nodes.Dirichlet(1.0 * np.ones(4))


lifestyle = bp.nodes.Categorical(p_lifestyle, plates=(N,))
lifestyle.observe(data[:, 4])

p_cholesterol = bp.nodes.Dirichlet(1.0 * np.ones(3))


cholesterol = bp.nodes.Categorical(p_cholesterol, plates=(N,))
cholesterol.observe(data[:, 5])

# Prepare nodes and establish edges


p_heartdisease = bp.nodes.Dirichlet(np.ones(2), plates=(5, 2, 2, 3, 4, 3))
heartdisease = bp.nodes.MultiMixture([age, gender, familyhistory, diet, lifestyle,
cholesterol],
bp.nodes.Categorical, p_heartdisease)
heartdisease.observe(data[:, 6])

# Update the network


p_heartdisease.update()

# Interactive Test
m = 0
while m == 0:
print("\n")
input_age = int(input('Enter Age (0-SuperSeniorCitizen, 1-SeniorCitizen, 2-
MiddleAged, 3-Youth, 4-Teen): '))
input_gender = int(input('Enter Gender (0-Male, 1-Female): '))
input_familyhistory = int(input('Enter FamilyHistory (0-Yes, 1-No): '))
input_diet = int(input('Enter Diet (0-High, 1-Medium, 2-Low): '))
input_lifestyle = int(input('Enter LifeStyle (0-Athlete, 1-Active, 2-Moderate,
3-Sedentary): '))
input_cholesterol = int(input('Enter Cholesterol (0-High, 1-BorderLine, 2-
Normal): '))

res = bp.nodes.MultiMixture([
input_age,
input_gender,
input_familyhistory,
input_diet,
input_lifestyle,
input_cholesterol
], bp.nodes.Categorical, p_heartdisease).get_moments()[0]
[heartDiseaseEnum['Yes']]

print(Fore.RED + "Probability of Heart Disease = " + str(res))


print(Style.RESET_ALL)
m = int(input("Enter 0 to Continue or 1 to Exit: "))

You might also like