문제

Update: I am using e1071 package for naiveBayes

I am new to R and trying to build Naive Bayes model around a toy data. Then I tried to call"predcit" on that model. The issue I saw is: the result from "predict()" has zero length. Please see the simple R repro code. Thanks for your inputs!

 df<-NULL

 df <- rbind(df, c(0,3))

 df <- rbind(df, c(1,1))

 df <- rbind(df, c(1,3))

 model <- naiveBayes(df[,2], df[,1])

 prediction <- predict(model, df[,-1])

 length(prediction)

 ## [1] 0
도움이 되었습니까?

해결책

The problem appears to be that the dependent variable is expected to be a factor. Instead of using a matrix to store the data, I'll use a data frame (df below) which can store multiple variable types (e.g. numerics and factors). I store into df a factor Y, and a numeric X and run the model...

df<-data.frame(Y=factor(c(0,1,1)),X=c(3,1,3))
model<-naiveBayes(Y~X,df)
predict(model,df)

Alternatively, to show that it's the factor that fixed the problem (i.e. not the use of a formula)...

model<-naiveBayes(df[,2],df[,1])
predict(model,df)

Still works.

다른 팁

I think the issue arises from the fact that naiveBayes assumes that y is a categorical variable.

In your example data, there are no (obvious) categorical data or contigency table data.

If we take the example from the help, using iris, the fifth column is Species and is a factor variable.

library(e1071)
data(iris)
m <- naiveBayes(iris[,-5], iris[,5])
m
table(predict(m, iris), iris[,5])


            setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         47         3
  virginica       0          3        47

It works as expected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top