.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_OT_1D_smooth.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_1D_smooth.py: ================================ Smooth and sparse OT example ================================ This example illustrates the computation of Smooth and Sparse (KL an L2 reg.) OT and sparsity-constrained OT, together with their visualizations. .. GENERATED FROM PYTHON SOURCE LINES 12-25 .. code-block:: Python # Author: Remi Flamary # # License: MIT License # sphinx_gallery_thumbnail_number = 5 import numpy as np import matplotlib.pylab as pl import ot import ot.plot from ot.datasets import make_1D_gauss as gauss .. GENERATED FROM PYTHON SOURCE LINES 26-28 Generate data ------------- .. GENERATED FROM PYTHON SOURCE LINES 31-46 .. code-block:: Python n = 100 # nb bins # bin positions x = np.arange(n, dtype=np.float64) # Gaussian distributions a = gauss(n, m=20, s=5) # m= mean, s= std b = gauss(n, m=60, s=10) # loss matrix M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) M /= M.max() .. GENERATED FROM PYTHON SOURCE LINES 47-49 Plot distributions and loss matrix ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-57 .. code-block:: Python pl.figure(1, figsize=(6.4, 3)) pl.plot(x, a, 'b', label='Source distribution') pl.plot(x, b, 'r', label='Target distribution') pl.legend() .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png :alt: plot OT 1D smooth :srcset: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 58-63 .. code-block:: Python pl.figure(2, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png :alt: Cost matrix M :srcset: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-66 Solve Smooth OT --------------- .. GENERATED FROM PYTHON SOURCE LINES 69-79 .. code-block:: Python lambd = 2e-3 Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl') pl.figure(3, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.') pl.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_003.png :alt: OT matrix Smooth OT KL reg. :srcset: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-89 .. code-block:: Python lambd = 1e-1 Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2') pl.figure(4, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.') pl.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_004.png :alt: OT matrix Smooth OT l2 reg. :srcset: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-100 .. code-block:: Python lambd = 1e-1 max_nz = 2 # two non-zero entries are permitted per column of the OT plan Gsc = ot.smooth.smooth_ot_dual( a, b, M, lambd, reg_type='sparsity_constrained', max_nz=max_nz) pl.figure(5, figsize=(5, 5)) ot.plot.plot1D_mat(a, b, Gsc, 'Sparsity constrained OT matrix; k=2.') pl.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png :alt: Sparsity constrained OT matrix; k=2. :srcset: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.003 seconds) .. _sphx_glr_download_auto_examples_plot_OT_1D_smooth.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_1D_smooth.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_OT_1D_smooth.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_