2014-11-10 6 views
1

저는 베이지안 네트워크 용 라이브러리를 사용하고 있으며 pbnt (https://github.com/thejinxters/pbnt) 모듈을 사용하여 베이 즈넷 질병 예측기를 만들려고합니다. 코드를 실행하려고 할 때 속성 오류가 발생합니다. python - import pbnt (bayes net module) 및 AttributeError 얻기

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] 

는 추론 예를

을 실행하고 난 다음 무엇입니까 :

Traceback (most recent call last): File "assign4.py", line 133, in main(sys.argv[1:]) File "assign4.py", line 130, in main disease_inference()
File "assign4.py", line 94, in disease_inference disease_net = EX.create_disease_net() AttributeError: 'module' object has no attribute 'create_disease_net'

내가 초보자 프로그래머 오전하지만 더 얻을 노력하고 있어요. 어떤 통찰력이라도 대단히 감사하겠습니다.

답변

1

라인 : ExampleModels 실제로이 오류가 발생합니다 create_disease_net 최고 수준의 방법을 가지고 있지 않는 오류 메시지에서 알 수 있듯이

import ExampleModels as EX 

ExampleModels의 전체를 만들 것입니다 그래서 이름 EX 있습니다. create_disease_net 네임 스페이스 대신 EX으로 정의 했으므로 호출에서 EX. 만 제거하면됩니다.

+0

감사합니다. – fourcockatiels