Skip to content

Instantly share code, notes, and snippets.

@smzn
Created February 18, 2024 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smzn/28df30e21474398c6c105071d00e6cd5 to your computer and use it in GitHub Desktop.
Save smzn/28df30e21474398c6c105071d00e6cd5 to your computer and use it in GitHub Desktop.
ベイジアンネットワーク例2
import networkx as nx
import matplotlib.pyplot as plt
# ベイジアンネットワークの有向グラフを作成
G2 = nx.DiGraph()
# ノードを追加
G2.add_node("B")
G2.add_node("E")
G2.add_node("A")
G2.add_node("P")
G2.add_node("S")
# エッジを追加
G2.add_edge("B", "A")
G2.add_edge("E", "A")
G2.add_edge("A", "P")
G2.add_edge("A", "S")
# ノードの位置を手動で設定
pos = {"B": (0.5, 2), "E": (1.5, 2), "A": (1, 1), "P": (0.5, 0), "S": (1.5, 0)}
# ベイジアンネットワークを描画
#pos = nx.spring_layout(G2) # グラフをレイアウト
nx.draw(G2, pos, with_labels=True, node_size=3000, node_color="skyblue", font_size=15, font_weight="bold") # グラフを描画
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment