From e43d9d3be1437823c89e473cf09346b8d30fd947 Mon Sep 17 00:00:00 2001 From: Francisco Coelho Date: Thu, 4 May 2023 15:41:12 +0100 Subject: [PATCH] AMartins | Tarefa 01 | 01 --- students/amartins/tarefas/asia2.bif | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ students/amartins/tarefas/graph_asia2.png | Bin 0 -> 33454 bytes students/amartins/tarefas/tarefa1a.py | 38 ++++++++++++++++++++++++++++++++++++++ students/amartins/tarefas/tarefa1b.py | 35 +++++++++++++++++++++++++++++++++++ students/amartins/tarefas/v01-tarefa1a.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+), 0 deletions(-) create mode 100644 students/amartins/tarefas/asia2.bif create mode 100644 students/amartins/tarefas/graph_asia2.png create mode 100644 students/amartins/tarefas/tarefa1a.py create mode 100644 students/amartins/tarefas/tarefa1b.py create mode 100644 students/amartins/tarefas/v01-tarefa1a.py diff --git a/students/amartins/tarefas/asia2.bif b/students/amartins/tarefas/asia2.bif new file mode 100644 index 0000000..1543625 --- /dev/null +++ b/students/amartins/tarefas/asia2.bif @@ -0,0 +1,60 @@ +network unknown { +} +variable asia { + type discrete [ 2 ] { yes, no }; +} +variable tub { + type discrete [ 2 ] { yes, no }; +} +variable smoke { + type discrete [ 2 ] { yes, no }; +} +variable lung { + type discrete [ 2 ] { yes, no }; +} +variable bronc { + type discrete [ 2 ] { yes, no }; +} +variable either { + type discrete [ 2 ] { yes, no }; +} +variable xray { + type discrete [ 2 ] { yes, no }; +} +variable dysp { + type discrete [ 2 ] { yes, no }; +} +probability ( asia ) { + table 0.01, 0.99; +} +probability ( tub | asia ) { + (yes) 0.05, 0.95; + (no) 0.01, 0.99; +} +probability ( smoke ) { + table 0.5, 0.5; +} +probability ( lung | smoke ) { + (yes) 0.1, 0.9; + (no) 0.01, 0.99; +} +probability ( bronc | smoke ) { + (yes) 0.6, 0.4; + (no) 0.3, 0.7; +} +probability ( either | lung, tub ) { + (yes, yes) 1.0, 0.0; + (no, yes) 1.0, 0.0; + (yes, no) 1.0, 0.0; + (no, no) 0.0, 1.0; +} +probability ( xray | either ) { + (yes) 0.98, 0.02; + (no) 0.05, 0.95; +} +probability ( dysp | bronc, either ) { + (yes, yes) 0.9, 0.1; + (no, yes) 0.7, 0.3; + (yes, no) 0.8, 0.2; + (no, no) 0.1, 0.9; +} diff --git a/students/amartins/tarefas/graph_asia2.png b/students/amartins/tarefas/graph_asia2.png new file mode 100644 index 0000000..5f3d141 Binary files /dev/null and b/students/amartins/tarefas/graph_asia2.png differ diff --git a/students/amartins/tarefas/tarefa1a.py b/students/amartins/tarefas/tarefa1a.py new file mode 100644 index 0000000..a6c2fc4 --- /dev/null +++ b/students/amartins/tarefas/tarefa1a.py @@ -0,0 +1,38 @@ +import bnlearn as bn + +asia = bn.import_DAG("asia2.bif") + +model = asia['model'] # Cria um objeto BayesianModel + + + +nos = model.nodes() +print("Número de nós:", len(nos)) #resposta à pergunta "quantos nós tem a rede?" + +print("Lista de nós:", nos) + +asc = 0 +des = 0 +nos_in = 0 +nos_out = 0 + +for x in nos: + # Obtem os nós pais e filhos de cada nós + parents = model.get_parents(x) + children = model.get_children(x) + + if len(parents) != 0: + asc += 1 + if len(children) != 0: + des += 1 + nos_in += len(children) + nos_out += len(parents) + +#resposta à pergunta "quantos nós são descendentes? quantos são ascendentes?" +print("Nós ascendentes: ", asc) +print("Nós descendentes: ", des) +# resposta à pergunta "qual é o número médio de arestas "in"? e "out"?" +nos_in = nos_in/len(nos) +nos_out = nos_out/len(nos) +print("Número médio de arestas in: ", nos_in) +print("Número médio de arestas out: ", nos_out) \ No newline at end of file diff --git a/students/amartins/tarefas/tarefa1b.py b/students/amartins/tarefas/tarefa1b.py new file mode 100644 index 0000000..f0ddec1 --- /dev/null +++ b/students/amartins/tarefas/tarefa1b.py @@ -0,0 +1,35 @@ +import bnlearn as bn +import networkx as nx +import pydot +from networkx.drawing.nx_pydot import graphviz_layout + +asia = bn.import_DAG("asia2.bif") +model = asia['model'] + +# DiGraph do networkx +G = nx.DiGraph() + +for node in model.nodes: + G.add_node(node) + +for edge in model.edges: + G.add_edge(edge[0], edge[1]) + +# objeto pydot +dot = pydot.Dot(graph_type='digraph') + +for node in model.nodes: + dot.add_node(pydot.Node(node)) + +for edge in model.edges: + dot.add_edge(pydot.Edge(edge[0], edge[1])) + +# defina o layout do grafo +pos = graphviz_layout(G, prog='dot') + +# desenhe o grafo usando o networkx +nx.draw(G, pos, with_labels=True) + +# renderize o grafo com o pydot +graph = pydot.graph_from_dot_data(dot.to_string())[0] +graph.write_png('graph_asia2.png') diff --git a/students/amartins/tarefas/v01-tarefa1a.py b/students/amartins/tarefas/v01-tarefa1a.py new file mode 100644 index 0000000..bac9ca2 --- /dev/null +++ b/students/amartins/tarefas/v01-tarefa1a.py @@ -0,0 +1,31 @@ +import bnlearn as bn + +asia = bn.import_DAG("asia2.bif") + +model = asia['model'] # Cria um objeto BayesianModel + + + +nos = model.nodes() +print("Número de nós:", len(nos)) #resposta à pergunta "quantos nós tem a rede?" + +print("Lista de nós:", nos) + +nos_in = 0 +nos_out = 0 + +for x in nos: + # Obtem os nós pais e filhos de cada nós + parents = model.get_parents(x) + children = model.get_children(x) + #resposta à pergunta "quantos nós são descendentes? quantos são ascendentes?" + print("Pais de ", x, ": ", parents) + print("Filhos de ", x, " :", children) + nos_in += len(children) + nos_out += len(parents) + +# resposta à pergunta "qual é o número médio de arestas "in"? e "out"?" +nos_in = nos_in/len(nos) +nos_out = nos_out/len(nos) +print("Número médio de arestas in: ", nos_in) +print("Número médio de arestas out: ", nos_out) \ No newline at end of file -- libgit2 0.21.2