.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/01_asterisk_K8/plot-01-demo=init_methods-model=mix+gauss.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_examples_01_asterisk_K8_plot-01-demo=init_methods-model=mix+gauss.py: ======================================== Initialization for Mixtures of Gaussians ======================================== How to initialize Gaussian observation models. We demonstrate a few possible initialization procedures for Gaussian observation models (which include Gauss, DiagGauss, ZeroMeanGauss). Initialization depends on two key user-specified procedures: 1) Specifying hyperparameters for the conjugate prior 2) Specifying how many clusters are created .. GENERATED FROM PYTHON SOURCE LINES 17-30 .. code-block:: default # SPECIFY WHICH PLOT CREATED BY THIS SCRIPT IS THE THUMBNAIL IMAGE # sphinx_gallery_thumbnail_number = 2 import bnpy import numpy as np import os from matplotlib import pylab import seaborn as sns FIG_SIZE = (3, 3) SMALL_FIG_SIZE = (2, 2) .. GENERATED FROM PYTHON SOURCE LINES 31-32 Read bnpy's built-in "AsteriskK8" dataset from file. .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: default dataset_path = os.path.join(bnpy.DATASET_PATH, 'AsteriskK8') dataset = bnpy.data.XData.read_npz( os.path.join(dataset_path, 'x_dataset.npz')) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Make a simple plot of the raw data .. GENERATED FROM PYTHON SOURCE LINES 39-46 .. code-block:: default pylab.figure(figsize=FIG_SIZE) pylab.plot(dataset.X[:, 0], dataset.X[:, 1], 'k.') pylab.gca().set_xlim([-2, 2]) pylab.gca().set_ylim([-2, 2]) pylab.tight_layout() .. image-sg:: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_001.png :alt: plot 01 demo=init methods model=mix+gauss :srcset: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-48 Utility function for displaying many random initializations side by side. .. GENERATED FROM PYTHON SOURCE LINES 49-79 .. code-block:: default def show_many_random_initial_models( obsPriorArgsDict, initArgsDict, nrows=1, ncols=6): ''' Create plot of many different random initializations ''' fig_handle, ax_handle_list = pylab.subplots( figsize=(SMALL_FIG_SIZE[0] * ncols, SMALL_FIG_SIZE[1] * nrows), nrows=nrows, ncols=ncols, sharex=True, sharey=True) for trial_id in range(nrows * ncols): cur_model = bnpy.make_initialized_model( dataset, allocModelName='FiniteMixtureModel', obsModelName='Gauss', algName='VB', allocPriorArgsDict=dict(gamma=10.0), obsPriorArgsDict=obsPriorArgsDict, initArgsDict=initArgsDict, seed=int(trial_id), ) # Plot the current model cur_ax_handle = ax_handle_list.flatten()[trial_id] bnpy.viz.PlotComps.plotCompsFromHModel( cur_model, Data=dataset, ax_handle=cur_ax_handle) cur_ax_handle.set_xticks([-2, -1, 0, 1, 2]) cur_ax_handle.set_yticks([-2, -1, 0, 1, 2]) pylab.tight_layout() .. GENERATED FROM PYTHON SOURCE LINES 80-92 initname: 'randexamples' ------------------------ This procedure selects K examples uniformly at random. Each cluster is then initialized from one selected example, using a standard global step update. **Example 1**: Initialize with 8 clusters, with prior biased towards small covariances .. math:: \E_{\mbox{prior}}[ \Sigma_k ] = 0.01 I_D .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: default show_many_random_initial_models( dict(sF=0.01, ECovMat='eye'), dict(initname='randexamples', K=8)) .. image-sg:: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_002.png :alt: plot 01 demo=init methods model=mix+gauss :srcset: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-104 **Example 2**: Initialize with 8 clusters, with prior biased towards moderate covariances .. math:: \E_{\mbox{prior}}[ \Sigma_k ] = 0.2 I_D .. GENERATED FROM PYTHON SOURCE LINES 104-109 .. code-block:: default show_many_random_initial_models( dict(sF=0.2, ECovMat='eye'), dict(initname='randexamples', K=8)) .. image-sg:: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_003.png :alt: plot 01 demo=init methods model=mix+gauss :srcset: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-121 initname: 'bregmankmeans' ------------------------- This procedure selects K examples using a distance-biased procedure. First, one example is chosen uniformly at random. Next, each successive example is chosen with probability proportional to the distance from the nearest example in the chosen set. We measure distance using the appropriate Bregman divergence. **Example 1**: Initialize with 8 clusters, with prior biased towards small covariances .. GENERATED FROM PYTHON SOURCE LINES 121-126 .. code-block:: default show_many_random_initial_models( dict(sF=0.01, ECovMat='eye'), dict(initname='bregmankmeans', K=8, init_NiterForBregmanKMeans=0)) .. image-sg:: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_004.png :alt: plot 01 demo=init methods model=mix+gauss :srcset: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-131 **Example 2**: Initialize as above, then allow the k-means algorithm to run for 10 iterations to "refine" the initial clustering. .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. code-block:: default show_many_random_initial_models( dict(sF=0.01, ECovMat='eye'), dict(initname='bregmankmeans', K=8, init_NiterForBregmanKMeans=10)) .. image-sg:: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_005.png :alt: plot 01 demo=init methods model=mix+gauss :srcset: /examples/01_asterisk_K8/images/sphx_glr_plot-01-demo=init_methods-model=mix+gauss_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.636 seconds) .. _sphx_glr_download_examples_01_asterisk_K8_plot-01-demo=init_methods-model=mix+gauss.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot-01-demo=init_methods-model=mix+gauss.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot-01-demo=init_methods-model=mix+gauss.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_