.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/others/plot_COOT.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_COOT.py: =================================================== Row and column alignments with CO-Optimal Transport =================================================== This example is designed to show how to use the CO-Optimal Transport [47]_ in POT. CO-Optimal Transport allows to calculate the distance between two **arbitrary-size** matrices, and to align their rows and columns. In this example, we consider two random matrices :math:`X_1` and :math:`X_2` defined by :math:`(X_1)_{i,j} = \cos(\frac{i}{n_1} \pi) + \cos(\frac{j}{d_1} \pi) + \sigma \mathcal N(0,1)` and :math:`(X_2)_{i,j} = \cos(\frac{i}{n_2} \pi) + \cos(\frac{j}{d_2} \pi) + \sigma \mathcal N(0,1)`. .. [49] Redko, I., Vayer, T., Flamary, R., and Courty, N. (2020). `CO-Optimal Transport `_. Advances in Neural Information Processing Systems, 33. .. GENERATED FROM PYTHON SOURCE LINES 18-29 .. code-block:: Python # Author: Remi Flamary # Quang Huy Tran # License: MIT License from matplotlib.patches import ConnectionPatch import matplotlib.pylab as pl import numpy as np from ot.coot import co_optimal_transport as coot from ot.coot import co_optimal_transport2 as coot2 .. GENERATED FROM PYTHON SOURCE LINES 30-31 Generating two random matrices .. GENERATED FROM PYTHON SOURCE LINES 31-49 .. code-block:: Python n1 = 20 n2 = 10 d1 = 16 d2 = 8 sigma = 0.2 X1 = ( np.cos(np.arange(n1) * np.pi / n1)[:, None] + np.cos(np.arange(d1) * np.pi / d1)[None, :] + sigma * np.random.randn(n1, d1) ) X2 = ( np.cos(np.arange(n2) * np.pi / n2)[:, None] + np.cos(np.arange(d2) * np.pi / d2)[None, :] + sigma * np.random.randn(n2, d2) ) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Visualizing the matrices .. GENERATED FROM PYTHON SOURCE LINES 51-63 .. code-block:: Python pl.figure(1, (8, 5)) pl.subplot(1, 2, 1) pl.imshow(X1) pl.title('$X_1$') pl.subplot(1, 2, 2) pl.imshow(X2) pl.title("$X_2$") pl.tight_layout() .. image-sg:: /auto_examples/others/images/sphx_glr_plot_COOT_001.png :alt: $X_1$, $X_2$ :srcset: /auto_examples/others/images/sphx_glr_plot_COOT_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-65 Visualizing the alignments of rows and columns, and calculating the CO-Optimal Transport distance .. GENERATED FROM PYTHON SOURCE LINES 65-98 .. code-block:: Python pi_sample, pi_feature, log = coot(X1, X2, log=True, verbose=True) coot_distance = coot2(X1, X2) print('CO-Optimal Transport distance = {:.5f}'.format(coot_distance)) fig = pl.figure(4, (9, 7)) pl.clf() ax1 = pl.subplot(2, 2, 3) pl.imshow(X1) pl.xlabel('$X_1$') ax2 = pl.subplot(2, 2, 2) ax2.yaxis.tick_right() pl.imshow(np.transpose(X2)) pl.title("Transpose($X_2$)") ax2.xaxis.tick_top() for i in range(n1): j = np.argmax(pi_sample[i, :]) xyA = (d1 - .5, i) xyB = (j, d2 - .5) con = ConnectionPatch(xyA=xyA, xyB=xyB, coordsA=ax1.transData, coordsB=ax2.transData, color="black") fig.add_artist(con) for i in range(d1): j = np.argmax(pi_feature[i, :]) xyA = (i, -.5) xyB = (-.5, j) con = ConnectionPatch( xyA=xyA, xyB=xyB, coordsA=ax1.transData, coordsB=ax2.transData, color="blue") fig.add_artist(con) .. image-sg:: /auto_examples/others/images/sphx_glr_plot_COOT_002.png :alt: Transpose($X_2$) :srcset: /auto_examples/others/images/sphx_glr_plot_COOT_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none CO-Optimal Transport cost at iteration 1: 0.10903416567681642 CO-Optimal Transport cost at iteration 2: 0.0987568083692249 CO-Optimal Transport cost at iteration 3: 0.09685398028260428 CO-Optimal Transport distance = 0.09685 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.401 seconds) .. _sphx_glr_download_auto_examples_others_plot_COOT.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_COOT.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_COOT.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_