summaryrefslogtreecommitdiff
path: root/_downloads/e0067247ba27f59d84e7f2e24e041fc1/plot_decomposition.ipynb
blob: 8938015d7d4ccff6431e76ce65bced9f64bc933d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Decomposition\n\nExample of creating a junction tree from a directed graph.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport networkx as nx\n\nB = nx.DiGraph()\nB.add_nodes_from([\"A\", \"B\", \"C\", \"D\", \"E\", \"F\"])\nB.add_edges_from(\n    [(\"A\", \"B\"), (\"A\", \"C\"), (\"B\", \"D\"), (\"B\", \"F\"), (\"C\", \"E\"), (\"E\", \"F\")]\n)\n\noptions = {\"with_labels\": True, \"node_color\": \"white\", \"edgecolors\": \"blue\"}\n\nfig = plt.figure(figsize=(6, 9))\naxgrid = fig.add_gridspec(3, 2)\n\nax1 = fig.add_subplot(axgrid[0, 0])\nax1.set_title(\"Bayesian Network\")\npos = nx.nx_agraph.graphviz_layout(B, prog=\"neato\")\nnx.draw_networkx(B, pos=pos, **options)\n\nmg = nx.moral_graph(B)\nax2 = fig.add_subplot(axgrid[0, 1], sharex=ax1, sharey=ax1)\nax2.set_title(\"Moralized Graph\")\nnx.draw_networkx(mg, pos=pos, **options)\n\njt = nx.junction_tree(B)\nax3 = fig.add_subplot(axgrid[1:, :])\nax3.set_title(\"Junction Tree\")\nax3.margins(0.15, 0.25)\nnsize = [2000 * len(n) for n in list(jt.nodes())]\npos = nx.nx_agraph.graphviz_layout(jt, prog=\"neato\")\nnx.draw_networkx(jt, pos=pos, node_size=nsize, **options)\n\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}