我正在使用贝叶斯网络库,并尝试使用名为pbnt(https://github.com/thejinxters/pbnt)的模块创建贝叶斯网络疾病预测器。尝试执行代码时出现属性错误。
import sys
from numpy import *
import ExampleModels as EX
sys.path.append('../lib')
from pbnt.Graph import *
from pbnt.Distribution import *
from pbnt.Node import *
from pbnt.Inference import *
#bayes network using pbnt library
def create_disease_net():
number_of_nodes = 5
pollution = 0
smoker = 1
cancer = 2
xray = 3
dyspnoea = 4
#events are pollution(high or low), smoker(true or false)
# cancer(true or false), xray(positive or negative), dyspnoea(true or false)
pollution_node = Node.BayesNode(0,1, name="pollution")
smoker_node = Node.BayesNode(1,1, name="smoker")
cancer_node = Node.BayesNode(2,4, name="cancer")
xray_node = Node.BayesNode(3,1, name="xray")
dyspnoea_node = Node.BayesNode(4,1, name="dyspnoea")
#pollution and smoker are parents of cancer node
#pollution and smoker are independent
pollution_node.add_child(cancer_node)
smoker_node.add_child(cancer_node)
cancer_node.add_parent(pollution_node)
cancer_node.add_parent(smoker_node)
#cancer node is parent of xray and dyspnoea nodes
cancer_node.add_child(xray_node)
cancer_node.add_child(dyspnoea_node)
#are xray and dyspnoea independent??????
xray_node.add_parent(cancer_node)
dyspnoea_node.add_parent(cancer_node)
nodes = [pollution_node, smoker_node, cancer_node, xray_node, dyspnoea_node]
#create distributions
#pollution distribution
pollution_distribution = DiscreteDistribution(pollution_node)
index = pollution_distribution.generate_index([],[])
pollution_distribution[index] = 0.9 #pollution = low
pollution_node.set_dist(pollution_distribution)
#smoker distribution
smoker_distribution = DiscreteDistribution(smoker_node)
index = smoker_distribution.generate_index([],[])
smoker_distribution[index] = 0.3 #smoker = true
smoker_node.set_dist(smoker_distribution)
#cancer conditional distribution (cancer given pollution and smoker)
dist = zeros([pollution_node.size(),smoker_node.size(),cancer_node.size()], dtype=float32)
dist[0,1,] = [0.05,0.95]
dist[0,0,] = [0.02,0.98]
dist[1,1,] = [0.03,0.97]
dist[1,0,] = [0.001,0.999]
cancer_distribution = ConditionalDiscreteDistribution(nodes=[pollution_node, smoker_node, cancer_node], table=dist)
cancer_node.set_dist(cancer_distribution)
#xray conditional distribution (xray given cancer)
dist = zeros([cancer_node.size(), xray_node.size()], dtype=float32)
dist[0,] = [0.2,0.8]
dist[1,] = [0.9,0.1]
xray_distribution = ConditionalDiscreteDistribution(nodes=[cancer_node, xray_node], table=dist)
xray_node.set_dist(xray_distribution)
#dyspnoea conditional distribution (dyspnoea given cancer)
dist = zeros([cancer_node.size(), dyspnoea_node.size()], dtype=float32)
dist[0,] = [0.2,0.8]
dist[1,] = [0.9,0.1]
dyspnoea_distribution = ConditionalDiscreteDistribution(nodes=[cancer_node, dyspnoea_node], table=dist)
dyspnoea_node.set_dist(dyspnoea_distribution)
#create bayes net
bnet = BayesNet(nodes)
return bnet
def disease_inference():
""" This is an example of how to perform inference on a network using the Junction Tree Engine. The exact same method could be used with any implemented inference engine by simply replaceing the line JunctionTreeEngine(water) with the appropriate constructor.
"""
#define disease network
disease_net = EX.create_disease_net()
#define variable indexes
for node in disease_net.nodes:
if node.id == 0:
pollution = node
if node.id == 1:
smoker = node
if node.id == 2:
cancer = node
if node.id == 3:
xray = node
if node.id == 4:
dyspnoea = node
#Create the inference engine object
engine = JunctionTreeEngine(disease_net)
test0 = 1
#Compute the marginal probability of sprinkler given no evidence
Q = engine.marginal(cancer)[0]
#Q is a DiscreteDistribution, and so to index into it, we have to use the class' method to create an index
index = Q.generate_index([False], range(Q.nDims))
print "The marginal probability of cancer=false:", Q[index]
#Set cloudy and rain to False and True in the evidence
engine.evidence[pollution] = False
engine.evidence[smoker] = True
#Compute the marginal probability given the evidence pollution=False, cancer=true
Q = engine.marginal(cancer)[0]
index = Q.generate_index([False],range(Q.nDims))
print "The marginal probability of cancer=false | pollution=False, smoker=True:", Q[index]
我得到以下内容:
追踪(最近一次通话): 文件" assign4.py",第133行,in 主(sys.argv中[1:]) 文件" assign4.py",第130行,在main中 disease_inference()
在疾病参考中归档" assign4.py",第94行 disease_net = EX.create_disease_net() AttributeError:'模块'对象没有属性' create_disease_net'
我是初学者程序员,但我想要变得更好。非常感谢任何见解。
答案 0 :(得分:1)
该行:
import ExampleModels as EX
将使整个ExampleModels
的名称为EX
,因此错误消息建议除非ExampleModels
实际上有顶级方法create_disease_net
,否则您将收到此错误。由于您要在 命名空间而不是create_disease_net
中定义EX
,因此您只需要从通话中删除EX.
。