VOOZH about

URL: https://www.geeksforgeeks.org/data-science/bayes-theorem-in-machine-learning/

⇱ Bayes Theorem in Machine learning - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bayes Theorem in Machine learning

Last Updated : 31 Dec, 2025

Bayes Theorem explains how to update the probability of a hypothesis when new evidence is observed. It combines prior knowledge with data to make better decisions under uncertainty and forms the basis of Bayesian inference in machine learning.

  • Handles uncertainty and noisy data effectively
  • Supports probabilistic interpretation of model predictions
  • Enables learning even with limited data
  • Plays a key role in real-world applications such as risk analysis and diagnosis

Mathematical Formulation of Bayes Theorem

Bayes Theorem describes the relationship between conditional probabilities and is mathematically expressed as:

where

  • Posterior Probability : the updated probability of hypothesis after observing evidence
  • Likelihood : the probability of observing evidence assuming hypothesis is true.
  • Prior Probability: the initial belief about hypothesis before observing any evidence.
  • Evidence (Marginal Likelihood): the total probability of observing evidence acting as a normalization factor.

Bayes Theorem for Multiple Hypotheses (n Events)

For a set of mutually exclusive and collectively exhaustive hypotheses and an observation the generalized Bayes’ Theorem is given by:

where

  • is the posterior probability of hypothesis
  • is the likelihood of observing under hypothesis
  • is the prior probability of

Step By Step Implementation

Here in this code we implements a Naive Bayes classifier that uses Bayes Theorem to compute the probability of a message being spam or ham based on word frequencies trains the model on labeled data, evaluates its performance and predicts the class of new unseen messages.

Step 1: Install and Import Required Libraries

  • Install essential Python libraries for data handling, ML modeling and visualization
  • pandas is used for data manipulation
  • scikit-learn provides ML algorithms and evaluation metrics
  • matplotlib and seaborn are used for visualization

Step 2: Load and Preprocess the Dataset

  • Load the spam dataset using pandas
  • Convert categorical labels into numeric values

You can download dataset from here

Output:

👁 bayes1
Output

Step 3: Separate Features and Target Variable

  • Text messages are taken as input features
  • Labels (0 for Ham, 1 for Spam) are the target variable

Step 4: Split Data into Training and Testing Sets

  • Split dataset into 80% training and 20% testing
  • random_state ensures reproducibility

Step 5: Convert Text Data into Numerical Form

  • Machine learning models cannot process raw text
  • CountVectorizer converts text into word frequency vectors

Step 6: Train the Naive Bayes Model

  • Multinomial Naive Bayes is well-suited for text classification
  • Train the model using vectorized training data

Step 7: Make Predictions on Test Data

  • Use the trained model to predict spam or ham messages
  • Predictions are stored for evaluation

Step 8: Evaluate Model Performance

  • Calculate accuracy to measure overall performance
  • Generate confusion matrix for detailed analysis
  • Display precision, recall, and F1-score

Output:

👁 Bayes2
Output

Step 9: Test the Model on New Messages

  • Use the trained model to classify unseen messages
  • Vectorize new messages using the same vocabulary

Output:

Message: 'Congratulations! You won a free ticket.' => Prediction: Spam
Message: 'Hey, are we still meeting today?' => Prediction: Ham

You can download full code from here

Applications of Bayes Theorem in Machine Learning

Bayes Theorem ability to handle uncertainty and incorporate prior knowledge allows models to make accurate predictions even with incomplete or noisy data like:

1. Naive Bayes Classifier: It is a simple probabilistic model based on Bayes’ theorem that assumes feature independence, making it efficient and effective for tasks like text classification and spam detection.

2. Bayes optimal classifier: The Bayes optimal classifier is a theoretical model that predicts the class with the highest posterior probability for given features, representing the best possible classification accuracy. It uses Bayes’ theorem to update probabilities based on new evidence.

where

  • : Predicted class label
  • : Possible class label
  • : Input feature vector
  • Posterior probability of class given features

3. Bayesian Optimization: Bayesian Optimization is a technique for efficiently finding the maximum or minimum of expensive to evaluate functions using a probabilistic model often a Gaussian process. It iteratively selects the most promising points to evaluate, making it ideal for tasks like hyperparameter tuning in machine learning.

4. Bayesian Belief Networks (BBNs): Bayesian Belief Networks (BBNs) or Bayesian networks are probabilistic graphical models that represent variables and their conditional dependencies using a directed acyclic graph (DAG). They are widely applied in risk analysis, diagnostics and decision-making.

Comment
Article Tags:

Explore