使用 R 朴素贝叶斯 e1702

using R naive bayes e1702

Is this code correct?
library(e1701) ## Categorical data only:
data(HouseVotes84, package = "mlbench")
model <-
naiveBayes(Class ~ ., data = HouseVotes84)
a<-c("n","y","n","y","n","n","y","y","n","n")
names(a)<-c("V1","V2","V3","V4","V5","V6","V7","V8","V9","V10")
pred<-predict(model,a)
tab<-table(pred,a)
sum(tab[row(tab)==col(tab)])/sum(tab)

我想使用模型根据投票记录进行预测

很难确切知道您的意图,但您似乎想根据 V1:V10 的价值观来预测这位立法者的党派 (Class)。如果是这样,那么这就是您想要的:

library(e1071) 
data(HouseVotes84, package = "mlbench")
model <- naiveBayes(Class ~ ., data = HouseVotes84)
a <- data.frame(matrix(c("n","y","n","y","n","n","y","y","n","n"), nrow = 1))
names(a) <- c("V1","V2","V3","V4","V5","V6","V7","V8","V9","V10")
(pred <- predict(model, a))
# [1] democrat
# Levels: democrat republican
(pred <- predict(model, a, type = "raw"))
#       democrat republican
# [1,] 0.9277703  0.0722297

您提供的代码有两个错误:第一,您没有正确加载包含 naiveBayes() 的包,因为名称实际上是 e1071;其次,您没有向 predict() 中的 newdata 提供正确的项目。这需要一个 data.frame,并且您为其提供一个向量,该向量在此处被视为 10 个观察值,每个观察值提供一个特征:第一个为 V1,第二个为 V2,等等。nativeBayes() 没有不关心你是否提供了一个不完整的功能列表,所以它仍然有效:

> pred
 [1] democrat democrat democrat democrat democrat democrat democrat democrat democrat democrat
Levels: democrat republican
> (pred <- predict(model,a, type = "raw"))
       democrat republican
 [1,] 0.6137931  0.3862069
 [2,] 0.6137931  0.3862069
 [3,] 0.6137931  0.3862069
 [4,] 0.6137931  0.3862069
 [5,] 0.6137931  0.3862069
 [6,] 0.6137931  0.3862069
 [7,] 0.6137931  0.3862069
 [8,] 0.6137931  0.3862069
 [9,] 0.6137931  0.3862069
[10,] 0.6137931  0.3862069

但是这里你得到十个没有信息的预测,因为你只有一个特征来预测每个。这就是预测匹配对应于先验的原因,因为您几乎没有数据进行更新:

# prior class probabilities (with no model)
> prop.table(table(HouseVotes84$Class))

  democrat republican 
 0.6137931  0.3862069 

在上面更正后的代码中,使用更多特征来预测 Class 这个新的(单次)观察结果以及十个投票特征的数据,我们更有信心地预测这位立法者是民主党人,因为后验概率基于更多数据更新先验 class 概率 0.61 和 0.39.