chapter five

5 Using probability to its maximum The naive Bayes algorithm

 

This chapter covers:

  • What is Bayes theorem?
  • When are events dependent of independent?
  • The prior and the posterior probabilities.
  • Calculating conditional probabilities based on events.
  • What is the naive Bayes algorithm?
  • Using the naive Bayes algorithm to predict if an email is spam or ham, based on the words in the email.
  • Coding the naive Bayes algorithm in Python.

Naive Bayes is a very important machine learning algorithm used for prediction. As opposed to the previous algorithms you’ve learned in this book, such as the perceptron algorithm, in which the prediction is discrete (0 or 1), the naive Bayes algorithm is purely probabilistic. This means, the prediction is a number between 0 and 1, indicating the probability that a label is positive. The main component of naive Bayes is Bayes Theorem.

Bayes Theorem is a fundamental theorem in probability and statistics, and it helps us calculate probabilities. The more we know about a certain situation, the more accurate the probability is. For example, let’s say we want to find the probability that it will snow today. If we have no information of where we are and what time of the year it is, we can only come up with a vague estimate.

5.1       Sick or healthy? A story with Bayes Theorem

5.1.2    Prelude to Bayes Theorem: The prior, the event, and the posterior

5.2       Use-case: Spam detection model

5.2.1    Finding the prior: The probability that any email is spam

5.2.2    Finding the posterior: The probability that an email is spam knowing that it contains a particular word

5.2.3    What the math just happened? Turning ratios into probabilities

5.2.3    What about two words? The naive Bayes algorithm

5.2.4    What about more than two words?

5.3.      Building a spam detection model with real data

5.3.1    Data preprocessing

5.3.2    Finding the priors

5.3.3    Finding the posteriors with Bayes theorem

5.3.4    Implementing the naive Bayes algorithm

5.3.5    Further work

5.6       Summary