.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/others/plot_factored_coupling.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_factored_coupling.py: ========================================== Optimal transport with factored couplings ========================================== Illustration of the factored coupling OT between 2D empirical distributions .. GENERATED FROM PYTHON SOURCE LINES 10-22 .. code-block:: Python # Author: Remi Flamary # # License: MIT License # sphinx_gallery_thumbnail_number = 2 import numpy as np import matplotlib.pylab as pl import ot import ot.plot .. GENERATED FROM PYTHON SOURCE LINES 23-25 Generate data an plot it ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 25-40 .. code-block:: Python # parameters and data generation np.random.seed(42) n = 100 # nb samples xs = np.random.rand(n, 2) - .5 xs = xs + np.sign(xs) xt = np.random.rand(n, 2) - .5 a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. 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') .. image-sg:: /auto_examples/others/images/sphx_glr_plot_factored_coupling_001.png :alt: Source and target distributions :srcset: /auto_examples/others/images/sphx_glr_plot_factored_coupling_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Source and target distributions') .. GENERATED FROM PYTHON SOURCE LINES 50-52 Compute Factored OT and exact OT solutions ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python M = ot.dist(xs, xt) G0 = ot.emd(a, b, M) .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python Ga, Gb, xb = ot.factored_optimal_transport(xs, xt, a, b, r=4) .. GENERATED FROM PYTHON SOURCE LINES 63-65 Plot factored OT and exact OT solutions --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 65-87 .. code-block:: Python pl.figure(2, (14, 4)) pl.subplot(1, 3, 1) ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.2, .2, .2], alpha=0.1) pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.title('Exact OT with samples') pl.subplot(1, 3, 2) ot.plot.plot2D_samples_mat(xs, xb, Ga, c=[.6, .6, .9], alpha=0.5) ot.plot.plot2D_samples_mat(xb, xt, Gb, c=[.9, .6, .6], alpha=0.5) pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.plot(xb[:, 0], xb[:, 1], 'og', label='Template samples') pl.title('Factored OT with template samples') pl.subplot(1, 3, 3) ot.plot.plot2D_samples_mat(xs, xt, Ga.dot(Gb), c=[.2, .2, .2], alpha=0.1) pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.title('Factored OT low rank OT plan') .. image-sg:: /auto_examples/others/images/sphx_glr_plot_factored_coupling_002.png :alt: Exact OT with samples, Factored OT with template samples, Factored OT low rank OT plan :srcset: /auto_examples/others/images/sphx_glr_plot_factored_coupling_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Factored OT low rank OT plan') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.132 seconds) .. _sphx_glr_download_auto_examples_others_plot_factored_coupling.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_factored_coupling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_factored_coupling.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_