.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/others/plot_WeakOT_VS_OT.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_others_plot_WeakOT_VS_OT.py: ==================================================== Weak Optimal Transport VS exact Optimal Transport ==================================================== 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-23 .. code-block:: Python # Author: Remi Flamary # # 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 24-26 Generate data an plot it ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 28-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, -.8], [-.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 = ot.unif(n), ot.unif(n) # uniform distribution on samples # loss matrix M = ot.dist(xs, xt) M /= M.max() .. GENERATED FROM PYTHON SOURCE LINES 47-59 .. 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/others/images/sphx_glr_plot_WeakOT_VS_OT_001.png :alt: Source and target distributions :srcset: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_002.png :alt: Cost matrix M :srcset: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_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 60-62 Compute Weak OT and exact OT solutions -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python G0 = ot.emd(a, b, M) .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: Python Gweak = ot.weak_optimal_transport(xs, xt, a, b) .. GENERATED FROM PYTHON SOURCE LINES 73-75 Plot weak OT and exact OT solutions -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 75-99 .. code-block:: Python pl.figure(3, (8, 5)) pl.subplot(1, 2, 1) pl.imshow(G0, interpolation='nearest') pl.title('OT matrix') pl.subplot(1, 2, 2) pl.imshow(Gweak, interpolation='nearest') pl.title('Weak OT matrix') pl.figure(4, (8, 5)) pl.subplot(1, 2, 1) ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.title('OT matrix with samples') pl.subplot(1, 2, 2) ot.plot.plot2D_samples_mat(xs, xt, Gweak, c=[.5, .5, 1]) pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.title('Weak OT matrix with samples') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_003.png :alt: OT matrix, Weak OT matrix :srcset: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_004.png :alt: OT matrix with samples, Weak OT matrix with samples :srcset: /auto_examples/others/images/sphx_glr_plot_WeakOT_VS_OT_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Weak OT matrix with samples') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.368 seconds) .. _sphx_glr_download_auto_examples_others_plot_WeakOT_VS_OT.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_WeakOT_VS_OT.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_WeakOT_VS_OT.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_