0
$\begingroup$

I'm working with the e1071:: naiveBayes() function, but I don't figure out how it works. My doubts arose when I read this post. I've posted this question on SO, but I think that probably it fits better here, so I deleted it from there.

It seems that the function works without checking if the column names in the testing dataset, are the same in the training dataset.

Here an example, I got two datasets (in the bottom of the post, dput() them) like those:

> trainNB
  com dot tiger jail and angry hungry birthday happy     sent
1   1   1     1    0   0     0      0        0     0 positive
2   0   0     0    1   0     0      0        0     0 negative
3   0   0     0    0   1     1      1        0     0 negative
4   0   0     0    0   0     0      0        1     1 positive
> testNB
  good spot very amazing place boring     sent
1    1    1    1       0     0      0 positive
2    0    0    0       1     1      0 positive
3    0    0    1       0     1      1 negative

As you can see, I'm trying sentiment analysis, I've clean up a bit the code, and I had this datasets of training and testing (respectively trainNB, testNB).

The columns are different, it means that there isn't any column with the same name in the two dataset.

So I tried the above mentioned function:

library(e1071)
classifier <- naiveBayes(as.factor(sent)~.,trainNB)
pred <- predict(classifier, newdata=testNB) 

And they both works (results are nonsense I suppose)! I'm concerned because I cannot figure out how it gives any kind of result, also because, in my real data, I got 95% of accuracy (bigger dataset).

In my real dataset some columns are in common, so I firstly thought that it selects the common ones( between train and test), as stated here, but if so, it should not work in my example posted here.

Here an interesting point, it seems you can predict via Bayes rule with a test dataset with only some of the columns in the training, and also if you give dimensions that are not in the training set. I do not know how.

However it seems that gives some constant probability to unknown data:

pred <- predict(classifier, newdata=testNB, 'raw') 
pred
     negative positive
[1,]      0.5      0.5
[2,]      0.5      0.5
[3,]      0.5      0.5

So the question is: why is it working, and how?

Thanks in advance.


Data:

trainNB <- structure(list(com = structure(c(`1` = 2L, `2` = 1L, `3` = 1L, 
`4` = 1L), .Label = c("0", "1"), class = "factor"), dot = structure(c(`1` = 2L, 
`2` = 1L, `3` = 1L, `4` = 1L), .Label = c("0", "1"), class = "factor"), 
    tiger = structure(c(`1` = 2L, `2` = 1L, `3` = 1L, `4` = 1L
    ), .Label = c("0", "1"), class = "factor"), jail = structure(c(`1` = 1L, 
    `2` = 2L, `3` = 1L, `4` = 1L), .Label = c("0", "1"), class = "factor"), 
    and = structure(c(`1` = 1L, `2` = 1L, `3` = 2L, `4` = 1L), .Label = c("0", 
    "1"), class = "factor"), angry = structure(c(`1` = 1L, `2` = 1L, 
    `3` = 2L, `4` = 1L), .Label = c("0", "1"), class = "factor"), 
    hungry = structure(c(`1` = 1L, `2` = 1L, `3` = 2L, `4` = 1L
    ), .Label = c("0", "1"), class = "factor"), birthday = structure(c(`1` = 1L, 
    `2` = 1L, `3` = 1L, `4` = 2L), .Label = c("0", "1"), class = "factor"), 
    happy = structure(c(`1` = 1L, `2` = 1L, `3` = 1L, `4` = 2L
    ), .Label = c("0", "1"), class = "factor"), sent = structure(c(2L, 
    1L, 1L, 2L), .Label = c("negative", "positive"), class = "factor")), row.names = c("1", 
"2", "3", "4"), class = "data.frame")

testNB <- structure(list(good = structure(c(`1` = 2L, `2` = 1L, `3` = 1L
), .Label = c("0", "1"), class = "factor"), spot = structure(c(`1` = 2L, 
`2` = 1L, `3` = 1L), .Label = c("0", "1"), class = "factor"), 
    very = structure(c(`1` = 2L, `2` = 1L, `3` = 2L), .Label = c("0", 
    "1"), class = "factor"), amazing = structure(c(`1` = 1L, 
    `2` = 2L, `3` = 1L), .Label = c("0", "1"), class = "factor"), 
    place = structure(c(`1` = 1L, `2` = 2L, `3` = 2L), .Label = c("0", 
    "1"), class = "factor"), boring = structure(c(`1` = 1L, `2` = 1L, 
    `3` = 2L), .Label = c("0", "1"), class = "factor"), sent = structure(c(2L, 
    2L, 1L), .Label = c("negative", "positive"), class = "factor")), row.names = c("1", 
"2", "3"), class = "data.frame")
$\endgroup$
  • $\begingroup$ @Peter Flom, I'm not asking a new dataset, I'm asking about this algorithm works, and why it is working when it -probably- should not. In case, if it's not clear, I'll gladly edit the question. $\endgroup$ – s_t Aug 26 '19 at 12:02
  • $\begingroup$ @Xi'an, I'm not asking a new dataset, I'm asking about this algorithm works, and why it is working when it -probably- should not. In case, if it's not clear, I'll gladly edit the question $\endgroup$ – s_t Aug 26 '19 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.