{"id":2364,"date":"2022-04-17T06:05:56","date_gmt":"2022-04-17T06:05:56","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=2364"},"modified":"2022-04-17T06:05:56","modified_gmt":"2022-04-17T06:05:56","slug":"naive-bayes-classifier-algorithm","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/04\/17\/naive-bayes-classifier-algorithm\/","title":{"rendered":"Na\u00efve Bayes Classifier Algorithm"},"content":{"rendered":"\n<ul><li>Na\u00efve Bayes algorithm is a supervised learning algorithm, which is based on&nbsp;<strong>Bayes theorem<\/strong>&nbsp;and used for solving classification problems.<\/li><li>It is mainly used in&nbsp;<em>text classification<\/em>&nbsp;that includes a high-dimensional training dataset.<\/li><li>Na\u00efve Bayes Classifier is one of the simple and most effective Classification algorithms which helps in building the fast machine learning models that can make quick predictions.<\/li><li><strong>It is a probabilistic classifier, which means it predicts on the basis of the probability of an object<\/strong>.<\/li><li>Some popular examples of Na\u00efve Bayes Algorithm are&nbsp;<strong>spam filtration, Sentimental analysis, and classifying articles<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why is it called Na\u00efve Bayes?<\/h2>\n\n\n\n<p>The Na\u00efve Bayes algorithm is comprised of two words Na\u00efve and Bayes, Which can be described as:<\/p>\n\n\n\n<ul><li><strong>Na\u00efve<\/strong>: It is called Na\u00efve because it assumes that the occurrence of a certain feature is independent of the occurrence of other features. Such as if the fruit is identified on the bases of color, shape, and taste, then red, spherical, and sweet fruit is recognized as an apple. Hence each feature individually contributes to identify that it is an apple without depending on each other.<\/li><li><strong>Bayes<\/strong>: It is called Bayes because it depends on the principle of\u00a0Bayes&#8217; Theorem.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Bayes&#8217; Theorem:<\/h2>\n\n\n\n<ul><li>Bayes&#8217; theorem is also known as&nbsp;<strong>Bayes&#8217; Rule<\/strong>&nbsp;or&nbsp;<strong>Bayes&#8217; law<\/strong>, which is used to determine the probability of a hypothesis with prior knowledge. It depends on the conditional probability.<\/li><li>The formula for Bayes&#8217; theorem is given as:<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/machine-learning\/images\/naive-bayes-classifier-algorithm.png\" alt=\"Na\u00efve Bayes Classifier Algorithm\"\/><\/figure>\n\n\n\n<p><strong>Where,<\/strong><\/p>\n\n\n\n<p><strong>P(A|B) is Posterior probability<\/strong>: Probability of hypothesis A on the observed event B.<\/p>\n\n\n\n<p><strong>P(B|A) is Likelihood probability<\/strong>: Probability of the evidence given that the probability of a hypothesis is true.<\/p>\n\n\n\n<p><strong>P(A) is Prior Probability<\/strong>: Probability of hypothesis before observing the evidence.<\/p>\n\n\n\n<p><strong>P(B) is Marginal Probability<\/strong>: Probability of Evidence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working of Na\u00efve Bayes&#8217; Classifier:<\/h2>\n\n\n\n<p>Working of Na\u00efve Bayes&#8217; Classifier can be understood with the help of the below example:<\/p>\n\n\n\n<p>Suppose we have a dataset of&nbsp;<strong>weather conditions<\/strong>&nbsp;and corresponding target variable &#8220;<strong>Play<\/strong>&#8220;. So using this dataset we need to decide that whether we should play or not on a particular day according to the weather conditions. So to solve this problem, we need to follow the below steps:<\/p>\n\n\n\n<ol><li>Convert the given dataset into frequency tables.<\/li><li>Generate Likelihood table by finding the probabilities of given features.<\/li><li>Now, use Bayes theorem to calculate the posterior probability.<\/li><\/ol>\n\n\n\n<p><strong>Problem<\/strong>: If the weather is sunny, then the Player should play or not?<\/p>\n\n\n\n<p><strong>Solution<\/strong>: To solve this, first consider the below dataset:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><th><\/th><th>Outlook<\/th><th>Play<\/th><\/tr><tr><td><strong>0<\/strong><\/td><td>Rainy<\/td><td>Yes<\/td><\/tr><tr><td><strong>1<\/strong><\/td><td>Sunny<\/td><td>Yes<\/td><\/tr><tr><td><strong>2<\/strong><\/td><td>Overcast<\/td><td>Yes<\/td><\/tr><tr><td><strong>3<\/strong><\/td><td>Overcast<\/td><td>Yes<\/td><\/tr><tr><td><strong>4<\/strong><\/td><td>Sunny<\/td><td>No<\/td><\/tr><tr><td><strong>5<\/strong><\/td><td>Rainy<\/td><td>Yes<\/td><\/tr><tr><td><strong>6<\/strong><\/td><td>Sunny<\/td><td>Yes<\/td><\/tr><tr><td><strong>7<\/strong><\/td><td>Overcast<\/td><td>Yes<\/td><\/tr><tr><td><strong>8<\/strong><\/td><td>Rainy<\/td><td>No<\/td><\/tr><tr><td><strong>9<\/strong><\/td><td>Sunny<\/td><td>No<\/td><\/tr><tr><td><strong>10<\/strong><\/td><td>Sunny<\/td><td>Yes<\/td><\/tr><tr><td><strong>11<\/strong><\/td><td>Rainy<\/td><td>No<\/td><\/tr><tr><td><strong>12<\/strong><\/td><td>Overcast<\/td><td>Yes<\/td><\/tr><tr><td><strong>13<\/strong><\/td><td>Overcast<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Frequency table for the Weather Conditions:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>Weather<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Overcast<\/td><td>5<\/td><td>0<\/td><\/tr><tr><td>Rainy<\/td><td>2<\/td><td>2<\/td><\/tr><tr><td>Sunny<\/td><td>3<\/td><td>2<\/td><\/tr><tr><td>Total<\/td><td>10<\/td><td>5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Likelihood table weather condition:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>Weather<\/td><td>No<\/td><td>Yes<\/td><td><\/td><\/tr><tr><td>Overcast<\/td><td>0<\/td><td>5<\/td><td>5\/14= 0.35<\/td><\/tr><tr><td>Rainy<\/td><td>2<\/td><td>2<\/td><td>4\/14=0.29<\/td><\/tr><tr><td>Sunny<\/td><td>2<\/td><td>3<\/td><td>5\/14=0.35<\/td><\/tr><tr><td>All<\/td><td>4\/14=0.29<\/td><td>10\/14=0.71<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Applying Bayes&#8217;theorem:<\/strong><\/p>\n\n\n\n<p><strong>P(Yes|Sunny)= P(Sunny|Yes)*P(Yes)\/P(Sunny)<\/strong><\/p>\n\n\n\n<p>P(Sunny|Yes)= 3\/10= 0.3<\/p>\n\n\n\n<p>P(Sunny)= 0.35<\/p>\n\n\n\n<p>P(Yes)=0.71<\/p>\n\n\n\n<p>So P(Yes|Sunny) = 0.3*0.71\/0.35=&nbsp;<strong>0.60<\/strong><\/p>\n\n\n\n<p><strong>P(No|Sunny)= P(Sunny|No)*P(No)\/P(Sunny)<\/strong><\/p>\n\n\n\n<p>P(Sunny|NO)= 2\/4=0.5<\/p>\n\n\n\n<p>P(No)= 0.29<\/p>\n\n\n\n<p>P(Sunny)= 0.35<\/p>\n\n\n\n<p>So P(No|Sunny)= 0.5*0.29\/0.35 =&nbsp;<strong>0.41<\/strong><\/p>\n\n\n\n<p>So as we can see from the above calculation that&nbsp;<strong>P(Yes|Sunny)&gt;P(No|Sunny)<\/strong><\/p>\n\n\n\n<p><strong>Hence on a Sunny day, Player can play the game.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages of Na\u00efve Bayes Classifier:<\/h3>\n\n\n\n<ul><li>Na\u00efve Bayes is one of the fast and easy ML algorithms to predict a class of datasets.<\/li><li>It can be used for Binary as well as Multi-class Classifications.<\/li><li>It performs well in Multi-class predictions as compared to the other Algorithms.<\/li><li>It is the most popular choice for&nbsp;<strong>text classification problems<\/strong>.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Disadvantages of Na\u00efve Bayes Classifier:<\/h3>\n\n\n\n<ul><li>Naive Bayes assumes that all features are independent or unrelated, so it cannot learn the relationship between features.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Applications of Na\u00efve Bayes Classifier:<\/h3>\n\n\n\n<ul><li>It is used for&nbsp;<strong>Credit Scoring<\/strong>.<\/li><li>It is used in&nbsp;<strong>medical data classification<\/strong>.<\/li><li>It can be used in&nbsp;<strong>real-time predictions<\/strong>&nbsp;because Na\u00efve Bayes Classifier is an eager learner.<\/li><li>It is used in Text classification such as&nbsp;<strong>Spam filtering<\/strong>&nbsp;and&nbsp;<strong>Sentiment analysis<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Na\u00efve Bayes Model:<\/h2>\n\n\n\n<p>There are three types of Naive Bayes Model, which are given below:<\/p>\n\n\n\n<ul><li><strong>Gaussian<\/strong>: The Gaussian model assumes that features follow a normal distribution. This means if predictors take continuous values instead of discrete, then the model assumes that these values are sampled from the Gaussian distribution.<\/li><li><strong>Multinomial<\/strong>: The Multinomial Na\u00efve Bayes classifier is used when the data is multinomial distributed. It is primarily used for document classification problems, it means a particular document belongs to which category such as Sports, Politics, education, etc.<br>The classifier uses the frequency of words for the predictors.<\/li><li><strong>Bernoulli<\/strong>: The Bernoulli classifier works similar to the Multinomial classifier, but the predictor variables are the independent Booleans variables. Such as if a particular word is present or not in a document. This model is also famous for document classification tasks.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Python Implementation of the Na\u00efve Bayes algorithm:<\/h2>\n\n\n\n<p>Now we will implement a Naive Bayes Algorithm using Python. So for this, we will use the &#8220;<strong>user_data<\/strong>&#8221;&nbsp;<strong>dataset<\/strong>, which we have used in our other classification model. Therefore we can easily compare the Naive Bayes model with the other models.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to implement:<\/h3>\n\n\n\n<ul><li>Data Pre-processing step<\/li><li>Fitting Naive Bayes to the Training set<\/li><li>Predicting the test result<\/li><li>Test accuracy of the result(Creation of Confusion matrix)<\/li><li>Visualizing the test set result.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1) Data Pre-processing step:<\/h3>\n\n\n\n<p>In this step, we will pre-process\/prepare the data so that we can use it efficiently in our code. It is similar as we did in&nbsp;<a href=\"https:\/\/www.javatpoint.com\/data-preprocessing-machine-learning\" target=\"_blank\" rel=\"noreferrer noopener\">data-pre-processing<\/a>. The code for this is given below:<a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><\/p>\n\n\n\n<ol><li>Importing&nbsp;the&nbsp;libraries&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;numpy&nbsp;as&nbsp;nm&nbsp;&nbsp;<\/li><li><strong>import<\/strong>&nbsp;matplotlib.pyplot&nbsp;as&nbsp;mtp&nbsp;&nbsp;<\/li><li class=\"\"><strong>import<\/strong>&nbsp;pandas&nbsp;as&nbsp;pd&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;<\/li><li class=\"\">#&nbsp;Importing&nbsp;the&nbsp;dataset&nbsp;&nbsp;<\/li><li>dataset&nbsp;=&nbsp;pd.read_csv(&#8216;user_data.csv&#8217;)&nbsp;&nbsp;<\/li><li class=\"\">x&nbsp;=&nbsp;dataset.iloc[:,&nbsp;[2,&nbsp;3]].values&nbsp;&nbsp;<\/li><li>y&nbsp;=&nbsp;dataset.iloc[:,&nbsp;4].values&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;<\/li><li>#&nbsp;Splitting&nbsp;the&nbsp;dataset&nbsp;into&nbsp;the&nbsp;Training&nbsp;set&nbsp;and&nbsp;Test&nbsp;set&nbsp;&nbsp;<\/li><li class=\"\"><strong>from<\/strong>&nbsp;sklearn.model_selection&nbsp;<strong>import<\/strong>&nbsp;train_test_split&nbsp;&nbsp;<\/li><li>x_train,&nbsp;x_test,&nbsp;y_train,&nbsp;y_test&nbsp;=&nbsp;train_test_split(x,&nbsp;y,&nbsp;test_size&nbsp;=&nbsp;0.25,&nbsp;random_state&nbsp;=&nbsp;0)&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;<\/li><li>#&nbsp;Feature&nbsp;Scaling&nbsp;&nbsp;<\/li><li class=\"\"><strong>from<\/strong>&nbsp;sklearn.preprocessing&nbsp;<strong>import<\/strong>&nbsp;StandardScaler&nbsp;&nbsp;<\/li><li>sc&nbsp;=&nbsp;StandardScaler()&nbsp;&nbsp;<\/li><li class=\"\">x_train&nbsp;=&nbsp;sc.fit_transform(x_train)&nbsp;&nbsp;<\/li><li>x_test&nbsp;=&nbsp;sc.transform(x_test)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>In the above code, we have loaded the dataset into our program using &#8220;<strong>dataset = pd.read_csv(&#8216;user_data.csv&#8217;)<\/strong>. The loaded dataset is divided into training and test set, and then we have scaled the feature variable.<\/p>\n\n\n\n<p>The output for the dataset is given as:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/machine-learning\/images\/naive-bayes-classifier-algorithm-1.png\" alt=\"Na\u00efve Bayes Classifier Algorithm 1\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2) Fitting Naive Bayes to the Training Set:<\/h3>\n\n\n\n<p>After the pre-processing step, now we will fit the Naive Bayes model to the Training set. Below is the code for it:<a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Fitting&nbsp;Naive&nbsp;Bayes&nbsp;to&nbsp;the&nbsp;Training&nbsp;set&nbsp;&nbsp;<\/li><li class=\"\"><strong>from<\/strong>&nbsp;sklearn.naive_bayes&nbsp;<strong>import<\/strong>&nbsp;GaussianNB&nbsp;&nbsp;<\/li><li>classifier&nbsp;=&nbsp;GaussianNB()&nbsp;&nbsp;<\/li><li class=\"\">classifier.fit(x_train,&nbsp;y_train)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p>In the above code, we have used the&nbsp;<strong>GaussianNB classifier<\/strong>&nbsp;to fit it to the training dataset. We can also use other classifiers as per our requirement.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Out[6]: GaussianNB(priors=None, var_smoothing=1e-09)\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) Prediction of the test set result:<\/h3>\n\n\n\n<p>Now we will predict the test set result. For this, we will create a new predictor variable&nbsp;<strong>y_pred<\/strong>, and will use the predict function to make the predictions.<a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Predicting&nbsp;the&nbsp;Test&nbsp;set&nbsp;results&nbsp;&nbsp;<\/li><li class=\"\">y_pred&nbsp;=&nbsp;classifier.predict(x_test)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/machine-learning\/images\/naive-bayes-classifier-algorithm-2.png\" alt=\"Na\u00efve Bayes Classifier Algorithm 2\"\/><\/figure>\n\n\n\n<p>The above output shows the result for prediction vector&nbsp;<strong>y_pred<\/strong>&nbsp;and real vector y_test. We can see that some predications are different from the real values, which are the incorrect predictions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4) Creating Confusion Matrix:<\/h3>\n\n\n\n<p>Now we will check the accuracy of the Naive Bayes classifier using the Confusion matrix. Below is the code for it:<a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Making&nbsp;the&nbsp;Confusion&nbsp;Matrix&nbsp;&nbsp;<\/li><li class=\"\"><strong>from<\/strong>&nbsp;sklearn.metrics&nbsp;<strong>import<\/strong>&nbsp;confusion_matrix&nbsp;&nbsp;<\/li><li>cm&nbsp;=&nbsp;confusion_matrix(y_test,&nbsp;y_pred)&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/machine-learning\/images\/naive-bayes-classifier-algorithm-3.png\" alt=\"Na\u00efve Bayes Classifier Algorithm 3\"\/><\/figure>\n\n\n\n<p>As we can see in the above confusion matrix output, there are 7+3= 10 incorrect predictions, and 65+25=90 correct predictions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5) Visualizing the training set result:<\/h3>\n\n\n\n<p>Next we will visualize the training set result using Na\u00efve Bayes Classifier. Below is the code for it:<a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Visualising&nbsp;the&nbsp;Training&nbsp;set&nbsp;results&nbsp;&nbsp;<\/li><li class=\"\"><strong>from<\/strong>&nbsp;matplotlib.colors&nbsp;<strong>import<\/strong>&nbsp;ListedColormap&nbsp;&nbsp;<\/li><li>x_set,&nbsp;y_set&nbsp;=&nbsp;x_train,&nbsp;y_train&nbsp;&nbsp;<\/li><li class=\"\">X1,&nbsp;X2&nbsp;=&nbsp;nm.meshgrid(nm.arange(start&nbsp;=&nbsp;x_set[:,&nbsp;0].min()&nbsp;&#8211;&nbsp;1,&nbsp;stop&nbsp;=&nbsp;x_set[:,&nbsp;0].max()&nbsp;+&nbsp;1,&nbsp;step&nbsp;=&nbsp;0.01),&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nm.arange(start&nbsp;=&nbsp;x_set[:,&nbsp;1].min()&nbsp;&#8211;&nbsp;1,&nbsp;stop&nbsp;=&nbsp;x_set[:,&nbsp;1].max()&nbsp;+&nbsp;1,&nbsp;step&nbsp;=&nbsp;0.01))&nbsp;&nbsp;<\/li><li class=\"\">mtp.contourf(X1,&nbsp;X2,&nbsp;classifier.predict(nm.array([X1.ravel(),&nbsp;X2.ravel()]).T).reshape(X1.shape),&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alpha&nbsp;=&nbsp;0.75,&nbsp;cmap&nbsp;=&nbsp;ListedColormap((&#8216;purple&#8217;,&nbsp;&#8216;green&#8217;)))&nbsp;&nbsp;<\/li><li class=\"\">mtp.xlim(X1.min(),&nbsp;X1.max())&nbsp;&nbsp;<\/li><li>mtp.ylim(X2.min(),&nbsp;X2.max())&nbsp;&nbsp;<\/li><li class=\"\"><strong>for<\/strong>&nbsp;i,&nbsp;j&nbsp;<strong>in<\/strong>&nbsp;enumerate(nm.unique(y_set)):&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;mtp.scatter(x_set[y_set&nbsp;==&nbsp;j,&nbsp;0],&nbsp;x_set[y_set&nbsp;==&nbsp;j,&nbsp;1],&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;ListedColormap((&#8216;purple&#8217;,&nbsp;&#8216;green&#8217;))(i),&nbsp;label&nbsp;=&nbsp;j)&nbsp;&nbsp;<\/li><li>mtp.title(&#8216;Naive&nbsp;Bayes&nbsp;(Training&nbsp;set)&#8217;)&nbsp;&nbsp;<\/li><li class=\"\">mtp.xlabel(&#8216;Age&#8217;)&nbsp;&nbsp;<\/li><li>mtp.ylabel(&#8216;Estimated&nbsp;Salary&#8217;)&nbsp;&nbsp;<\/li><li class=\"\">mtp.legend()&nbsp;&nbsp;<\/li><li>mtp.show()&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/machine-learning\/images\/naive-bayes-classifier-algorithm-4.png\" alt=\"Na\u00efve Bayes Classifier Algorithm 4\"\/><\/figure>\n\n\n\n<p>In the above output we can see that the Na\u00efve Bayes classifier has segregated the data points with the fine boundary. It is Gaussian curve as we have used&nbsp;<strong>GaussianNB<\/strong>&nbsp;classifier in our code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6) Visualizing the Test set result:<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/machine-learning-naive-bayes-classifier#\"><\/a><\/p>\n\n\n\n<ol><li>#&nbsp;Visualising&nbsp;the&nbsp;Test&nbsp;set&nbsp;results&nbsp;&nbsp;<\/li><li class=\"\"><strong>from<\/strong>&nbsp;matplotlib.colors&nbsp;<strong>import<\/strong>&nbsp;ListedColormap&nbsp;&nbsp;<\/li><li>x_set,&nbsp;y_set&nbsp;=&nbsp;x_test,&nbsp;y_test&nbsp;&nbsp;<\/li><li class=\"\">X1,&nbsp;X2&nbsp;=&nbsp;nm.meshgrid(nm.arange(start&nbsp;=&nbsp;x_set[:,&nbsp;0].min()&nbsp;&#8211;&nbsp;1,&nbsp;stop&nbsp;=&nbsp;x_set[:,&nbsp;0].max()&nbsp;+&nbsp;1,&nbsp;step&nbsp;=&nbsp;0.01),&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nm.arange(start&nbsp;=&nbsp;x_set[:,&nbsp;1].min()&nbsp;&#8211;&nbsp;1,&nbsp;stop&nbsp;=&nbsp;x_set[:,&nbsp;1].max()&nbsp;+&nbsp;1,&nbsp;step&nbsp;=&nbsp;0.01))&nbsp;&nbsp;<\/li><li class=\"\">mtp.contourf(X1,&nbsp;X2,&nbsp;classifier.predict(nm.array([X1.ravel(),&nbsp;X2.ravel()]).T).reshape(X1.shape),&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alpha&nbsp;=&nbsp;0.75,&nbsp;cmap&nbsp;=&nbsp;ListedColormap((&#8216;purple&#8217;,&nbsp;&#8216;green&#8217;)))&nbsp;&nbsp;<\/li><li class=\"\">mtp.xlim(X1.min(),&nbsp;X1.max())&nbsp;&nbsp;<\/li><li>mtp.ylim(X2.min(),&nbsp;X2.max())&nbsp;&nbsp;<\/li><li class=\"\"><strong>for<\/strong>&nbsp;i,&nbsp;j&nbsp;<strong>in<\/strong>&nbsp;enumerate(nm.unique(y_set)):&nbsp;&nbsp;<\/li><li>&nbsp;&nbsp;&nbsp;&nbsp;mtp.scatter(x_set[y_set&nbsp;==&nbsp;j,&nbsp;0],&nbsp;x_set[y_set&nbsp;==&nbsp;j,&nbsp;1],&nbsp;&nbsp;<\/li><li class=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;ListedColormap((&#8216;purple&#8217;,&nbsp;&#8216;green&#8217;))(i),&nbsp;label&nbsp;=&nbsp;j)&nbsp;&nbsp;<\/li><li>mtp.title(&#8216;Naive&nbsp;Bayes&nbsp;(test&nbsp;set)&#8217;)&nbsp;&nbsp;<\/li><li class=\"\">mtp.xlabel(&#8216;Age&#8217;)&nbsp;&nbsp;<\/li><li>mtp.ylabel(&#8216;Estimated&nbsp;Salary&#8217;)&nbsp;&nbsp;<\/li><li class=\"\">mtp.legend()&nbsp;&nbsp;<\/li><li>mtp.show()&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/machine-learning\/images\/naive-bayes-classifier-algorithm-5.png\" alt=\"Na\u00efve Bayes Classifier Algorithm 5\"\/><\/figure>\n\n\n\n<p>The above output is final output for test set data. As we can see the classifier has created a Gaussian curve to divide the &#8220;purchased&#8221; and &#8220;not purchased&#8221; variables. There are some wrong predictions which we have calculated in Confusion matrix. But still it is pretty good classifier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Na\u00efve Bayes algorithm is a supervised learning algorithm, which is based on&nbsp;Bayes theorem&nbsp;and used for solving classification problems. It is mainly used in&nbsp;text classification&nbsp;that includes a high-dimensional training dataset. Na\u00efve Bayes Classifier is one of the simple and most effective Classification algorithms which helps in building the fast machine learning models that can make quick [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[828],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2364"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=2364"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/2364\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=2364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=2364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=2364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}