title: How to do Naive Bayes modelling (using sklearn MultinomialNB) in python
body: <p>I am currently learning how to do Naive Bayes modelling and attempting to apply it in python and R however, using a toy example, I am struggling to recreate the same numbers in python that I get from doing the calculations in either R or by hand. </p>
  
  <p>Help in figuring out why I am getting different numbers would be appreciated! </p>
  
  <p>The toy data is </p>
  
  <pre><code>Class (y)   A A A A  B B B B B B
    var x1    2 1 1 0  0 1 1 0 0 0
    var x2    0 0 1 0  0 1 1 1 1 1  
  </code></pre>
  
  <p>That is to say my dependent variable y has 2 levels  A &amp; B,  explanatory variable x1 has 3 levels  0,1,2  and x2 has two levels 0 &amp; 1.</p>
  
  <p>My current objective is to predict, using a multinomial naivebayes model, the class probabilities of a new data point with values x1=1 &amp; x2=1.</p>
  
  <p>My current python code is:</p>
  
  <pre><code>import pandas as pd
  from sklearn.naive_bayes import MultinomialNB
  
  dat = pd.DataFrame({
      "class" : ["A", "A","A","A", "B","B","B","B","B","B"],
      "x1" : [2,1,1,0,0,1,1,0,0,0],
      "x2" : [0,0,1,0,1,0,1,1,1,1]
  })
  
  mnb = MultinomialNB(alpha= 0)
  x = mnb.fit(dat[["x1","x2"]], dat["class"])
  x.predict_proba(  pd.DataFrame( [[1,1]] , columns=["x1","x2"])   )
  
  ## Out[160]: array([[ 0.34325744,  0.65674256]])
  </code></pre>
  
  <p>However attempting the same in R I get:</p>
  
  <pre><code>library(dplyr)
  library(e1071)    
  
  dat = data_frame(
      "class" = c("A", "A","A","A", "B","B","B","B","B","B"),
      "x1" = c(2,1,1,0,0,1,1,0,0,0),
      "x2" = c(0,0,1,0,1,0,1,1,1,1)
  )
  
  model &lt;- naiveBayes(class ~ . , data = table(dat) )
  
  predict(
      model, 
      newdata = data_frame(
          x1 = factor(1, levels = c(0,1,2)) ,
          x2 = factor(1, levels = c(0,1))),  
      type = "raw"
  )
  ##             A         B
  ## [1,] 0.2307692 0.7692308
  </code></pre>
  
  <p>And by hand I get the following:  </p>
  
  <p>The model is </p>
  
  <p><a href="https://i.stack.imgur.com/Y1RxI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y1RxI.png" alt="enter image description here"></a>  </p>
  
  <p>From the data we get the following probability estimates </p>
  
  <p><a href="https://i.stack.imgur.com/ZPnNK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZPnNK.png" alt="enter image description here"></a>  </p>
  
  <p>Thus plugging the numbers in we get  </p>
  
  <p><a href="https://i.stack.imgur.com/3qjYM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3qjYM.png" alt="enter image description here"></a>    </p>
  
  <p>Which matches the results from R. So again I'm confused as to what I am doing wrong in the python example. Any help would be appreciated. </p>