<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/naive-bayes-classification-in-r-part-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://sw23993.wordpress.com/2017/02/17/naive-bayes-classification-in-r-part-1/"> Environmental Science and Data Analytics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Introduction</h2>
<p>A very useful machine learning method which, for its simplicity, is incredibly successful in many real world applications is <em>the Naive Bayes classifier</em>. I am currently taking a machine learning module as part of my data science college course and this week’s practical work involved a classification problem using the Naive Bayes method. I thought that I’d write a two-part piece about the project and the concepts of probability in order to consolidate my own understanding as well to share my method with others who may find it useful. If the reader wishes to get straight into the Bayes classification problem, please see Part 2 <a target="_blank" rel="nofollow" href="https://wordpress.com/post/sw23993.wordpress.com/1197">here</a>.</p>
<p>The dataset used in the classification problem is titled “Breast cancer data” and is sourced from Matjaz Zwitter and Milan Soklic from the Institute of Oncology, University Medical Center in Ljubljana, Slovenia (formerly Yugoslavia). It is an interesting dataset which contains categorical data on two classes with nine attributes. The dataset can be found <a target="_blank" rel="nofollow" href="https://archive.ics.uci.edu/ml/datasets/Breast+Cancer">here</a>. The two outcome classes are “no-recurrence events” and “recurrence events” which refer to whether breast cancer returned to a patient or not having previously being diagnosed  with the disease and treated for it.</p>
<p>Our goal was to use these data to build a Naive Bayes classification model which could predict with good accuracy whether a patient diagnosed with breast cancer is likely to experience a recurrence of the disease based on the attributes. First, I will briefly discuss the basic concepts of probability before moving on to the classification problem in Part 2.</p>
<h2>Fundamentals of Probability</h2>
<p>Probability boils down to working out proportions and performing mathematical operations on them such as addition, multiplication and division. The fact that Naive Bayes classification has been so successful given this simple foundation is truly remarkable. There are a number of rules and concepts involved in probability calculations which one should be aware of in order to understand how the Naive Bayes classifier works. We will discuss these now but first I want to explain independent events, dependent events and mutual exclusiveness.</p>
<p><em>Independent events</em> are those which, upon occurrence, do not affect the probability of another event occurring. An example would be Brazil winning the football World Cup and a hurricane forming over the Atlantic Ocean in September. <em>Dependent</em> events are those which, upon occurrence, affect the probability of another event occurring (i.e. they are linked in some way). An example would be getting a cold and going to work the following day. The probability of you going to work can be affected by the occurrence of a cold as you don’t feel very well and would like to not infect your colleagues. When two or more events are <em>mutually exclusive</em>, they cannot occur simultaneously. What is the probability of rolling a 6 and a 3 on a single dice roll? Zero. It is impossible. These two outcomes are mutually exclusive.</p>
<p>To calculate the probability of a single event occurring, sometimes called <em>the observational probability</em>, you just take the number of times the event occurred and divide it by the total number of processes which occurred that could have lead to that event. For example, let’s say I sprint 100 metres fifty times over the course of a month. I want to know the probability of my time being below 10 seconds based on the previous sprints. If I ran sub-10 second 100 metres on 27 occasions, then the observational probability of me running a sub-10 second 100 metres is simply:</p>
<p>P(&lt;10 second 100 m) = (27 / 50) = 0.54 (54%)</p>
<p>Therefore, I can expect to sprint 100 metres in under 10 seconds about half of the time based on records of the fifty completed sprints. Please note that the P() wrapper is used to denote the probability of some outcome and is used throughout these posts.</p>
<h3>Multiplication Rules for AND Events</h3>
<p>When calculating the probability of two or more events occurring simultaneously, we first consider whether the events are independent or dependent. If they are independent we can use the <em>simple multiplication rule</em>:</p>
<p>P(outcome 1 <strong>AND</strong> outcome 2) = P(outcome 1) * P(outcome 2)</p>
<p>If I were to calculate the probability of Brazil winning the football World Cup and a hurricane forming over the Atlantic Ocean in September, I would use this simple multiplication rule. The two events are independent as the occurrence of one does not affect the other’s chance of occurring. If Brazil have a probability of winning the World Cup of 41% and the probability of a hurricane over the Atlantic Ocean in September is 91%, then we calculate the probability of both occurring:</p>
<p>P(Brazil <strong>AND</strong> Hurricane) = P(Brazil) * P(Hurricane)</p>
<p>= (0.41) * (0.91)</p>
<p>= 0.3731 (37%)</p>
<p>If two or more events are dependent, however, we must use the <em>general multiplication rule. </em>This formula is always valid, in both cases of independent and dependent events, as we will see.</p>
<p>P(outcome 1 <strong>AND</strong> outcome 2) = P(outcome 1) * P(outcome 2 | outcome 1)</p>
<p>P(outcome 2 | outcome 1) refers to the<em> conditional probability</em> of outcome 2 occurring given outcome 1 has already occurred. One can immediately see how this formula incorporates dependence between the events. If the events were independent, then the conditional probability is irrelevant as one outcome does not influence the chance of the other and P(outcome 2 | outcome 1) is simply P(outcome 2).  The formula just becomes the simple multiplication rule already described.</p>
<p>We can apply the general multiplication rule to a deck of cards example. What would be the probability of drawing a King of any suit and an Ace of any suit from a 52-card deck with just two draws? There are 4 Kings and 4 Aces in a standard deck so we know that drawing an Ace immediately affects our chances of drawing a King as there are fewer cards in the deck after the first draw. We can use the general multiplication formula as follows:</p>
<p>P(Ace <strong>AND</strong> King) = P(Ace) * P(King | Ace)</p>
<p>= (4 / 52) * (4 / 51)</p>
<p>= 0.006033183 (0.6%)</p>
<p>Obviously, if two events are mutually exclusive and cannot occur simultaneously, they are disjoint and the multiplication rules cannot be applied. The dice roll example describes such a scenario. The best we can do in such a case is state the probability of either outcome occurring. This brings us to the <em>the simple addition rule.</em><em><br>
</em></p>
<h3>Addition Rules for OR Events</h3>
<p>When calculating the probability of either one event or the other occurring, we use <em>the addition rule</em>. When the outcomes are mutually exclusive, we use the <em>simple addition</em> formula:</p>
<p>P(outcome 1 <strong>OR</strong> outcome 2) = P(outcome 1) + P(outcome 2)</p>
<p>Applied to the dice roll example, what is the probability of rolling a 6 or a 3? Both outcomes cannot occur simultaneously. The probability of rolling a 6 is (1 / 6) and the same can be said for rolling a 3. Therefore,</p>
<p>P(6 <strong>OR</strong> 3) = (1 / 6) + (1 / 6) = 0.33 (33%)</p>
<p>However, if the events are not mutually exclusive and can occur simultaneously, we must use the following <em>general addition</em> formula which is always valid in both cases of mutual exclusiveness and non-mutual exclusiveness.</p>
<p>P(outcome 1 <strong>OR</strong> outcome 2) = P(outcome 1) + P(outcome 2) – P(outcome 1 <strong>AND</strong> outcome 2)</p>
<p>Applied to the World Cup and hurricane example, this would mean calculating the probabilities of both outcomes as well as the probability of both occurring simultaneously.</p>
<p>P(Brazil) + P(Hurricane) – P(Brazil <strong>AND</strong> Hurricane)</p>
<p>We know that the outcomes are independent, the occurrence of one  does not affect the probability of the other occurring, and we can therefore use the <em>simple multiplication rule</em> for two simultaneous events to calculate P(Brazil <strong>AND</strong> Hurricane).</p>
<p>P(Brazil <strong>AND</strong> Hurricane) = (0.41) * (0.91)</p>
<p>= 0.3731 (37%)</p>
<p>Finally, we can plug this probability into the main formula to get our answer:</p>
<p>P(Brazil <strong>OR</strong> Hurricane) = (0.91) + (0.41) – (0.3731) = 0.9469 (95%)</p>
<p>This makes sense right? Brazil are indeed an awesome football team. However, what drives this probability up is the fact that September is one of the months in the Atlantic hurricane season and you are almost guaranteed to see a hurricane event.</p>
<h2>Summary</h2>
<p>That was a brief introduction to probability and the rules associated with it. I hope it is clear and easy to follow. The important rules to remember are the general forms of the multiplication rule and the addition rule as they are valid in all cases. Using them in the above examples in place of the simple rules still yields the same results. As such, it may be more useful to memorise these general forms. Here they are once more:</p>
<p>P(outcome 1 <strong>AND</strong> outcome 2) = P(outcome 1) * P(outcome 2 | outcome 1)</p>
<p>P(outcome 1 <strong>OR</strong> outcome 2) = P(outcome 1) + P(outcome 2) – P(outcome 1 <strong>AND</strong> outcome 2)</p>
<p>I have chosen to use “outcome 1” and “outcome 2” instead of the usual “A” and “B” format because I believe it easier to understand Naive Bayes when replacing the letters with meaningful words. Here is how the general rules are typically written:</p>
<p>P(A and B) = P(A) * P(B | A)</p>
<p>P(A or B) = P(A) + P(B) – P(A and B)</p>
<p>In Part 2 I describe how to implement the Naive Bayes classifier in R and explain how it works based on the fundamentals of probability outlined here.</p>
<p><em>Featured image By Dennis Hill from The OC, So. Cal. – misc 24, CC BY 2.0, <a target="_blank" rel="nofollow" href="https://commons.wikimedia.org/w/index.php?curid=54704175">https://commons.wikimedia.org/w/index.php?curid=54704175</a></em></p>
<p> </p>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sw23993.wordpress.com/693/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/sw23993.wordpress.com/693/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://sw23993.wordpress.com/2017/02/17/naive-bayes-classification-in-r-part-1/"> Environmental Science and Data Analytics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oW71xi2di7M"></a></div>