.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/queries/plot_marginalize.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_queries_plot_marginalize.py: .. _marginalizing_an_spn: ==================== Marginalizing an SPN ==================== "Marginalizing an SPN" means summing out all other non-relevant variables. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: default from spn.structure.leaves.parametric.Parametric import Categorical from spn.structure.Base import Sum, Product from spn.structure.Base import assign_ids, rebuild_scopes_bottom_up from spn.io.Graphics import draw_spn import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 21-23 We will start with the Sum-Product Network structure from the :ref:`composing_spn_object_hierarchy` example. .. GENERATED FROM PYTHON SOURCE LINES 23-37 .. code-block:: default p0 = Product(children=[Categorical(p=[0.3, 0.7], scope=1), Categorical(p=[0.4, 0.6], scope=2)]) p1 = Product(children=[Categorical(p=[0.5, 0.5], scope=1), Categorical(p=[0.6, 0.4], scope=2)]) s1 = Sum(weights=[0.3, 0.7], children=[p0, p1]) p2 = Product(children=[Categorical(p=[0.2, 0.8], scope=0), s1]) p3 = Product(children=[Categorical(p=[0.2, 0.8], scope=0), Categorical(p=[0.3, 0.7], scope=1)]) p4 = Product(children=[p3, Categorical(p=[0.4, 0.6], scope=2)]) spn = Sum(weights=[0.4, 0.6], children=[p2, p4]) assign_ids(spn) rebuild_scopes_bottom_up(spn) ax = draw_spn(spn) .. image-sg:: /auto_examples/queries/images/sphx_glr_plot_marginalize_001.png :alt: plot marginalize :srcset: /auto_examples/queries/images/sphx_glr_plot_marginalize_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-40 If we want to marginalize this SPN by summing out all other variables to leave variables 1 and 2, we can do this as follows: .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: default from spn.algorithms.Marginalization import marginalize spn_marg = marginalize(spn, [1, 2]) .. GENERATED FROM PYTHON SOURCE LINES 46-49 This marginalizes all the variables *not* in :math:`[1, 2]`, and create a *new* structure that knows nothing about the previous one nor about the variable 0. .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: default draw_spn(spn_marg) .. image-sg:: /auto_examples/queries/images/sphx_glr_plot_marginalize_002.png :alt: plot marginalize :srcset: /auto_examples/queries/images/sphx_glr_plot_marginalize_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.454 seconds) .. _sphx_glr_download_auto_examples_queries_plot_marginalize.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_marginalize.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_marginalize.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_