Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

DS/CMPSC 442 Project 4: Bayesian Networks

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.

Project Overview

The assignment has two main parts:

  1. Question 1 - Variable Elimination
    Implements variable elimination on the classic Burglary/Earthquake/Alarm Bayesian network to compute:

    P(Burglary | John Calls = +j)
    
  2. 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 Structure

Project 4/
├── DS442 project-4-1.pdf
├── README.md
└── Solution-2/
    ├── Naive-Bayes-Classification-Data.csv
    ├── solution_q1.py
    └── solution_q2.py

Files

DS442 project-4-1.pdf

The original project instructions.

Solution-2/solution_q1.py

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.

Solution-2/solution_q2.py

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) and P(X2 | Y)
  • Computes posterior probabilities P(Y | X1, X2)
  • Generates predictions for each test row
  • Prints the final model accuracy

Solution-2/Naive-Bayes-Classification-Data.csv

The dataset used by solution_q2.py. It contains the following columns:

  • glucose
  • bloodpressure
  • diabetes

Requirements

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 pandas

How to Run

Open a terminal and navigate into the Solution-2 folder:

cd "Project 4/Solution-2"

Run Question 1:

python solution_q1.py

Run Question 2:

python solution_q2.py

Depending on your system setup, you may need to use python3 instead of python:

python3 solution_q1.py
python3 solution_q2.py

Expected Output Summary

Question 1

The script prints the probability distribution:

P(Burglary | John Calls = +j)
+b 0.016283729947
-b 0.983716270053

Question 2

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

Notes

  • solution_q2.py reads Naive-Bayes-Classification-Data.csv using a relative path, so it should be run from inside the Solution-2 folder.
  • The train-test split uses random.Random(1) so the results are reproducible.
  • The dataset split is stratified by the diabetes label to preserve the class balance in both training and testing sets.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages