.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/barycenters/plot_free_support_barycenter.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_barycenters_plot_free_support_barycenter.py: ======================================================== 2D free support Wasserstein barycenters of distributions ======================================================== Illustration of 2D Wasserstein and Sinkhorn barycenters if distributions are weighted sum of Diracs. .. GENERATED FROM PYTHON SOURCE LINES 11-25 .. code-block:: Python # Authors: Vivien Seguy # Rémi Flamary # Eduardo Fernandes Montesuma # # License: MIT License # sphinx_gallery_thumbnail_number = 2 import numpy as np import matplotlib.pylab as pl import ot .. GENERATED FROM PYTHON SOURCE LINES 26-28 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 28-51 .. code-block:: Python N = 2 d = 2 I1 = pl.imread("../../data/redcross.png").astype(np.float64)[::4, ::4, 2] I2 = pl.imread("../../data/duck.png").astype(np.float64)[::4, ::4, 2] sz = I2.shape[0] XX, YY = np.meshgrid(np.arange(sz), np.arange(sz)) x1 = np.stack((XX[I1 == 0], YY[I1 == 0]), 1) * 1.0 x2 = np.stack((XX[I2 == 0] + 80, -YY[I2 == 0] + 32), 1) * 1.0 x3 = np.stack((XX[I2 == 0], -YY[I2 == 0] + 32), 1) * 1.0 measures_locations = [x1, x2] measures_weights = [ot.unif(x1.shape[0]), ot.unif(x2.shape[0])] pl.figure(1, (12, 4)) pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) pl.title("Distributions") .. image-sg:: /auto_examples/barycenters/images/sphx_glr_plot_free_support_barycenter_001.png :alt: Distributions :srcset: /auto_examples/barycenters/images/sphx_glr_plot_free_support_barycenter_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Distributions') .. GENERATED FROM PYTHON SOURCE LINES 52-54 Compute free support Wasserstein barycenter ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-63 .. code-block:: Python k = 200 # number of Diracs of the barycenter X_init = np.random.normal(0.0, 1.0, (k, d)) # initial Dirac locations b = ( np.ones((k,)) / k ) # weights of the barycenter (it will not be optimized, only the locations are optimized) X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) .. GENERATED FROM PYTHON SOURCE LINES 64-66 Plot the Wasserstein barycenter ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 66-75 .. code-block:: Python pl.figure(2, (8, 3)) pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) pl.scatter(X[:, 0], X[:, 1], s=b * 1000, marker="s", label="2-Wasserstein barycenter") pl.title("Data measures and their barycenter") pl.legend(loc="lower right") pl.show() .. image-sg:: /auto_examples/barycenters/images/sphx_glr_plot_free_support_barycenter_002.png :alt: Data measures and their barycenter :srcset: /auto_examples/barycenters/images/sphx_glr_plot_free_support_barycenter_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-77 Compute free support Sinkhorn barycenter .. GENERATED FROM PYTHON SOURCE LINES 77-88 .. code-block:: Python k = 200 # number of Diracs of the barycenter X_init = np.random.normal(0.0, 1.0, (k, d)) # initial Dirac locations b = ( np.ones((k,)) / k ) # weights of the barycenter (it will not be optimized, only the locations are optimized) X = ot.bregman.free_support_sinkhorn_barycenter( measures_locations, measures_weights, X_init, 20, b, numItermax=15 ) .. GENERATED FROM PYTHON SOURCE LINES 89-91 Plot the Wasserstein barycenter ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 91-99 .. code-block:: Python pl.figure(2, (8, 3)) pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5) pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5) pl.scatter(X[:, 0], X[:, 1], s=b * 1000, marker="s", label="2-Wasserstein barycenter") pl.title("Data measures and their barycenter") pl.legend(loc="lower right") pl.show() .. image-sg:: /auto_examples/barycenters/images/sphx_glr_plot_free_support_barycenter_003.png :alt: Data measures and their barycenter :srcset: /auto_examples/barycenters/images/sphx_glr_plot_free_support_barycenter_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.623 seconds) .. _sphx_glr_download_auto_examples_barycenters_plot_free_support_barycenter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_free_support_barycenter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_free_support_barycenter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_free_support_barycenter.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_