Multinomial Naive Bayes is one of the variation of Naive Bayes algorithm. A classification algorithm based on Bayes' Theorem ideal for discrete data and is typically used in text classification problems. It models the frequency of words as counts and assumes each feature or word is multinomially distributed. MNB is widely used for tasks like classifying documents based on word frequencies like in spam email detection.
How Does Multinomial Naive Bayes Work?
In Multinomial Naive bayes - Naive means that the method assumes all features like words in a sentence are independent from each other and Multinomial refers to how many times a word appears or how often a category occurs. It works by using word counts to classify text. The main idea is that it assumes each word in a message or feature is independent of each others. This means the presence of one word doesn't affect the presence of another word which makes the model easy to use.
The model looks at how many times each word appears in messages from different categories (like "spam" or "not spam"). For example if the word "free" appears often in spam messages that will be used to help predict whether a new message is spam or not.
To calculate the probability of a message belonging to a certain category Multinomial Naive Bayes uses the multinomial distribution:
P(X) = \frac{n!}{n_1! n_2! \ldots n_m!} p_1^{n_1} p_2^{n_2} \ldots p_m^{n_m}
Where:
- n is the total number of trials.
- ni is the count of occurrences for outcome i.
- pi is the probability of outcome i.
To estimate how likely each word is in a particular class like "spam" or "not spam" we use a method called Maximum Likelihood Estimation (MLE). This helps finding probabilities based on actual counts from our data. The formula is:
\quad \theta_{c,i} = \frac{\text{count}(w_i, c) + 1}{N + v}
Where:
- count(wi,c) is the number of times word wi appears in documents of class c.
- \Nu is the total number of words in documents of class cc.
- v is the vocabulary size.
Implementation of Multinomial Naive Bayes
Let's understand it with a example of spam email detection. We'll classify emails into two categories: spam and not spam.
1. Importing Libraries:
pandas: Used for handling data in DataFrame format.CountVectorizer: Converts a collection of text documents into a matrix of token counts.train_test_split: Splits the data into training and test sets for model evaluation.MultinomialNB: A Naive Bayes classifier suited for classification tasks with discrete features (such as word counts).accuracy_score: Computes the accuracy of the model's predictions.
Python
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
2. Creating the Dataset
Python
data = {
'text': [
'Free money now',
'Call now to claim your prize',
'Meet me at the park',
'Let’s catch up later',
'Win a new car today!',
'Lunch plans?',
'Congratulations! You won a lottery',
'Can you send me the report?',
'Exclusive offer for you',
'Are you coming to the meeting?'
],
'label': ['spam', 'spam', 'not spam', 'not spam', 'spam', 'not spam', 'spam', 'not spam', 'spam', 'not spam']
}
df = pd.DataFrame(data)
- A simple dataset is created with text messages labeled as either spam or not spam.
- This data is then converted into a DataFrame for easy handling.
3. Mapping Labels to Numerical Values
Python
df['label'] = df['label'].map({'spam': 1, 'not spam': 0})
- The labels (
spam and not spam) are mapped to numerical values where spam becomes 1 and not spam becomes 0. This is necessary for the classifier, as it works with numerical data.
4. Splitting the Data
Python
X = df['text']
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
X contains the text messages (features), and y contains the labels (target).- The dataset is split into training (70%) and testing (30%) sets using
train_test_split.
5. Vectorizing the Text Data
Python
vectorizer = CountVectorizer()
X_train_vectors = vectorizer.fit_transform(X_train)
X_test_vectors = vectorizer.transform(X_test)
CountVectorizer is used to convert text data into numerical vectors. It counts the occurrences of each word in the corpus.fit_transform() is applied to the training data to learn the vocabulary and transform it into a feature matrix.transform() is applied to the test data to convert it into the same feature space.
6. Training the Naive Bayes Model
Python
model = MultinomialNB()
model.fit(X_train_vectors, y_train)
- A Multinomial Naive Bayes classifier is created and trained using the vectorized training data (
X_train_vectors) and corresponding labels (y_train).
7. Making Predictions and Evaluating Accuracy
Python
y_pred = model.predict(X_test_vectors)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%\n")
8. Predicting for a Custom Message
Python
custom_message = ["Congratulations, you've won a free vacation"]
custom_vector = vectorizer.transform(custom_message)
prediction = model.predict(custom_vector)
print("Prediction for custom message:", "Spam" if prediction[0] == 1 else "Not Spam")
Output:
Accuracy: 66.67%
Prediction for custom message: Spam
In the above code we did spam detection for given set of messages and evaluated model accuracy for the output it gave.
How Multinomial Naive Bayes differs from Gaussian Naive Bayes?
The Multinomial naive bayes and Gaussian naive bayes both are the variants of same algorithm. However they have several number of differences which are discussed below:
Multinomial Naive Bayes | Gaussian Naive Bayes |
|---|
It specially designed for discrete data particularly text data. | It is suitable for continuous data where features follow a Gaussian distribution. |
It assumes features and represent its counts like word counts. | It assumes a Gaussian distribution for the likelihood. |
It is commonly used in NLP for document classification tasks. | It is commonly used in tasks involving continuous data such as medical diagnosis, fraud detection and weather prediction. |
The likelihood of each feature is calculated using the multinomial distribution. | The likelihood of each feature is modeled using the Gaussian distribution. |
It is more efficient when the number of features is very high like in text datasets with thousands of words. | It can handle continuous data but if the data is sparse or contains many outliers it struggle with accuracy |
Multinomial Naive Bayes efficiency combined with its ability to handle large datasets makes it useful for applications like document categorization and email filtering. In the next article we will explore the Bernoulli Naive Bayes model which is another variant of Naive Bayes.
Similar Reads
Multinomial Naive Bayes
Multinomial Naive Bayes is one of the variation of Naive Bayes algorithm. A classification algorithm based on Bayes' Theorem ideal for discrete data and is typically used in text classification problems. It models the frequency of words as counts and assumes each feature or word is multinomially dis
6 min read
Multinomial Naive Bayes Classifier in R
The Multinomial Naive Bayes (MNB) classifier is a popular machine learning algorithm, especially useful for text classification tasks such as spam detection, sentiment analysis, and document categorization. In this article, we discuss about the basics of the MNB classifier and how to implement it in
6 min read
Gaussian Naive Bayes
Gaussian Naive Bayes is a type of Naive Bayes method working on continuous attributes and the data features that follows Gaussian distribution throughout the dataset. Before diving deep into this topic we must gain a basic understanding of principles on which Gaussian Naive Bayes work. Here are some
5 min read
Bernoulli Naive Bayes
Bernoulli Naive Bayes is a subcategory of the Naive Bayes Algorithm. It is typically used when the data is binary and it models the occurrence of features using Bernoulli distribution. It is used for the classification of binary features such as 'Yes' or 'No', '1' or '0', 'True' or 'False' etc. Here
5 min read
Applying Multinomial Naive Bayes to NLP Problems
Multinomial Naive Bayes (MNB) is a popular machine learning algorithm for text classification problems in Natural Language Processing (NLP). It is particularly useful for problems that involve text data with discrete features such as word frequency counts. MNB works on the principle of Bayes theorem
7 min read
Naive Bayes Classifiers
Naive Bayes classifiers are supervised machine learning algorithms used for classification tasks, based on Bayes' Theorem to find probabilities. This article will give you an overview as well as more advanced use and implementation of Naive Bayes in machine learning. Key Features of Naive Bayes Clas
9 min read
Complement Naive Bayes (CNB) Algorithm
Naive Bayes algorithms are a group of very popular and commonly used Machine Learning algorithms used for classification. There are many different ways the Naive Bayes algorithm is implemented like Gaussian Naive Bayes, Multinomial Naive Bayes, etc. To learn more about the basics of Naive Bayes, you
7 min read
Gaussian Naive Bayes using Sklearn
In the world of machine learning, Gaussian Naive Bayes is a simple yet powerful algorithm used for classification tasks. It belongs to the Naive Bayes algorithm family, which uses Bayes' Theorem as its foundation. The goal of this post is to explain the Gaussian Naive Bayes classifier and offer a de
8 min read
Important Naive Bayes Interview Questions
Naive Bayes is a foundational algorithm in machine learning based on Bayes' Theorem - which is a way to calculate the probability of an event occurring given some prior knowledge. For classifying, it helps predict the class (or category) of a new data point based on its features. Naive Bayes is a co
7 min read
ML | Naive Bayes Scratch Implementation using Python
Naive Bayes is a probabilistic machine learning algorithms based on the Bayes Theorem. It is a simple yet powerful algorithm because of its understanding, simplicity and ease of implementation. It is popular method for classification applications such as spam filtering and text classification. In th
7 min read