Blame view

students/amartins/tarefas/tarefa2.py 1.17 KB
3e0f9b8a   Francisco Coelho   back to work?
1
2
from pgmpy.readwrite import BIFReader
import networkx as nx
505cdc43   Francisco Coelho   long due comeback
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
from bninput import *

def get_yes_probabilities(model):
    nodes = model.nodes()

    yes_probabilities = {}

    for node in nodes:
        cpd = model.get_cpds(node)
        state_names = cpd.state_names[node]
        yes_index = state_names.index("yes")
        yes_prob = cpd.values[yes_index]
        yes_probabilities[node] = yes_prob

    return yes_probabilities

if __name__ == "__main__":
    summary = summary_dag("asia2.bif")
3e0f9b8a   Francisco Coelho   back to work?
23
24
25
26
27
28
29
30
31
    model = summary["bnmodel"]
    probabilities = get_yes_probabilities(model)
    for node, yes_prob in probabilities.items():
        parents = model.get_parents(node)
        s = ""
        if len(parents) == 0:
            s += str(yes_prob) + " :: " + str(node)
            print(s)
        elif len(parents) == 1:
505cdc43   Francisco Coelho   long due comeback
32
            s += str(yes_prob[0]) + " :: " + str(node) + " <- " + str(parents) + "\n"
3e0f9b8a   Francisco Coelho   back to work?
33
            s += str(yes_prob[1]) + " :: " + str(node) + " <- -" + str(parents) + "\n"
505cdc43   Francisco Coelho   long due comeback
34
35
36
37
            print(s)
        else:
            print("Node:", node)
            print("Probability of 'yes':", yes_prob)
3e0f9b8a   Francisco Coelho   back to work?
38
            print()
505cdc43   Francisco Coelho   long due comeback

353a8086   Francisco Coelho   alterações feitas...

3e0f9b8a   Francisco Coelho   back to work?

505cdc43   Francisco Coelho   long due comeback

353a8086   Francisco Coelho   alterações feitas...

3e0f9b8a   Francisco Coelho   back to work?