Note
Go to the end to download the full example code.
Plot Fused-Gromov-Wasserstein
This example first illustrates the computation of FGW for 1D measures estimated using a Conditional Gradient solver [24].
[24] Vayer Titouan, Chapel Laetitia, Flamary Rémi, Tavenard Romain and Courty Nicolas “Optimal Transport for structured data with application on graphs” International Conference on Machine Learning (ICML). 2019.
# Author: Titouan Vayer <titouan.vayer@irisa.fr>
#
# License: MIT License
# sphinx_gallery_thumbnail_number = 3
import matplotlib.pyplot as pl
import numpy as np
import ot
from ot.gromov import gromov_wasserstein, fused_gromov_wasserstein
Generate data
# parameters
# We create two 1D random measures
n = 20 # number of points in the first distribution
n2 = 30 # number of points in the second distribution
sig = 1 # std of first distribution
sig2 = 0.1 # std of second distribution
np.random.seed(0)
phi = np.arange(n)[:, None]
xs = phi + sig * np.random.randn(n, 1)
ys = np.vstack(
(np.ones((n // 2, 1)), 0 * np.ones((n // 2, 1)))
) + sig2 * np.random.randn(n, 1)
phi2 = np.arange(n2)[:, None]
xt = phi2 + sig * np.random.randn(n2, 1)
yt = np.vstack(
(np.ones((n2 // 2, 1)), 0 * np.ones((n2 // 2, 1)))
) + sig2 * np.random.randn(n2, 1)
yt = yt[::-1, :]
p = ot.unif(n)
q = ot.unif(n2)
Plot data
# plot the distributions
pl.figure(1, (7, 7))
pl.subplot(2, 1, 1)
pl.scatter(ys, xs, c=phi, s=70)
pl.ylabel("Feature value a", fontsize=20)
pl.title("$\mu=\sum_i \delta_{x_i,a_i}$", fontsize=25, y=1)
pl.xticks(())
pl.yticks(())
pl.subplot(2, 1, 2)
pl.scatter(yt, xt, c=phi2, s=70)
pl.xlabel("coordinates x/y", fontsize=25)
pl.ylabel("Feature value b", fontsize=20)
pl.title("$\\nu=\sum_j \delta_{y_j,b_j}$", fontsize=25, y=1)
pl.yticks(())
pl.tight_layout()
pl.show()