diff options
author | Rémi Flamary <remi.flamary@gmail.com> | 2017-09-01 15:31:44 +0200 |
---|---|---|
committer | Rémi Flamary <remi.flamary@gmail.com> | 2017-09-01 15:31:44 +0200 |
commit | 062071b20d1d40c64bb619931bd11bd28e780485 (patch) | |
tree | 74bfcd48bb65304c2a5be74c24cdff29bd82ba4b /docs/source/auto_examples/plot_OT_L1_vs_L2.rst | |
parent | 212f3889b1114026765cda0134e02766daa82af2 (diff) |
update example with rst titles
Diffstat (limited to 'docs/source/auto_examples/plot_OT_L1_vs_L2.rst')
-rw-r--r-- | docs/source/auto_examples/plot_OT_L1_vs_L2.rst | 351 |
1 files changed, 242 insertions, 109 deletions
diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst index ba52bfe..83a7491 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst @@ -7,6 +7,8 @@ 2D Optimal transport for different metrics ========================================== +2D OT on empirical distributio with different gound metric. + Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf @@ -14,6 +16,80 @@ https://arxiv.org/pdf/1706.07650.pdf +.. code-block:: python + + + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + + + + + + + +Dataset 1 : uniform sampling +############################################################################# + + + +.. code-block:: python + + + n = 20 # nb samples + xs = np.zeros((n, 2)) + xs[:, 0] = np.arange(n) + 1 + xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... + + xt = np.zeros((n, 2)) + xt[:, 1] = np.arange(n) + 1 + + a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + + # loss matrix + M1 = ot.dist(xs, xt, metric='euclidean') + M1 /= M1.max() + + # loss matrix + M2 = ot.dist(xs, xt, metric='sqeuclidean') + M2 /= M2.max() + + # loss matrix + Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) + Mp /= Mp.max() + + # Data + pl.figure(1, figsize=(7, 3)) + pl.clf() + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + pl.title('Source and traget distributions') + + + # Cost matrices + pl.figure(2, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') + + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') + pl.title('Squared Euclidean cost') + + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') + pl.title('Sqrt Euclidean cost') + pl.tight_layout() + + + .. rst-class:: sphx-glr-horizontal @@ -28,138 +104,195 @@ https://arxiv.org/pdf/1706.07650.pdf .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_004.png - :scale: 47 +Dataset 1 : Plot OT Matrices +############################################################################# + + + +.. code-block:: python + + + + + #%% EMD + G1 = ot.emd(a, b, M1) + G2 = ot.emd(a, b, M2) + Gp = ot.emd(a, b, Mp) + + # OT matrices + pl.figure(3, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + ot.plot.plot2D_samples_mat(xs, xt, G1, 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.axis('equal') + # pl.legend(loc=0) + pl.title('OT Euclidean') + + pl.subplot(1, 3, 2) + ot.plot.plot2D_samples_mat(xs, xt, G2, 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.axis('equal') + # pl.legend(loc=0) + pl.title('OT squared Euclidean') + + pl.subplot(1, 3, 3) + ot.plot.plot2D_samples_mat(xs, xt, Gp, 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.axis('equal') + # pl.legend(loc=0) + pl.title('OT sqrt Euclidean') + pl.tight_layout() + + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png + :align: center + + + + +Dataset 2 : Partial circle +############################################################################# + + + +.. code-block:: python + + + n = 50 # nb samples + xtot = np.zeros((n + 1, 2)) + xtot[:, 0] = np.cos( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + xtot[:, 1] = np.sin( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + + xs = xtot[:n, :] + xt = xtot[1:, :] + + a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + + # loss matrix + M1 = ot.dist(xs, xt, metric='euclidean') + M1 /= M1.max() + + # loss matrix + M2 = ot.dist(xs, xt, metric='sqeuclidean') + M2 /= M2.max() + + # loss matrix + Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) + Mp /= Mp.max() + + + # Data + pl.figure(4, figsize=(7, 3)) + pl.clf() + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') + pl.axis('equal') + pl.title('Source and traget distributions') + + + # Cost matrices + pl.figure(5, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') + + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') + pl.title('Squared Euclidean cost') + + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') + pl.title('Sqrt Euclidean cost') + pl.tight_layout() + + + + +.. rst-class:: sphx-glr-horizontal + * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png + .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_007.png :scale: 47 * - .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_006.png + .. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_008.png :scale: 47 +Dataset 2 : Plot OT Matrices +############################################################################# + + .. code-block:: python - # Author: Remi Flamary <remi.flamary@unice.fr> - # - # License: MIT License - import numpy as np - import matplotlib.pylab as pl - import ot - #%% parameters and data generation - - for data in range(2): - - if data: - n = 20 # nb samples - xs = np.zeros((n, 2)) - xs[:, 0] = np.arange(n) + 1 - xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... - - xt = np.zeros((n, 2)) - xt[:, 1] = np.arange(n) + 1 - else: - - n = 50 # nb samples - xtot = np.zeros((n + 1, 2)) - xtot[:, 0] = np.cos( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - xtot[:, 1] = np.sin( - (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) - - xs = xtot[:n, :] - xt = xtot[1:, :] - - a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples - - # loss matrix - M1 = ot.dist(xs, xt, metric='euclidean') - M1 /= M1.max() - - # loss matrix - M2 = ot.dist(xs, xt, metric='sqeuclidean') - M2 /= M2.max() - - # loss matrix - Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) - Mp /= Mp.max() - - #%% plot samples - - pl.figure(1 + 3 * data, figsize=(7, 3)) - pl.clf() - pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') - pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') - pl.axis('equal') - pl.title('Source and traget distributions') - - pl.figure(2 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - pl.imshow(M1, interpolation='nearest') - pl.title('Euclidean cost') - - pl.subplot(1, 3, 2) - pl.imshow(M2, interpolation='nearest') - pl.title('Squared Euclidean cost') - - pl.subplot(1, 3, 3) - pl.imshow(Mp, interpolation='nearest') - pl.title('Sqrt Euclidean cost') - pl.tight_layout() - - #%% EMD - G1 = ot.emd(a, b, M1) - G2 = ot.emd(a, b, M2) - Gp = ot.emd(a, b, Mp) - - pl.figure(3 + 3 * data, figsize=(7, 3)) - - pl.subplot(1, 3, 1) - ot.plot.plot2D_samples_mat(xs, xt, G1, 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.axis('equal') - # pl.legend(loc=0) - pl.title('OT Euclidean') - - pl.subplot(1, 3, 2) - ot.plot.plot2D_samples_mat(xs, xt, G2, 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.axis('equal') - # pl.legend(loc=0) - pl.title('OT squared Euclidean') - - pl.subplot(1, 3, 3) - ot.plot.plot2D_samples_mat(xs, xt, Gp, 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.axis('equal') - # pl.legend(loc=0) - pl.title('OT sqrt Euclidean') - pl.tight_layout() + #%% EMD + G1 = ot.emd(a, b, M1) + G2 = ot.emd(a, b, M2) + Gp = ot.emd(a, b, Mp) + + # OT matrices + pl.figure(6, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + ot.plot.plot2D_samples_mat(xs, xt, G1, 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.axis('equal') + # pl.legend(loc=0) + pl.title('OT Euclidean') + + pl.subplot(1, 3, 2) + ot.plot.plot2D_samples_mat(xs, xt, G2, 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.axis('equal') + # pl.legend(loc=0) + pl.title('OT squared Euclidean') + + pl.subplot(1, 3, 3) + ot.plot.plot2D_samples_mat(xs, xt, Gp, 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.axis('equal') + # pl.legend(loc=0) + pl.title('OT sqrt Euclidean') + pl.tight_layout() pl.show() -**Total running time of the script:** ( 0 minutes 1.906 seconds) + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_011.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 1.217 seconds) @@ -178,4 +311,4 @@ https://arxiv.org/pdf/1706.07650.pdf .. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery <http://sphinx-gallery.readthedocs.io>`_ + `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ |