Commit e43d9d3be1437823c89e473cf09346b8d30fd947
1 parent
9d633d90
Exists in
master
AMartins | Tarefa 01 | 01
Showing
5 changed files
with
164 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,60 @@ |
| 1 | +network unknown { | |
| 2 | +} | |
| 3 | +variable asia { | |
| 4 | + type discrete [ 2 ] { yes, no }; | |
| 5 | +} | |
| 6 | +variable tub { | |
| 7 | + type discrete [ 2 ] { yes, no }; | |
| 8 | +} | |
| 9 | +variable smoke { | |
| 10 | + type discrete [ 2 ] { yes, no }; | |
| 11 | +} | |
| 12 | +variable lung { | |
| 13 | + type discrete [ 2 ] { yes, no }; | |
| 14 | +} | |
| 15 | +variable bronc { | |
| 16 | + type discrete [ 2 ] { yes, no }; | |
| 17 | +} | |
| 18 | +variable either { | |
| 19 | + type discrete [ 2 ] { yes, no }; | |
| 20 | +} | |
| 21 | +variable xray { | |
| 22 | + type discrete [ 2 ] { yes, no }; | |
| 23 | +} | |
| 24 | +variable dysp { | |
| 25 | + type discrete [ 2 ] { yes, no }; | |
| 26 | +} | |
| 27 | +probability ( asia ) { | |
| 28 | + table 0.01, 0.99; | |
| 29 | +} | |
| 30 | +probability ( tub | asia ) { | |
| 31 | + (yes) 0.05, 0.95; | |
| 32 | + (no) 0.01, 0.99; | |
| 33 | +} | |
| 34 | +probability ( smoke ) { | |
| 35 | + table 0.5, 0.5; | |
| 36 | +} | |
| 37 | +probability ( lung | smoke ) { | |
| 38 | + (yes) 0.1, 0.9; | |
| 39 | + (no) 0.01, 0.99; | |
| 40 | +} | |
| 41 | +probability ( bronc | smoke ) { | |
| 42 | + (yes) 0.6, 0.4; | |
| 43 | + (no) 0.3, 0.7; | |
| 44 | +} | |
| 45 | +probability ( either | lung, tub ) { | |
| 46 | + (yes, yes) 1.0, 0.0; | |
| 47 | + (no, yes) 1.0, 0.0; | |
| 48 | + (yes, no) 1.0, 0.0; | |
| 49 | + (no, no) 0.0, 1.0; | |
| 50 | +} | |
| 51 | +probability ( xray | either ) { | |
| 52 | + (yes) 0.98, 0.02; | |
| 53 | + (no) 0.05, 0.95; | |
| 54 | +} | |
| 55 | +probability ( dysp | bronc, either ) { | |
| 56 | + (yes, yes) 0.9, 0.1; | |
| 57 | + (no, yes) 0.7, 0.3; | |
| 58 | + (yes, no) 0.8, 0.2; | |
| 59 | + (no, no) 0.1, 0.9; | |
| 60 | +} | ... | ... |
32.7 KB
| ... | ... | @@ -0,0 +1,38 @@ |
| 1 | +import bnlearn as bn | |
| 2 | + | |
| 3 | +asia = bn.import_DAG("asia2.bif") | |
| 4 | + | |
| 5 | +model = asia['model'] # Cria um objeto BayesianModel | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | +nos = model.nodes() | |
| 10 | +print("Número de nós:", len(nos)) #resposta à pergunta "quantos nós tem a rede?" | |
| 11 | + | |
| 12 | +print("Lista de nós:", nos) | |
| 13 | + | |
| 14 | +asc = 0 | |
| 15 | +des = 0 | |
| 16 | +nos_in = 0 | |
| 17 | +nos_out = 0 | |
| 18 | + | |
| 19 | +for x in nos: | |
| 20 | + # Obtem os nós pais e filhos de cada nós | |
| 21 | + parents = model.get_parents(x) | |
| 22 | + children = model.get_children(x) | |
| 23 | + | |
| 24 | + if len(parents) != 0: | |
| 25 | + asc += 1 | |
| 26 | + if len(children) != 0: | |
| 27 | + des += 1 | |
| 28 | + nos_in += len(children) | |
| 29 | + nos_out += len(parents) | |
| 30 | + | |
| 31 | +#resposta à pergunta "quantos nós são descendentes? quantos são ascendentes?" | |
| 32 | +print("Nós ascendentes: ", asc) | |
| 33 | +print("Nós descendentes: ", des) | |
| 34 | +# resposta à pergunta "qual é o número médio de arestas "in"? e "out"?" | |
| 35 | +nos_in = nos_in/len(nos) | |
| 36 | +nos_out = nos_out/len(nos) | |
| 37 | +print("Número médio de arestas in: ", nos_in) | |
| 38 | +print("Número médio de arestas out: ", nos_out) | |
| 0 | 39 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,35 @@ |
| 1 | +import bnlearn as bn | |
| 2 | +import networkx as nx | |
| 3 | +import pydot | |
| 4 | +from networkx.drawing.nx_pydot import graphviz_layout | |
| 5 | + | |
| 6 | +asia = bn.import_DAG("asia2.bif") | |
| 7 | +model = asia['model'] | |
| 8 | + | |
| 9 | +# DiGraph do networkx | |
| 10 | +G = nx.DiGraph() | |
| 11 | + | |
| 12 | +for node in model.nodes: | |
| 13 | + G.add_node(node) | |
| 14 | + | |
| 15 | +for edge in model.edges: | |
| 16 | + G.add_edge(edge[0], edge[1]) | |
| 17 | + | |
| 18 | +# objeto pydot | |
| 19 | +dot = pydot.Dot(graph_type='digraph') | |
| 20 | + | |
| 21 | +for node in model.nodes: | |
| 22 | + dot.add_node(pydot.Node(node)) | |
| 23 | + | |
| 24 | +for edge in model.edges: | |
| 25 | + dot.add_edge(pydot.Edge(edge[0], edge[1])) | |
| 26 | + | |
| 27 | +# defina o layout do grafo | |
| 28 | +pos = graphviz_layout(G, prog='dot') | |
| 29 | + | |
| 30 | +# desenhe o grafo usando o networkx | |
| 31 | +nx.draw(G, pos, with_labels=True) | |
| 32 | + | |
| 33 | +# renderize o grafo com o pydot | |
| 34 | +graph = pydot.graph_from_dot_data(dot.to_string())[0] | |
| 35 | +graph.write_png('graph_asia2.png') | ... | ... |
| ... | ... | @@ -0,0 +1,31 @@ |
| 1 | +import bnlearn as bn | |
| 2 | + | |
| 3 | +asia = bn.import_DAG("asia2.bif") | |
| 4 | + | |
| 5 | +model = asia['model'] # Cria um objeto BayesianModel | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | +nos = model.nodes() | |
| 10 | +print("Número de nós:", len(nos)) #resposta à pergunta "quantos nós tem a rede?" | |
| 11 | + | |
| 12 | +print("Lista de nós:", nos) | |
| 13 | + | |
| 14 | +nos_in = 0 | |
| 15 | +nos_out = 0 | |
| 16 | + | |
| 17 | +for x in nos: | |
| 18 | + # Obtem os nós pais e filhos de cada nós | |
| 19 | + parents = model.get_parents(x) | |
| 20 | + children = model.get_children(x) | |
| 21 | + #resposta à pergunta "quantos nós são descendentes? quantos são ascendentes?" | |
| 22 | + print("Pais de ", x, ": ", parents) | |
| 23 | + print("Filhos de ", x, " :", children) | |
| 24 | + nos_in += len(children) | |
| 25 | + nos_out += len(parents) | |
| 26 | + | |
| 27 | +# resposta à pergunta "qual é o número médio de arestas "in"? e "out"?" | |
| 28 | +nos_in = nos_in/len(nos) | |
| 29 | +nos_out = nos_out/len(nos) | |
| 30 | +print("Número médio de arestas in: ", nos_in) | |
| 31 | +print("Número médio de arestas out: ", nos_out) | |
| 0 | 32 | \ No newline at end of file | ... | ... |