R中的朴素贝叶斯算法无法预测 - 因子(0) 级别:

11

我有一个数据集,看起来像这样:

data.flu <- data.frame(chills = c(1,1,1,0,0,0,0,1), runnyNose = c(0,1,0,1,0,1,1,1), headache = c("M", "N", "S", "M", "N", "S", "S", "M"), fever = c(1,0,1,1,0,1,0,1), flu = c(0,1,1,1,0,1,0,1) )
> data.flu
   chills runnyNose headache fever flu
1      1         0        M     1   0
2      1         1        N     0   1
3      1         0        S     1   1
4      0         1        M     1   1
5      0         0        N     0   0
6      0         1        S     1   1
7      0         1        S     0   0
8      1         1        M     1   1

> str(data.flu)
'data.frame':   8 obs. of  5 variables:
 $ chills   : num  1 1 1 0 0 0 0 1
 $ runnyNose: num  0 1 0 1 0 1 1 1
 $ headache : Factor w/ 3 levels "M","N","S": 1 2 3 1 2 3 3 1
 $ fever    : num  1 0 1 1 0 1 0 1
 $ flu      : num  0 1 1 1 0 1 0 1

为什么predict函数没有返回任何值?
# I can see the model has been successfully created.
model <- naiveBayes(flu~., data=data.flu)
# I created a new data 
patient <- data.frame(chills = c(1), runnyNose = c(0), headache = c("M"), fever = c(1))
> predict(model, patient)
factor(0)
Levels:
# I tried with the training data, still won't work
> predict(model, data.flu[,-5])
factor(0)
Levels:

我尝试按照naiveBayes帮助手册中的示例操作,它对我有效。我不确定我的方法有什么问题。非常感谢!

我认为在应用naivebayes模型之前可能存在数据类型的问题,我尝试使用as.factor将所有变量更改为因子,并且似乎对我有效。但是我仍然非常困惑背后的“如何”和“为什么”。

1个回答

31

问题不在predict()函数中,而在您的模型定义中。

naiveBayes()的帮助文件如下:

Computes the conditional a-posterior probabilities of a categorical class variable 
given independent predictor variables using the Bayes rule.

所以y值应该是分类的,但在你的情况下它们是数值型的。

解决方案是将flu转换为factor。

model <- naiveBayes(as.factor(flu)~., data=data.flu)
predict(model, patient)
[1] 1
Levels: 0 1

谢谢你的回答。我现在正在学习朴素贝叶斯,这个模型与我的手算结果相符。我想知道predict是如何确定y==1实际上具有更好的代价函数?代价函数在哪里,我如何在R中找到y=0和y=1的代价函数值? - B.Mr.W.
抱歉,我无法回答这个问题,因为我对朴素贝叶斯非常熟悉。 - Didzis Elferts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接