使用朴素贝叶来预测新值

时间:2016-04-08 08:17:42

标签: r naivebayes

我有一个看起来像这样的数据框

weather <- c("good", "good", "good", "bad", "bad", "good")
temp <- c("high", "low", "low", "high", "low", "low")
golf <- c("yes", "no", "yes", "no", "yes" , "no")
df <- data.frame(weather, temp, golf)

我现在想做的是使用朴素贝叶斯方法来获得这个新数据集的概率

df_new <- data.frame(weather = "good", temp = "low")

因此我尝试

library(e1071)
model <- naiveBayes(golf ~.,data=df)
predict(model, df_new)

但是这给了我:

NO

任何想法我怎么能把它变成概率?

1 个答案:

答案 0 :(得分:5)

如果您使用type = "raw"

,则会返回

概率

predict(model, df_new, type = "raw")
no yes
[1,] 0.5 0.5

predict(model, df_new, type = "class")
[1] no
Levels: no yes