{"id":4834,"date":"2026-10-14T12:20:25","date_gmt":"2026-10-14T12:20:25","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=4834"},"modified":"2026-10-15T15:15:07","modified_gmt":"2026-10-15T15:15:07","slug":"naive-bayes-classifier-in-machine-learning","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/","title":{"rendered":"Naive Bayes Classifier in Machine Learning"},"content":{"rendered":"<h1><strong><b>Naive Bayes Classifier in Machine Learning<\/b><\/strong><\/h1>\n<p>Every day your email inbox decides, in a fraction of a second, whether a message is spam. It does not read the message the way you do. It looks at the words, asks how often each one appears in spam compared with normal mail, and combines those clues into a single judgement. One of the oldest and most successful ways to do that is the Naive Bayes classifier.<\/p>\n<p>Naive Bayes is a classification method built on a result from probability called Bayes theorem. Despite a deliberately simple assumption, it is fast, needs little training data and works surprisingly well on text problems such as spam filtering, sentiment analysis and document sorting. That is why it is often the first model people try, and a sensible baseline to beat.<\/p>\n<p>This guide builds the idea from the ground up. We start with Bayes theorem and why the method is called naive, then work through a full example by hand so you can see every number. After that comes Gaussian Naive Bayes for continuous data with a working Python program, the other variants, smoothing, strengths and limits, applications, common mistakes and a short FAQ.<\/p>\n<h2><strong><b>Introduction to Bayes Theorem<\/b><\/strong><\/h2>\n<p>Bayes theorem tells you how to update a belief when you see new evidence. It connects two questions that sound similar but are not the same: how likely is the evidence if the thing is true, and how likely is the thing to be true given the evidence.<\/p>\n<p>P(A given B) = P(B given A) * P(A) \/ P(B)<\/p>\n<ul>\n<li><b><\/b><strong><b>P(A given B)<\/b><\/strong>is the probability of A once you know B has happened. This is what we want.<\/li>\n<li><b><\/b><strong><b>P(B given A)<\/b><\/strong>is the probability of seeing B if A is true. It is called the likelihood.<\/li>\n<li><b><\/b><strong><b>P(A)<\/b><\/strong>is how likely A is before seeing anything. It is called the prior.<\/li>\n<li><b><\/b><strong><b>P(B)<\/b><\/strong>is how likely the evidence is overall.<\/li>\n<\/ul>\n<p>Here is a small example. Suppose 20 percent of your email is spam. The word offer appears in 60 percent of spam but only 5 percent of normal mail. An email arrives containing offer. How likely is it to be spam?<\/p>\n<p>Spam and offer: \u00a0\u00a0\u00a0\u00a0\u00a00.60 * 0.20 = 0.12<\/p>\n<p>Not spam and offer: \u00a00.05 * 0.80 = 0.04<\/p>\n<p>P(spam given offer) = 0.12 \/ (0.12 + 0.04) = 0.75<\/p>\n<p>A 20 percent prior turned into a 75 percent probability after one piece of evidence. That is Bayes theorem at work.<\/p>\n<h2><strong><b>Why is It Called Naive?<\/b><\/strong><\/h2>\n<p>A real email has many words, not one. Doing the exact calculation with all of them would need to know how every word combination behaves together, which needs far more data than anyone has. Naive Bayes takes a shortcut. It assumes that, once you know the class, each feature is independent of the others. So it simply multiplies the clues together.<\/p>\n<p>That assumption is almost never exactly true. Words like free and offer often appear together. But the method is still often accurate, because it only needs to rank the classes correctly and not to get every probability right. The assumption is the naive part, and it is also what makes the method so cheap to train.<\/p>\n<p>One correction to a common misreading: this does not mean every feature gets the same weight. Features with stronger evidence move the answer more. It means the features are treated as independent given the class.<\/p>\n<h2><strong><b>Naive Bayes Classifier Example<\/b><\/strong><\/h2>\n<p>Imagine you want to predict whether a cricket match will be played, based on the weather. Here are 14 past days. The first four columns are the <strong><b>features<\/b><\/strong>, collected into a feature matrix. The last column is the <strong><b>response<\/b><\/strong>, the thing we want to predict, collected into a response vector.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 640px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Weather and cricket dataset<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Outlook<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Temperature<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Humidity<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Windy<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Cricket played<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Sunny<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Hot<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Sunny<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Hot<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Overcast<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Hot<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Rainy<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Mild<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Rainy<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Cool<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Rainy<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Cool<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Overcast<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Cool<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Sunny<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Mild<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Sunny<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Cool<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Rainy<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Mild<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Sunny<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Mild<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Overcast<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Mild<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">High<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-weight: bold; color: #0f2a4a;\">Overcast<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Hot<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Normal<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<td style=\"padding: 12px 16px; font-weight: bold; color: #0f2a4a;\">Rainy<\/td>\n<td style=\"padding: 12px 16px;\">Mild<\/td>\n<td style=\"padding: 12px 16px;\">High<\/td>\n<td style=\"padding: 12px 16px;\">Yes<\/td>\n<td style=\"padding: 12px 16px;\">No<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong><b>The Two Assumptions<\/b><\/strong><\/p>\n<ul>\n<li><b><\/b><strong><b>Conditional independence.<\/b><\/strong>Given whether cricket is played, the weather features do not influence each other.<\/li>\n<li><b><\/b><strong><b>Every feature contributes.<\/b><\/strong>All the features are used as evidence, though not all push equally hard.<\/li>\n<\/ul>\n<h2><strong><b>How Naive Bayes Works, Step by Step<\/b><\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4835\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png\" alt=\"naive bayes algorithm in python\" width=\"850\" height=\"505\" \/><\/p>\n<p>New day to predict: Sunny, Mild, Normal humidity, not windy. Will cricket be played? For each answer, Yes and No, we multiply a prior by the likelihood of each feature, then compare.<\/p>\n<p><strong><b>Step 1: the priors.<\/b><\/strong>\u00a0Cricket was played on 9 of the 14 days and not played on 5.<\/p>\n<p>P(Yes) = 9\/14 = 0.643<\/p>\n<p>P(No) \u00a0= 5\/14 = 0.357<\/p>\n<p><strong><b>Step 2: the likelihoods for Yes.<\/b><\/strong>\u00a0Among the 9 days when cricket was played, count how often each feature value appeared.<\/p>\n<p>P(Sunny | Yes) \u00a0= 2\/9<\/p>\n<p>P(Mild | Yes) \u00a0\u00a0= 4\/9<\/p>\n<p>P(Normal | Yes) = 6\/9<\/p>\n<p>P(Not windy | Yes) = 6\/9<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4836\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB2.png\" alt=\"naive bayes algorithm\" width=\"850\" height=\"110\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB2.png 681w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB2-300x39.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><strong><b>Step 3: the likelihoods for No.<\/b><\/strong>\u00a0Do the same for the 5 days when cricket was not played.<\/p>\n<p>P(Sunny | No) \u00a0= 3\/5<\/p>\n<p>P(Mild | No) \u00a0\u00a0= 2\/5<\/p>\n<p>P(Normal | No) = 1\/5<\/p>\n<p>P(Not windy | No) = 2\/5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4837\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB3.png\" alt=\"naive bayes classifier\" width=\"850\" height=\"99\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB3.png 653w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB3-300x35.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><strong><b>Step 4: multiply and compare.<\/b><\/strong><\/p>\n<p>Score(Yes) = 0.643 * 0.222 * 0.444 * 0.667 * 0.667 = 0.0282<\/p>\n<p>Score(No) \u00a0= 0.357 * 0.600 * 0.400 * 0.200 * 0.400 = 0.0069<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4838\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB8.png\" alt=\"naive bayes algorithm tutorial\" width=\"850\" height=\"515\" \/><\/p>\n<p>Yes scores higher. To turn the scores into probabilities, divide each by their total. That gives about 80.5 percent for Yes and 19.5 percent for No. Naive Bayes predicts that cricket will be played.<\/p>\n<h3><strong><b>The Zero Frequency Problem and Smoothing<\/b><\/strong><\/h3>\n<p>What if a feature value never appeared with a class in your training data? Its likelihood would be zero, and because we multiply, one zero wipes out the whole score. The fix is <strong><b>smoothing<\/b><\/strong>. Add a small count, usually 1, to every feature value so nothing is ever exactly zero. Adding 1 is called Laplace smoothing. In scikit learn, the setting is called alpha.<\/p>\n<h2><strong><b>Gaussian Naive Bayes Classifier<\/b><\/strong><\/h2>\n<p>The weather example uses categories. What about measurements such as height, temperature in degrees or a blood test value? Gaussian Naive Bayes handles continuous features by assuming that, within each class, each feature follows a bell shaped normal distribution. For every class it learns the average and the spread of each feature.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4839\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB4.png\" alt=\"Gaussian Naive Bayes classifier\" width=\"850\" height=\"741\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB4.png 451w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB4-300x261.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>The probability of a feature value x for a class y is then read off the bell curve:<\/p>\n<p>P(x | y) = 1 \/ sqrt(2 * pi * sigma_y^2) * exp( -(x &#8211; mu_y)^2 \/ (2 * sigma_y^2) )<\/p>\n<p>Here mu_y is the average of the feature in class y, and sigma_y is its standard deviation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4840\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB5.png\" alt=\"gaussian naive bayes\" width=\"850\" height=\"124\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB5.png 396w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB5-300x44.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2><strong><b>Gaussian Naive Bayes in Python<\/b><\/strong><\/h2>\n<p>We will use the breast cancer dataset that comes with scikit learn, which has 30 measurements for each of 569 tumours and a label for malignant or benign. This complete program loads the data, splits it, trains the model and checks the accuracy.<\/p>\n<p>from sklearn.datasets import load_breast_cancer<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.naive_bayes import GaussianNB<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<p>&nbsp;<\/p>\n<p>X, y = load_breast_cancer(return_X_y=True)<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)<\/p>\n<p>&nbsp;<\/p>\n<p>model = GaussianNB()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>&nbsp;<\/p>\n<p>print(&#8220;Accuracy:&#8221;, round(accuracy_score(y_test, y_pred) * 100, 2), &#8220;percent&#8221;)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-4841\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB6.png\" alt=\"gaussian naive bayes Probability\" width=\"850\" height=\"194\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB6.png 709w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB6-300x69.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>Running it prints:<\/p>\n<p>Accuracy: 93.71 percent<\/p>\n<p>A model trained in a fraction of a second gets about 94 percent of the test cases right, which is a respectable result for a baseline. If you also want the probabilities, call predict_proba, but see the warning below before you trust them.<\/p>\n<h2><strong><b>Types of Naive Bayes Classifiers<\/b><\/strong><\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Naive Bayes variants<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 170px;\" scope=\"col\">Variant<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 210px;\" scope=\"col\">Feature type<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Best suited for<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">GaussianNB<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Continuous numbers<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Measurements that look roughly bell shaped<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">MultinomialNB<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Counts<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Text classification using word counts or tf idf<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">ComplementNB<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Counts<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Imbalanced text data, often better than MultinomialNB<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">BernoulliNB<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes or no features<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Word present or absent, shorter documents<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">CategoricalNB<\/th>\n<td style=\"padding: 12px 16px;\">Categories<\/td>\n<td style=\"padding: 12px 16px;\">Data like the weather table, encoded as numbers first<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><strong><b>Strengths and Limitations of Naive Bayes<\/b><\/strong><\/h2>\n<h3><strong><b>Strengths<\/b><\/strong><\/h3>\n<ul>\n<li>Fast to train and fast to predict.<\/li>\n<li>Needs only a small amount of training data.<\/li>\n<li>Copes well with a very large number of features, such as words.<\/li>\n<li>Works well on text problems like spam filtering.<\/li>\n<li>Simple to explain, since you can read off which features pushed the answer.<\/li>\n<\/ul>\n<h3><strong><b>Limitations<\/b><\/strong><\/h3>\n<ul>\n<li>The independence assumption is rarely true, so strongly related features are double counted.<\/li>\n<li><b><\/b><strong><b>The probability scores are unreliable.<\/b><\/strong>scikit learn itself notes that Naive Bayes is a decent classifier but a poor estimator, so the output of predict_proba should not be taken too seriously without calibration.<\/li>\n<li>Gaussian Naive Bayes expects roughly bell shaped features and can struggle otherwise.<\/li>\n<li>It cannot learn interactions between features.<\/li>\n<\/ul>\n<h2><strong><b>Applications of Naive Bayes<\/b><\/strong><\/h2>\n<ul>\n<li><b><\/b><strong><b>Spam filtering. <\/b><\/strong>Classifying email from word evidence.<\/li>\n<li><b><\/b><strong><b>Sentiment analysis. <\/b><\/strong>Deciding whether a review is positive or negative.<\/li>\n<li><b><\/b><strong><b>Document and news classification. <\/b><\/strong>Sorting text by topic.<\/li>\n<li><b><\/b><strong><b>Recommendation and ranking. <\/b><\/strong>Quick first stage classifiers.<\/li>\n<li><b><\/b><strong><b>Medical screening. <\/b><\/strong>Combining test results into a risk estimate.<\/li>\n<li><b><\/b><strong><b>Real time prediction. <\/b><\/strong>The speed suits systems that must answer instantly.<\/li>\n<\/ul>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/EGbyBqFlTcQ\" width=\"850\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h2><strong><b>Common Mistakes to Avoid<\/b><\/strong><\/h2>\n<ul>\n<li><b><\/b><strong><b>Saying all features have equal weight. <\/b><\/strong>The assumption is independence, not equal weight.<\/li>\n<li><b><\/b><strong><b>Trusting the probabilities. <\/b><\/strong>Use the predicted class, and calibrate if you need real probabilities.<\/li>\n<li><b><\/b><strong><b>Forgetting smoothing. <\/b><\/strong>One unseen value can zero out a prediction.<\/li>\n<li><b><\/b><strong><b>Using the wrong variant. <\/b><\/strong>Use Gaussian for continuous values, Multinomial for counts and Bernoulli for yes or no.<\/li>\n<li><b><\/b><strong><b>Evaluating on the training data. <\/b><\/strong>Always check accuracy on a held out test set.<\/li>\n<li><b><\/b><strong><b>Pasting code with curly quotes. <\/b><\/strong>Paste into a plain editor first.<\/li>\n<\/ul>\n<p>Naive Bayes proves that a simple idea can go a long way. Work through the weather table by hand once, run the Python program, and then try a text dataset with MultinomialNB. Treat it as the baseline that every fancier model has to beat.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Naive Bayes Classifier in Machine Learning Every day your email inbox decides, in a fraction of a second, whether a message is spam. It does not read the message the way you do. It looks at the words, asks how often each one appears in spam compared with normal mail, and combines those clues into [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,1696],"tags":[1753,1755,458,1754],"class_list":["post-4834","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-machine-learning-modules","tag-a-gentle-introduction-to-naive-bayes-for-machine-learning","tag-machine-learning-naive-bayes-classifier-with-python-examples","tag-naive-bayes-classifier-in-machine-learning","tag-understanding-naive-bayes-classifiers-advantages-and-applications"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Naive Bayes Classifier in Machine Learning Explained<\/title>\n<meta name=\"description\" content=\"Understand Bayes theorem and the naive independence assumption, work through a Naive Bayes example by hand, then build a Gaussian classifier in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Naive Bayes Classifier in Machine Learning Explained\" \/>\n<meta property=\"og:description\" content=\"Understand Bayes theorem and the naive independence assumption, work through a Naive Bayes example by hand, then build a Gaussian classifier in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-14T12:20:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-15T15:15:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png\" \/>\n\t<meta property=\"og:image:width\" content=\"266\" \/>\n\t<meta property=\"og:image:height\" content=\"158\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/\",\"url\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/\",\"name\":\"Naive Bayes Classifier in Machine Learning Explained\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png\",\"datePublished\":\"2026-10-14T12:20:25+00:00\",\"dateModified\":\"2026-10-15T15:15:07+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Understand Bayes theorem and the naive independence assumption, work through a Naive Bayes example by hand, then build a Gaussian classifier in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png\",\"width\":266,\"height\":158},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Naive Bayes Classifier in Machine Learning\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Naive Bayes Classifier in Machine Learning Explained","description":"Understand Bayes theorem and the naive independence assumption, work through a Naive Bayes example by hand, then build a Gaussian classifier in Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/","og_locale":"en_US","og_type":"article","og_title":"Naive Bayes Classifier in Machine Learning Explained","og_description":"Understand Bayes theorem and the naive independence assumption, work through a Naive Bayes example by hand, then build a Gaussian classifier in Python.","og_url":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-14T12:20:25+00:00","article_modified_time":"2026-10-15T15:15:07+00:00","og_image":[{"width":266,"height":158,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png","type":"image\/png"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/","url":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/","name":"Naive Bayes Classifier in Machine Learning Explained","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png","datePublished":"2026-10-14T12:20:25+00:00","dateModified":"2026-10-15T15:15:07+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Understand Bayes theorem and the naive independence assumption, work through a Naive Bayes example by hand, then build a Gaussian classifier in Python.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2020\/02\/NB7.png","width":266,"height":158},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/machine-learning\/machine-learning-modules\/naive-bayes-classifier-in-machine-learning\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Naive Bayes Classifier in Machine Learning"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=4834"}],"version-history":[{"count":11,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4834\/revisions"}],"predecessor-version":[{"id":11999,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/4834\/revisions\/11999"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=4834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=4834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=4834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}