{"id":3565,"date":"2024-09-02T20:55:10","date_gmt":"2024-09-02T20:55:10","guid":{"rendered":"https:\/\/workhouse.sweetdishy.com\/?p=3565"},"modified":"2024-09-02T20:55:11","modified_gmt":"2024-09-02T20:55:11","slug":"naive-bayes-classification","status":"publish","type":"post","link":"https:\/\/workhouse.sweetdishy.com\/index.php\/2024\/09\/02\/naive-bayes-classification\/","title":{"rendered":"\u00a0Na\u00efve Bayes Classification"},"content":{"rendered":"\n<p id=\"Arti00002046\">Data science has progressed from simple linear regression models to complex techniques but practitioners still prefer the models that are simple and easy to interpret. In this widely used category of algorithms Na\u00efve Bayes algorithm is one of the prominent names as it is not only simple but so powerful that it outperforms complex algorithms for very large datasets.<\/p>\n\n\n\n<p id=\"Arti00002047\"><em>Naive Bayes is a probabilistic machine learning algorithm based on the Bayes Theorem. This algorithm is extensively used in a wide range of classification tasks varying from<\/em>&nbsp;filtering spam, classifying documents to predicting sentiments.<\/p>\n\n\n\n<p id=\"Arti00002048\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00002050\"><strong>Programming Tip<\/strong>: The tern na\u00efve means that features that are used in the model are independent of each other. So when one feature change, the other is not affected.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002051\"><a><\/a><strong>2.6.1 Understanding Conditional Probability<\/strong><\/h4>\n\n\n\n<p id=\"Arti00002052\">To understand conditional probability, consider an example of flipping a coin. There are equal chances of getting either heads or tails. So, the probability of getting heads is 50%.<\/p>\n\n\n\n<p id=\"Arti00002053\">Now, in case we are supposed to find the probability of getting a queen spade from a deck of 52 cards then the denominator is 13 (the eligible population) and not 52. Since there is only one queen in 13 spades, the probability that we get a queen spade is 1\/13.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p114-1.png\" alt=\"images\" width=\"268\" height=\"160\"><\/p>\n\n\n\n<p id=\"Arti00002056\"><a><\/a><strong>FIGURE 2.27<\/strong>&nbsp;Sample Dataset<\/p>\n\n\n\n<p id=\"Arti00002057\">Therefore, we can say that the conditional probability of A given B, denotes the probability of occurring A given that B has already occurred. Mathematically, it can be expressed as, P(A|B) = P(A AND B) \/ P(B).<\/p>\n\n\n\n<p id=\"Arti00002058\">Let\u2019s apply this concept on the data set given in\u00a0Fig. 2.27\u00a0and calculate the conditional probability that the teacher is a male.<\/p>\n\n\n\n<p id=\"Arti00002059\">So, the required conditional probability P(Teacher | Male) = 12 \/ 60 = 0.2.<\/p>\n\n\n\n<p id=\"Arti00002060\">&nbsp;<\/p>\n\n\n\n<p id=\"Arti00002062\"><strong>Programming Tip:<\/strong>&nbsp;If Y has two categories, then calculate the probability of each class of Y and select the one with higher value.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p114-2.png\" alt=\"images\"\/><\/figure>\n\n\n\n<p id=\"Arti00002065\">Likewise, the conditional probability of B given A can be computed as,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p114-3.png\" alt=\"images\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p115-1.png\" alt=\"images\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002070\"><a><\/a><strong>2.6.2 The Bayes Rule<\/strong><\/h4>\n\n\n\n<p id=\"Arti00002071\">The Bayes Rule uses P(X|Y), known from the training dataset, to find P(Y|X). For observations in test dataset, X would be known while Y is unknown. And for each row of the test dataset, we have to find the probability of Y given that X has already happened (refer\u00a0Fig. 2.28).<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/img_p115-2.png\" alt=\"images\" width=\"410\" height=\"265\"><\/p>\n\n\n\n<p id=\"Arti00002074\"><a><\/a><strong>FIGURE 2.28<\/strong>&nbsp;Bayes Rule for Conditional Probability<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002075\"><a><\/a><strong>2.6.3 Types of Events<\/strong><\/h4>\n\n\n\n<p id=\"Arti00002076\">If two probabilities, P(B) and P(B|A) are the same, then it means that the occurrence of event A had no effect on event B. Therefore, event A and event B are&nbsp;<em>independent events<\/em>.<\/p>\n\n\n\n<p id=\"Arti00002077\">If the conditional probability is zero, then it means that the occurrence of event A implies that event B cannot occur. If the reverse is also true, then the events are said to be mutually exclusive events. In such a case, only one event can take place at a time.<\/p>\n\n\n\n<p id=\"Arti00002078\">All other cases are classified as dependent events where the conditional probability can be either lower or higher than the original.<\/p>\n\n\n\n<p id=\"Arti00002079\">For example, if we toss a coin twice and we want to calculate the probability of getting a head both times, then we must consider that the second event is independent of the first. The desired probability is P(Heads in first throw)<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/star.png\" alt=\"images\" width=\"7\" height=\"7\">P(Heads in second throw)= \u00bd X \u00bd = \u00bc<\/p>\n\n\n\n<p id=\"Arti00002081\">Mathematically, we can say that if the events are not independent, we multiply the probability of one event with the probability of the second event.<\/p>\n\n\n\n<p id=\"Arti00002082\">For expressing dependent events mathematically, we multiply the probability of any one event with the probability of the second event after the first has happened.<\/p>\n\n\n\n<p id=\"Arti00002083\">P(A and B)=P(A)<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/star.png\" alt=\"images\" width=\"7\" height=\"7\">P(B|A) or P(A and B)=P(B)<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learning.oreilly.com\/api\/v2\/epubs\/urn:orm:book:9789357053778\/files\/images\/star.png\" alt=\"images\" width=\"7\" height=\"7\">P(A|B)<\/p>\n\n\n\n<p id=\"Arti00002086\"><a><\/a>For example, while drawing two cards (King and Queen) without replacement, the probability of the first event is dependent of 52 cards whereas the probability of the second event is dependent on 51 cards.<\/p>\n\n\n\n<p id=\"Arti00002087\">This means that&nbsp;<strong>P(King and Queen)=P(Queen)P(King|Queen)<\/strong><\/p>\n\n\n\n<p id=\"Arti00002089\">So if P(Queen) is 4\/52 then once the King is drawn, P(King|Queen) is 4\/51 as now only 51 cards are left. Therefore, the desired probability = 4\/52 X 4\/51<\/p>\n\n\n\n<p id=\"Arti00002090\">The probability of two non-mutually exclusive events is given as,&nbsp;<strong>P(A OR B)=P(A) + P(B) &#8211; P(A AND B)<\/strong>.<\/p>\n\n\n\n<p id=\"Arti00002091\">For example, in a throw of dice, the probability of getting a number that is multiple of 2 or 3 is a scenario of events which are not mutually exclusive since 6 is both a multiple of 2 and 3 and is counted twice. Therefore,&nbsp;<strong>P(multiple of 2 or 3) = P(Multiple of 2) + P(Multiple of 3) &#8211; P(Multiple of 2 AND 3) = P(2, 4, 6) + P(3, 6) &#8211; P(6) = 3\/6 + 2\/6 -1\/6 = 4\/6 = 2\/3<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002092\"><a><\/a><strong>2.6.4 Naive Bayes Algorithm<\/strong><\/h4>\n\n\n\n<p id=\"Arti00002093\">In real-world problems, there are multiple X variables. So, when the features are independent, the Bayes Rule can be extended to what is called Naive Bayes. It is called \u2018Naive\u2019 because of the naive assumption that the Xs are independent of each other.<\/p>\n\n\n\n<p id=\"Arti00002094\">Naive Bayes classification algorithm can be used with continuous features but performs best for categorical variables. When dealing with numeric features, the algorithm assumes that numerical values are normally distributed.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002095\"><a><\/a><strong>2.6.5 Laplace Correction<\/strong><\/h4>\n\n\n\n<p id=\"Arti00002096\">We can better understand the importance of Laplace correction with an example. If we were to classify fruits as mango, orange or banana, then the value of P(Long | Orange) will be zero as there are no \u2018Long\u2019 oranges.<\/p>\n\n\n\n<p id=\"Arti00002097\">When we use this probability in the model having multiple features, then the entire probability will become zero as anything multiplied by zero is zero. To avoid this, we need to increase the count of the variable with zero to a small value (usually 1) in the numerator, so that the overall probability does not become zero.<\/p>\n\n\n\n<p id=\"Arti00002098\">This correction is called \u2018Laplace Correction and the function for building the Naive Bayes model will accept this correction as a parameter.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002099\"><a><\/a><strong>2.6.6 Pros and Cons of Naive Bayes Algorithm<\/strong><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002100\"><strong>Pros<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\" id=\"Arti00002101\">\n<li>It is a simple algorithm that is easy to build and understand.<\/li>\n\n\n\n<li>It predicts classes faster than many other classification algorithms.<\/li>\n\n\n\n<li>It can be easily trained using a small data set.<\/li>\n\n\n\n<li>It can be used to predict values for even large data sets.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002106\"><a><\/a><strong>Cons<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\" id=\"Arti00002107\">\n<li>If a given class and a feature has 0 frequency, then the conditional probability will become 0 resulting in the \u2018Zero Conditional Probability Problem.\u2019 This is a serious issue as it wipes out all other vital information about probabilities. However, to avoid such situations there are several sample correction techniques like the \u2018Laplacian Correction.\u2019<\/li>\n\n\n\n<li>Another disadvantage is the very strong assumption of independence class features that it makes. It is near to impossible to find such data sets in real life.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Arti00002110\"><a><\/a><strong>2.6.7 Applications<\/strong><\/h4>\n\n\n\n<p id=\"Arti00002111\">Some of the real-world scenarios where Naive Bayes algorithm is used include the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"Arti00002112\">\n<li><strong>Text classification<\/strong>: Na\u00efve Bayes classification algorithm is one of the most preferred algorithms to classify whether a text document belongs to one or more categories (classes).<\/li>\n\n\n\n<li><strong>Spam filtration<\/strong>: Many popular email services uses the Bayesian spam filtering to distinguish spam email from legitimate email. Many server-side email filters like DSPAM, SpamBayes, SpamAssassin, Bogofilter, and ASSP are all based on Na\u00efve Bayes classification technique.\n<ul class=\"wp-block-list\">\n<li><strong>Sentiment Analysis<\/strong>: The Na\u00efve Bayes algorithm is also used to analyse the analyse tweets, comments, and reviews posted on social networking site to classify as being negative, positive or neutral.<\/li>\n\n\n\n<li><strong>Recommendation System<\/strong>: Companies uses recommendation systems to predict whether a particular user will like their product and\/ or buy their product or not. For this, the Naive Bayes algorithm is used in conjunction with collaborative filtering techniques to build hybrid recommendation systems.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Data science has progressed from simple linear regression models to complex techniques but practitioners still prefer the models that are simple and easy to interpret. In this widely used category of algorithms Na\u00efve Bayes algorithm is one of the prominent names as it is not only simple but so powerful that it outperforms complex algorithms [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3333,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[452],"tags":[],"class_list":["post-3565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence-technologies"],"jetpack_featured_media_url":"https:\/\/workhouse.sweetdishy.com\/wp-content\/uploads\/2024\/08\/artificial-intelligence.png","_links":{"self":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/comments?post=3565"}],"version-history":[{"count":1,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3565\/revisions"}],"predecessor-version":[{"id":3566,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/posts\/3565\/revisions\/3566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/media\/3333"}],"wp:attachment":[{"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/media?parent=3565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/categories?post=3565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/workhouse.sweetdishy.com\/index.php\/wp-json\/wp\/v2\/tags?post=3565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}