首页 文章

R中的多项式朴素贝叶斯分类器

提问于
浏览
1

我再问这个问题(同名)Multinomial Naive Bayes Classifier . 这个问题似乎已经接受了一个答案,我认为这个答案是错误的,或者我不理解 .

到目前为止,我在R中看到的每个Naive Bayes分类器(包括bnlearnklaR)都有假设这些特征具有高斯可能性的实现 .

在R中是否存在使用多项式可能性(类似于scikit-learn's MultinomialNB)的朴素贝叶斯分类器的实现?

特别是 - 如果事实证明有一些方法在这些模块中的任何一个中调用 naive.bayes ,那么用多项分布估计可能性 - 我真的很感激's done. I' ve如何搜索示例并且没有找到任何 . 例如:这是 usekernal 中的 usekernal 参数是什么?

1 回答

  • 1

    我不知道 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计算,

相关问题