Group Information Retrieval Practicals

Q3: Construct a simple Bayesian network using medical features and demonstrate heart-disease classification. The answer uses a small example dataset and Naive Bayes conditional probabilities. This educational example is not a medical diagnostic tool.

Bayesian Network for Heart Disease Data

Solution and implementation for Q3 from Information Retrieval (ir).

3_bayesian_heart_disease.py Download
"""Small Naive Bayes network example for heart-disease data."""

data = [
    {"chest_pain": "yes", "high_cholesterol": "yes", "disease": "yes"},
    {"chest_pain": "yes", "high_cholesterol": "no", "disease": "yes"},
    {"chest_pain": "no", "high_cholesterol": "yes", "disease": "yes"},
    {"chest_pain": "no", "high_cholesterol": "no", "disease": "no"},
    {"chest_pain": "no", "high_cholesterol": "no", "disease": "no"},
    {"chest_pain": "yes", "high_cholesterol": "no", "disease": "yes"},
]

evidence = {"chest_pain": input("Chest pain (yes/no): ").lower(),
            "high_cholesterol": input("High cholesterol (yes/no): ").lower()}
classes = ["yes", "no"]
scores = {}

for diagnosis in classes:
    class_rows = [row for row in data if row["disease"] == diagnosis]
    probability = len(class_rows) / len(data)
    for feature, value in evidence.items():
        matches = sum(row[feature] == value for row in class_rows)
        probability *= (matches + 1) / (len(class_rows) + 2)
    scores[diagnosis] = probability

prediction = max(scores, key=scores.get)
print("Estimated class:", "Heart disease" if prediction == "yes" else "No heart disease")
print("Class probabilities:", scores)
print("This small example is for coursework only, not medical diagnosis.")

Other Questions in Information Retrieval

See All Available Questions
Download