Naive Bayes Basics
Lesson Introduction
Hey there! Today we are going to explore an exciting topic in machine learning called Naive Bayes. By the end of this lesson, you'll understand what Naive Bayes is and how to implement it using Python's Scikit-Learn library. Let’s dive in!
Understanding Naive Bayes
Naive Bayes is a classification algorithm based on Bayes' Theorem. Imagine you’re a detective using clues (features) to decide who the culprit is (class). Naive Bayes helps by calculating probabilities.
Bayes' Theorem is stated as:
Where:
- is the posterior probability of class given predictor .
- is the likelihood which is the probability of predictor given class .
- is the prior probability of class .
- is the prior probability of predictor .
How Naive Bayes Works
- Prior Probability: The algorithm starts by calculating the prior probability for each class based on the training data. It is simply the probability of a sample being of the class if we know no data about the sample. For example, imagine we predict if the email is spam or not. If the 93% of the emails in the data are not spam, then it is reasonable to suppose that a given email will be not spam with the probability of
93%. This is what the prior probability is. - Likelihood: For each feature, the likelihood (probability of the feature given the class) is calculated. It is essentially the probability of a sample with a given feature to be of the given class.
- Independent Features Assumption (Naive Assumption): Assumes that the features are independent, which simplifies calculations.
- Posterior Probability: Using Bayes' Theorem, the posterior probability of each class is computed given the feature values. The class with the highest posterior probability is chosen as the prediction.
How Naive Bayes Learns
Naive Bayes updates its likelihoods and priors using the training data. When the model encounters new data, it breaks the data into its constituent features and applies Bayes' Theorem to calculate the class probabilities. The class with the highest probability is the predicted class.
We will focus on GaussianNB, commonly used when features are continuous and assumed to follow a normal (Gaussian) distribution.
