【问题标题】:Error in bn.fit predict function in bnlear Rbnlear R中的bn.fit预测函数错误
【发布时间】:2015-08-16 09:39:37
【问题描述】:

我已经在 bnlearn R 包中学习并安装了贝叶斯网络,我希望预测它的“事件”节点值。

fl="data/discrete_kdd_10.txt"
h=TRUE
dtbl1 = read.csv(file=fl, head=h, sep=",")
net=hc(dtbl1)
fitted=bn.fit(net,dtbl1)

我想根据存储在另一个文件中的证据来预测“事件”节点的值,该文件与用于学习的文件具有相同的结构。

fileName="data/dcmp.txt"
dtbl2 = read.csv(file=fileName, head=h, sep=",")
predict(fitted,"event",dtbl2)

但是,预测失败 Error in check.data(data) : variable duration must have at least two levels.

我不明白为什么要对证据 data.frame 中的变量级别数有任何限制。

dtbl2 data.frame 仅包含几行,每行对应我想要预测“事件”值的每个场景。

我知道我可以使用 cpquery,但我希望将 predict 函数也用于具有混合变量(离散和连续)的网络。我还没有发现如何在 cpqery 中使用连续变量的证据。

有人可以解释一下我在使用predict 函数时做错了什么,我应该如何做对? 提前致谢!

【问题讨论】:

    标签: r bayesian bayesian-networks


    【解决方案1】:

    问题是在读取证据data.frame

    fileName="data/dcmp.txt"
    dtbl2 = read.csv(file=fileName, head=h, sep=",")
    predict(fitted,"event",dtbl2)
    

    导致分类变量成为具有不同级别数的因子(原始训练集的级别子集)。

    我使用以下代码解决了这个问题。

    for(i in 1:dim(dtbl2)[2]){
      dtbl2[[i]] = factor(dtbl2[[i]],levels = levels(dtbl1[[i]]))
    }
    

    顺便说一下,bnlearn 包确实适合具有混合变量的模型,并且还提供了用于预测的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2017-01-24
      • 2018-01-09
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多