.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_OT_2D_samples.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_plot_OT_2D_samples.py: ==================================================== Optimal Transport between 2D empirical distributions ==================================================== Illustration of 2D optimal transport between distributions that are weighted sum of Diracs. The OT matrix is plotted with the samples. .. GENERATED FROM PYTHON SOURCE LINES 11-24 .. code-block:: Python # Author: Remi Flamary # Kilian Fatras # # License: MIT License # sphinx_gallery_thumbnail_number = 4 import numpy as np import matplotlib.pylab as pl import ot import ot.plot .. GENERATED FROM PYTHON SOURCE LINES 25-27 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 29-46 .. code-block:: Python n = 50 # nb samples mu_s = np.array([0, 0]) cov_s = np.array([[1, 0], [0, 1]]) mu_t = np.array([4, 4]) cov_t = np.array([[1, -0.8], [-0.8, 1]]) xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples # loss matrix M = ot.dist(xs, xt) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 51-62 .. code-block:: Python pl.figure(1) pl.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") pl.plot(xt[:, 0], xt[:, 1], "xr", label="Target samples") pl.legend(loc=0) pl.title("Source and target distributions") pl.figure(2) pl.imshow(M, interpolation="nearest") pl.title("Cost matrix M") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png :alt: Source and target distributions :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png :alt: Cost matrix M :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Cost matrix M') .. GENERATED FROM PYTHON SOURCE LINES 63-65 Compute EMD ----------- .. GENERATED FROM PYTHON SOURCE LINES 67-82 .. code-block:: Python G0 = ot.solve(M, a, b).plan pl.figure(3) pl.imshow(G0, interpolation="nearest") pl.title("OT matrix G0") pl.figure(4) ot.plot.plot2D_samples_mat(xs, xt, G0, c=[0.5, 0.5, 1]) pl.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") pl.plot(xt[:, 0], xt[:, 1], "xr", label="Target samples") pl.legend(loc=0) pl.title("OT matrix with samples") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png :alt: OT matrix G0 :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png :alt: OT matrix with samples :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'OT matrix with samples') .. GENERATED FROM PYTHON SOURCE LINES 83-85 Compute Sinkhorn ---------------- .. GENERATED FROM PYTHON SOURCE LINES 87-107 .. code-block:: Python # reg term lambd = 1e-1 Gs = ot.sinkhorn(a, b, M, lambd) pl.figure(5) pl.imshow(Gs, interpolation="nearest") pl.title("OT matrix sinkhorn") pl.figure(6) ot.plot.plot2D_samples_mat(xs, xt, Gs, color=[0.5, 0.5, 1]) pl.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") pl.plot(xt[:, 0], xt[:, 1], "xr", label="Target samples") pl.legend(loc=0) pl.title("OT matrix Sinkhorn with samples") pl.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png :alt: OT matrix sinkhorn :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png :alt: OT matrix Sinkhorn with samples :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 108-110 Empirical Sinkhorn ------------------- .. GENERATED FROM PYTHON SOURCE LINES 112-130 .. code-block:: Python # reg term lambd = 1e-1 Ges = ot.bregman.empirical_sinkhorn(xs, xt, lambd) pl.figure(7) pl.imshow(Ges, interpolation="nearest") pl.title("OT matrix empirical sinkhorn") pl.figure(8) ot.plot.plot2D_samples_mat(xs, xt, Ges, color=[0.5, 0.5, 1]) pl.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples") pl.plot(xt[:, 0], xt[:, 1], "xr", label="Target samples") pl.legend(loc=0) pl.title("OT matrix Sinkhorn from samples") pl.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_007.png :alt: OT matrix empirical sinkhorn :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_007.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_2D_samples_008.png :alt: OT matrix Sinkhorn from samples :srcset: /auto_examples/images/sphx_glr_plot_OT_2D_samples_008.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.861 seconds) .. _sphx_glr_download_auto_examples_plot_OT_2D_samples.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_OT_2D_samples.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_OT_2D_samples.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_OT_2D_samples.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_