Natural Language Processing(Part 20)-Training Naïve Bayes

Coursesteach
5 min readDec 3, 2023

--

📚Chapter 3: Sentiment Analysis (Naive Bayes)

This tutorial, I’ll show you how to train the Naive Bayes classifier. In this context, we train in something different than in logistic regression or deep learning. There is no gradient descent. We’re just counting frequencies of words in the corpus. You will now be creating step-by-step, a Naive Bayes model for sentiment analysis using a corpus of tweets that you’ve already collected. That’s awesome.

Sections

Get annotate a Dataset
Preprocess
Computer Frequency
Get Lambda
Computer Logprior
Summary

Section-1- Get annotate a Dataset

The first step for any supervised machine learning project is to gather the data to train and test your model. For sentiment analysis of tweets, this step involves getting a corpus of tweets and dividing it into two groups; positive and negative tweets.

Section 2- Preprocess

The next step is fundamental to your model success. The preprocessing step, as described in the previous module, consists of five smaller steps. One, lowercase the texts, 2, remove punctuation, URLs, and handles, 3, remove stop words, 4, stemming or reducing words to their common stem, and 5, finally, tokenizing or splitting your document into single words or tokens. For the assignments in this week, it will be relatively straightforward to implement this processing pipeline. In the real-world, you might find the gathering and processing of texts takes up a big chunk of your project’s hours.

Section 3- Computer Frequency

Once you have a clean corpus of processed tweets, you will start by computing the vocabulary foreach word in class, like you did in the previous week. This process will produce a table like the one shown here. You can compute the sum of words and class in each corpus in the same step. From this table of frequencies, you get the conditional probability or probability of word given class by using the Laplacian smoothing formula. See how the number of unique words in V class is equal to 6. You only account for the words in the table, not the total number of words in the original corpus. This produces a table of conditional probabilities for each word and each class. This table only contains values greater than 0.

Section 4- Get Lambda

For the forth step, you’ll get the Lambda score for each word, which is the log of the ratio of your conditional
probabilities.

Section 5- Computer Logprior

The fifth step is the estimation of the log prior. To do this, you’ll need to count the number of positive and negative tweets. Then the log prior is the log of the ratio of the number of positive tweets over the number of negative tweets. In the upcoming assignments, you’ll be working with a balanced datasets. The log prior is equal to 0. For unbalanced datasets, this term will become important.

Summary

In summary, training a Naive Bayes model can be divided into six logical steps. You get to annotate a dataset with positive and negative tweets. Typically, it’s better if your tweets match the same context that you want to use in the final model. Then you process the raw texts to get a corpus of clean standardized tokens. You compute the dictionary frequencies for each word in class, then compute the conditional probabilities of each word using the Laplacian smoothing formula, you compute the lambda factor for each word, and finally, you estimate the log prior of the model or how likely it is to see a positive tweet in your accounts. That was quite a lot. Now, you have seen how to build a probability table needed to apply Naive Bayes. The next exciting thing we’ll do is to classify
your sentences.

Please Follow and 👏 Clap for the story courses teach to see latest updates on this story

If you want to learn more about these topics: Python, Machine Learning Data Science, Statistic For Machine learning, Linear Algebra for Machine learning Computer Vision and Research

Then Login and Enroll in Coursesteach to get fantastic content in the data field.

Stay tuned for our upcoming articles where we will explore specific topics related to NLP in more detail!

Remember, learning is a continuous process. So keep learning and keep creating and sharing with others!💻✌️

Note:if you are a NLP export and have some good suggestions to improve this blog to share, you write comments and contribute.

if you need more update about NLP and want to contribute then following and enroll in following

👉Course: Natural Language Processing (NLP)

👉📚GitHub Repository

👉 📝Notebook

Do you want to get into data science and AI and need help figuring out how? I can offer you research supervision and long-term career mentoring.
Skype: themushtaq48, email:mushtaqmsit@gmail.com

Contribution: We would love your help in making coursesteach community even better! If you want to contribute in some courses , or if you have any suggestions for improvement in any coursesteach content, feel free to contact and follow.

Together, let’s make this the best AI learning Community! 🚀

👉WhatsApp

👉 Facebook

👉Github

👉LinkedIn

👉Youtube

👉Twitter

References

1- Natural Language Processing with Classification and Vector Spaces

--

--