.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/models/plot_learn_spn_classifier.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_plot_learn_spn_classifier.py: ================================== Learning an SPN for Classification ================================== We can perform classification by learning an SPN from data and then comparing the probabilities for the given classes. .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: default import numpy as np np.random.seed(123) from spn.algorithms.LearningWrappers import learn_parametric, learn_classifier from spn.structure.leaves.parametric.Parametric import Categorical, Gaussian from spn.structure.Base import Context import matplotlib.pyplot as plt import seaborn as sns .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject return f(*args, **kwds) .. GENERATED FROM PYTHON SOURCE LINES 21-28 Imagine we have the following dataset generated by two gaussians with means :math:`(5,5)` and :math:`(10,10)`, and we label the cluster at :math:`(5,5)` to be class 0 and the cluster at :math:`(10,10)` to be class 1. Here, we model our problem as containing 3 features: two Gaussians for the coordinates and one Categorical for the label. We specify that the label is in column 2, and create the corresponding SPN. .. GENERATED FROM PYTHON SOURCE LINES 28-36 .. code-block:: default train_data = np.c_[ np.r_[np.random.normal(5, 1, (500, 2)), np.random.normal(10, 1, (500, 2))], np.r_[np.zeros((500, 1)), np.ones((500, 1))], ] sns.scatterplot(train_data[:, 0], train_data[:, 1], hue=train_data[:, 2]) .. image-sg:: /auto_examples/models/images/sphx_glr_plot_learn_spn_classifier_001.png :alt: plot learn spn classifier :srcset: /auto_examples/models/images/sphx_glr_plot_learn_spn_classifier_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. FutureWarning .. GENERATED FROM PYTHON SOURCE LINES 37-38 We can learn an SPN from the training data: .. GENERATED FROM PYTHON SOURCE LINES 38-47 .. code-block:: default spn_classification = learn_classifier( train_data, Context(parametric_types=[Gaussian, Gaussian, Categorical]).add_domains(train_data), learn_parametric, 2 ) from spn.io.Graphics import draw_spn draw_spn(spn_classification) .. image-sg:: /auto_examples/models/images/sphx_glr_plot_learn_spn_classifier_002.png :alt: plot learn spn classifier :srcset: /auto_examples/models/images/sphx_glr_plot_learn_spn_classifier_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /opt/hostedtoolcache/Python/3.6.15/x64/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject return f(*args, **kwds) .. GENERATED FROM PYTHON SOURCE LINES 48-53 Now, imagine we want to classify two instances, one located at :math:`(3,4)` and another one at :math:`(12,8)`. To do that, we first create an array with two rows and 3 columns. We set the last column to ``np.nan`` to indicate that we don't know the labels. And we set the rest of the values in the 2D array accordingly. .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: default test_data = np.array([3.0, 4.0, np.nan, 12.0, 18.0, np.nan]).reshape(-1, 3) .. GENERATED FROM PYTHON SOURCE LINES 57-59 We can do classification via approximate most probable explanation (MPE). Here, we expect the first instance to be labeled as 0 and the second one as 1. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: default from spn.algorithms.MPE import mpe print(mpe(spn_classification, test_data)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/runner/work/SPFlow/SPFlow/src/spn/structure/leaves/parametric/Inference.py:88: RuntimeWarning: divide by zero encountered in log probs[idx_in] = np.array(np.log(node.p))[cat_data[~out_domain_ids]] [[ 3. 4. 0.] [12. 18. 1.]] .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.883 seconds) .. _sphx_glr_download_auto_examples_models_plot_learn_spn_classifier.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_learn_spn_classifier.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_learn_spn_classifier.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_