我重新问这个问题(同名)Multinomial Naive Bayes Classifier 。这个问题似乎已经接受了一个答案,我认为这个答案要么是错误的,要么我需要更多解释,因为我仍然不明白。
到目前为止,我在 R 中看到的每个朴素贝叶斯分类器(包括 bnlearn 和 klaR )都有假设特征具有高斯似然的实现。
R 中是否有使用多项似然的朴素贝叶斯分类器的实现(类似于 scikit-learn's MultinomialNB )?
特别是——如果事实证明在这些模块中存在某种调用naive.bayes
的方法,以便用多项分布来估计可能性——我真的很感激一个例子这是怎么做到的。我搜索过示例,但没有找到任何示例。例如:这就是 klaR.NaiveBayes
中 usekernal
参数的用途吗?
最佳答案
我不知道 predict
方法在 naive.bayes
模型上调用什么算法,但您可以根据条件概率表(mle 估计)自行计算预测
# You may need to get dependencies of gRain from here
# source("http://bioconductor.org/biocLite.R")
# biocLite("RBGL")
library(bnlearn)
library(gRain)
使用naive.bayes
帮助页面中的第一个示例
data(learning.test)
# fit model
bn <- naive.bayes(learning.test, "A")
# look at cpt's
fit <- bn.fit(bn, learning.test)
# check that the cpt's (proportions) are the mle of the multinomial dist.
# Node A:
all.equal(prop.table(table(learning.test$A)), fit$A$prob)
# Node B:
all.equal(prop.table(table(learning.test$B, learning.test$A),2), fit$B$prob)
# look at predictions - include probabilities
pred <- predict(bn, learning.test, prob=TRUE)
pr <- data.frame(t(attributes(pred)$prob))
pr <- cbind(pred, pr)
head(pr, 2)
# preds a b c
# 1 c 0.29990442 0.33609392 0.36400165
# 2 a 0.80321241 0.17406706 0.02272053
通过运行查询来计算 cpt 的预测概率 - 使用“gRain”
# query using junction tree- algorithm
jj <- compile(as.grain(fit))
# Get ptredicted probs for first observation
net1 <- setEvidence(jj, nodes=c("B", "C", "D", "E", "F"),
states=c("c", "b", "a", "b", "b"))
querygrain(net1, nodes="A", type="marginal")
# $A
# A
# a b c
# 0.3001765 0.3368022 0.3630213
# Get ptredicted probs for secondobservation
net2 <- setEvidence(jj, nodes=c("B", "C", "D", "E", "F"),
states=c("a", "c", "a", "b", "b"))
querygrain(net2, nodes="A", type="marginal")
# $A
# A
# a b c
# 0.80311043 0.17425364 0.02263593
因此,这些概率非常接近您从 bnlearn
获得的概率,并且是使用 MLE 计算的,
关于r - R 中的多项式朴素贝叶斯分类器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816994/