This project contains Python solutions for a Artificial Intelligence course, this project in specific was focused on Bayesian networks, variable elimination and Naive Bayes classification.
The assignment has two main parts:
-
Question 1 - Variable Elimination
Implements variable elimination on the classic Burglary/Earthquake/Alarm Bayesian network to compute:P(Burglary | John Calls = +j) -
Question 2 - Diabetes Prediction Model
Builds a Naive Bayes prediction model using glucose level, blood pressure level and diabetes diagnosis data. The script splits the dataset into training and testing sets, learns probability tables from the training data and evaluates prediction accuracy on the test data.
Project 4/
├── DS442 project-4-1.pdf
├── README.md
└── Solution-2/
├── Naive-Bayes-Classification-Data.csv
├── solution_q1.py
└── solution_q2.py
The original project instructions.
Contains the implementation for Question 1. It defines helper functions for:
- Creating factors
- Applying evidence
- Multiplying factors
- Summing out hidden variables
- Normalizing the final factor
- Running variable elimination
When executed, it prints the final probability distribution for burglary given that John called.
Contains the implementation for Question 2. It performs the following steps:
- Loads the diabetes classification dataset
- Performs a stratified 70/30 train-test split
- Computes prior probabilities
P(Y) - Computes conditional probability tables
P(X1 | Y)andP(X2 | Y) - Computes posterior probabilities
P(Y | X1, X2) - Generates predictions for each test row
- Prints the final model accuracy
The dataset used by solution_q2.py. It contains the following columns:
glucosebloodpressurediabetes
This project is written in Python and is intended to run with Python 3.9 or later.
Question 1 only uses standard Python libraries.
Question 2 requires:
pandas
Install the dependency with:
pip install pandasOpen a terminal and navigate into the Solution-2 folder:
cd "Project 4/Solution-2"Run Question 1:
python solution_q1.pyRun Question 2:
python solution_q2.pyDepending on your system setup, you may need to use python3 instead of python:
python3 solution_q1.py
python3 solution_q2.pyThe script prints the probability distribution:
P(Burglary | John Calls = +j)
+b 0.016283729947
-b 0.983716270053
The script prints:
- Data split details
- Training and testing class counts
- Prior probabilities for
Y - Conditional probability tables for glucose and blood pressure
- Posterior probability lookup table for test data
- Predicted labels for all test rows
- Final accuracy
The current final accuracy is:
Accuracy = 273/300 = 0.910000000000
solution_q2.pyreadsNaive-Bayes-Classification-Data.csvusing a relative path, so it should be run from inside theSolution-2folder.- The train-test split uses
random.Random(1)so the results are reproducible. - The dataset split is stratified by the
diabeteslabel to preserve the class balance in both training and testing sets.