Ai 5
Ai 5
ARTIFICIAL INTELLIGENCE
LAB NO 5
NAIVE BAYES ALGORITHM
TASK NO 1:
Objective:
Implement the Naïve Bayes classification model in Python to classify a given test sample based on
a provided training dataset. The model will utilize Gaussian probability distribution for numerical
features to predict the class of the test sample.
Task Steps:
2. Algorithm Explanation:
• Provide a detailed breakdown of the Naïve Bayes algorithm steps:
• Computation of prior probabilities for each class based on the training data labels.
• Calculation of likelihood probabilities for the test sample against training samples using
Gaussian probability density function.
• Computation of posterior probabilities by combining prior probabilities and likelihoods.
• Prediction of the test sample's class based on maximum posterior probability.
3. Python Implementation:
• Share the Python code implementing the Naïve Bayes classifier.
• Highlight the key functions and their purposes:
• `compute_prior_probability`: Computes prior probabilities for each class.
• `gaussian_likelihood`: Computes likelihood probabilities using Gaussian distribution.
• `naive_bayes`: Implements the Naïve Bayes algorithm for classification.
• Explain the structure of the code, including the representation of training data, labels, and
the test sample.
4. Dataset Description:
• Describe the provided dataset consisting of age, loan amount, and class (defaulter).
• Explain the significance of the features (age, loan amount) and the target variable (class).
5. Classification Process:
• Discuss the steps involved in classifying the test sample using the Naïve Bayes model:
• Loading the provided training dataset.
• Initializing the test sample for classification.
7. Discussion:
• Discuss the implications of the Naïve Bayes classifier's outcome on predicting the class
of the test sample.
• Analyze the model's efficiency and its potential applications in similar classification
tasks.
8. Conclusion:
• Summarize the effectiveness of the Naïve Bayes model in classifying the test sample.
• Provide insights into possible areas for improvement or further exploration.
Code:
Output:
TASK NO 2:
Objective:
Implement the Naïve Bayes classification algorithm from scratch using Python for the diabetes
dataset, comprising features like Pregnancies, Glucose, BloodPressure, SkinThickness, Insulin,
BMI, DiabetesPedigreeFunction, Age, and Outcome.
Steps Taken:
2. 2-Fold CrossValidation:
Split the dataset into training and testing sets using a 2-fold cross-validation approach
(test_size=0.5, random_state=42).
Conclusion:
• Successfully implemented Naïve Bayes classifier from scratch for the diabetes dataset.
• Achieved accuracy in predicting the classes using Gaussian probabilities and prior
probabilities.
• Adjustments made regarding function representations and library imports resulted in the
successful execution of the algorithm.
Code:
Output: