# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="KzkBSef9Lr-V"
# **7. 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**

# + colab={"base_uri": "https://localhost:8080/"} id="6baujLTsHTQn" outputId="7e9ae9ac-d2e8-48c8-fb7b-1056ea4d6713"
pip install pgmpy

# + id="FzO_uYnwDMkC"
import numpy as np
import pandas as pd
import csv
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianModel
from pgmpy.inference import VariableElimination


# + id="EQHeiM0YITEe"
#read CleveLand Heart Disease Data
# heartDisease = pd.read_csv('/home/sahyadri/Download/heart.csv')
heartDisease = pd.read_csv('heart.csv')
heartDisease = heartDisease.replace('?', np.nan)

# + colab={"base_uri": "https://localhost:8080/"} id="Gcq9WlzgITG_" outputId="f5062362-a5ae-4c01-c336-fe23f203946d"
#display the data
print('Sample Instance from the dataset are given below')
print(heartDisease.head())

# + colab={"base_uri": "https://localhost:8080/"} id="9Qc4jxD9JzeC" outputId="08574c7e-05f8-4407-880c-1fcf4496620f"
#display the Attributes names and datatypes
print('\n Attributes and datatypes')
print(heartDisease.dtypes)

# + colab={"base_uri": "https://localhost:8080/"} id="1eKugVVhJzgN" outputId="5bfa841d-765b-4b3c-8c67-031fc7303c7d"
#Create Model Bayesian network 
model = BayesianModel([('age','heartdisease'),('sex','heartdisease'),('exang','heartdisease'),('cp','heartdisease'),('heartdisease',
'restecg'),('heartdisease','chol')])

# + colab={"base_uri": "https://localhost:8080/"} id="Ume9-cfFKryD" outputId="1cef1edc-07e6-4422-a19e-249d19dc0e3b"
#Learning CPDs using Maximum Likelihood Estimators
print('\n Learning CPD using Maximum likelihood estimators')
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)

# + colab={"base_uri": "https://localhost:8080/"} id="Zrx_g0WQK7L2" outputId="e0b0142a-3903-4827-e6b3-ad2d63c51a6f"
# Inferencing with Bayesian Network
print('\n Inferencing with Bayesian Network:')
HeartDiseasetest_infer = VariableElimination(model)

# + colab={"base_uri": "https://localhost:8080/", "height": 341, "referenced_widgets": ["83fce258599d4f1bbbe87cb2384c0d4a", "2cd9395ada774c209882a08f27f2da3a", "65e8305ee6344d8d8621c6bd027a408e", "<KEY>", "270f8288e1b1492d8426ead7c3db1ead", "503bad1f45e54fbf82106de34fd4b494", "673d332fdef74f16b9903b11aed2b588", "<KEY>", "<KEY>", "<KEY>", "5a29b79606a84ec497fa336fec0130c2", "5d26bba5c6284a8d9382fb2c4ede478d", "53526a17dad047cca7b47adba798aaaa", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "18f26f0997154950890225eb00b4e445", "<KEY>", "4f5578d826f54c588c15861c3119c25c"]} id="bNxAYShKK7OU" outputId="245a456d-57c5-4c12-a1c1-02fd81229676"
#computing the Probability of HeartDisease given restecg
print('\n 1.Probability of HeartDisease given evidence=restecg :1')
q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)

# + colab={"base_uri": "https://localhost:8080/", "height": 341, "referenced_widgets": ["7b4319ac717a444487fcd2815e7a4726", "ee62c98f02a645f5b52a40b9929215c6", "f2236c3e73a2410688ddbee7f82de1d4", "a4e059ec76a548c7af0a5d757e2f6d86", "a96e0ad762054116a3d2e8f3cfa8b38f", "24a6be2884bd44a999413cbc37eeb060", "32dbd9dd9b2442dba700c251393e6c07", "d015df69f52a4788b713a0eb201f9b8d", "841bf09366b448079b54b3135b52d601", "87ea1c52145a43468cf7085330c2aaaf", "a326365a9c6f457ea55070e74ca6d9b0", "<KEY>", "<KEY>", "<KEY>", "1ecaa375f0164b3abf5797c29abc2a0d", "7db1427fef074c91adbe8864b3f44046", "56875e7c2045407d82d417672987752e", "984818cd37f54ee9a30cdf6fee6ec4af", "0d694421c2af4cfcaba1b07ea4841ace", "<KEY>", "4fa533e8dd2342769c745df1f5ffd3ac", "e8455fac044f441489838922e012dc3f"]} id="gBKoecJ_LHrT" outputId="64eab528-4568-4e08-b753-2d7ec764dc03"
#computing the Probability of HeartDisease given cp
print('\n 2.Probability of HeartDisease given evidence= cp:2 ')
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})
print(q2)

StarcoderdataJupyterScriptsDedupFiltered