我不知道是什麼算法naive.bayes
車型predict
方法調用,但你可以從自己的條件概率表計算的預測(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
從中華映管通過運行查詢
計算預測概率 - 用「糧食」
# 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的計算,
第一個例子在'bnlearn'鏈接(learning.test)具有離散變量的底部。要查看條件概率表,請使用'bn.fit(bn,learning.test)' – user20650
感謝user20650。我看到naive.bayes可以處理離散或繼續的數據。我的問題是:特徵估計的可能性如何?它在文檔中說它假定它們是高斯的。有沒有辦法改變這個? – gabe
我沒有看過預測是如何計算的,但我預計他們是使用CPT計算的 - 這是多項分佈的mle。我已經加了一個小的前 - 也許有幫助 – user20650