Naive bayes in R

前端 未结 1 1592
悲&欢浪女
悲&欢浪女 2021-02-04 21:20

I am getting an error while running naive bayes classifier in R. I am using the following code-

mod1 <- naiveBayes(factor(X20) ~ factor(X1) + factor(X2) +fact         


        
相关标签:
1条回答
  • 2021-02-04 21:41

    You seem to be using the e1071::naiveBayes algorithm, which expects a newdata argument for prediction, hence the two errors raised when running your code. (You can check the source code of the predict.naiveBayes function on CRAN; the second line in the code is expecting a newdata, as newdata <- as.data.frame(newdata).) Also as pointed out by @Vincent, you're better off converting your variables to factor before calling the NB algorithm, although this has certainly nothing to do with the above errors.

    Using NaiveBayes from the klar package, no such problem would happen. E.g.,

    data(spam, package="ElemStatLearn")
    library(klaR)
    
    # set up a training sample
    train.ind <- sample(1:nrow(spam), ceiling(nrow(spam)*2/3), replace=FALSE)
    
    # apply NB classifier
    nb.res <- NaiveBayes(spam ~ ., data=spam[train.ind,])
    
    # predict on holdout units
    nb.pred <- predict(nb.res, spam[-train.ind,])
    
    # but this also works on the training sample, i.e. without using a `newdata`
    head(predict(nb.res))
    
    0 讨论(0)
提交回复
热议问题