.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/models/cnets.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_models_cnets.py: ======================= Cutset Networks (CNets) ======================= With SPFlow we can learn both the structure and the parameters of CNets, a particular kind of SPNs with CLTs as leaf providing exact MPE inference. .. GENERATED FROM PYTHON SOURCE LINES 9-55 .. code-block:: default import numpy as np np.random.seed(123) from spn.structure.leaves.cltree.CLTree import create_cltree_leaf from spn.structure.Base import Context from spn.structure.leaves.parametric.Parametric import Bernoulli from spn.algorithms.LearningWrappers import learn_parametric, learn_cnet from spn.algorithms.Inference import log_likelihood from spn.algorithms.MPE import mpe train_data = np.random.binomial(1, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.1], size=(100, 10)) ds_context = Context( parametric_types=[ Bernoulli, Bernoulli, Bernoulli, Bernoulli, Bernoulli, Bernoulli, Bernoulli, Bernoulli, Bernoulli, Bernoulli, ] ).add_domains(train_data) # Learning a CNet with a naive mle conditioning cnet_naive_mle = learn_cnet(train_data, ds_context, cond="naive_mle", min_instances_slice=20, min_features_slice=1) # Learning a CNet with random conditioning cnet_random = learn_cnet(train_data, ds_context, cond="random", min_instances_slice=20, min_features_slice=1) ll = log_likelihood(cnet_naive_mle, train_data) print("Naive mle conditioning", np.mean(ll)) ll = log_likelihood(cnet_random, train_data) print("Random conditioning", np.mean(ll)) # computing exact MPE train_data_mpe = train_data.astype(float) train_data_mpe[:, 0] = np.nan print(mpe(cnet_random, train_data_mpe)) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_models_cnets.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: cnets.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: cnets.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_