Debian Codegraph @

examples/barycenters/plot_free_support_barycenter.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ========================================================
4 2D free support Wasserstein barycenters of distributions
5 ========================================================
6
7 Illustration of 2D Wasserstein and Sinkhorn barycenters if distributions are weighted
8 sum of Diracs.
9
10 """
11
12 # Authors: Vivien Seguy <[email protected]>
13 # Rémi Flamary <[email protected]>
14 # Eduardo Fernandes Montesuma <[email protected]>
15 #
16 # License: MIT License
17
18 # sphinx_gallery_thumbnail_number = 2
19
20 import numpy as np
21 import matplotlib.pylab as pl
22 import ot
23
24
25 # %%
26 # Generate data
27 # -------------
28
29 N = 2
30 d = 2
31
32 I1 = pl.imread("../../data/redcross.png").astype(np.float64)[::4, ::4, 2]
33 I2 = pl.imread("../../data/duck.png").astype(np.float64)[::4, ::4, 2]
34
35 sz = I2.shape[0]
36 XX, YY = np.meshgrid(np.arange(sz), np.arange(sz))
37
38 x1 = np.stack((XX[I1 == 0], YY[I1 == 0]), 1) * 1.0
39 x2 = np.stack((XX[I2 == 0] + 80, -YY[I2 == 0] + 32), 1) * 1.0
40 x3 = np.stack((XX[I2 == 0], -YY[I2 == 0] + 32), 1) * 1.0
41
42 measures_locations = [x1, x2]
43 measures_weights = [ot.unif(x1.shape[0]), ot.unif(x2.shape[0])]
44
45 pl.figure(1, (12, 4))
46 pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5)
47 pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5)
48 pl.title("Distributions")
49
50
51 # %%
52 # Compute free support Wasserstein barycenter
53 # -------------------------------------------
54
55 k = 200 # number of Diracs of the barycenter
56 X_init = np.random.normal(0.0, 1.0, (k, d)) # initial Dirac locations
57 b = (
58 np.ones((k,)) / k
59 ) # weights of the barycenter (it will not be optimized, only the locations are optimized)
60
61 X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b)
62
63 # %%
64 # Plot the Wasserstein barycenter
65 # -------------------------------
66
67 pl.figure(2, (8, 3))
68 pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5)
69 pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5)
70 pl.scatter(X[:, 0], X[:, 1], s=b * 1000, marker="s", label="2-Wasserstein barycenter")
71 pl.title("Data measures and their barycenter")
72 pl.legend(loc="lower right")
73 pl.show()
74
75 # %%
76 # Compute free support Sinkhorn barycenter
77
78 k = 200 # number of Diracs of the barycenter
79 X_init = np.random.normal(0.0, 1.0, (k, d)) # initial Dirac locations
80 b = (
81 np.ones((k,)) / k
82 ) # weights of the barycenter (it will not be optimized, only the locations are optimized)
83
84 X = ot.bregman.free_support_sinkhorn_barycenter(
85 measures_locations, measures_weights, X_init, 20, b, numItermax=15
86 )
87
88 # %%
89 # Plot the Wasserstein barycenter
90 # -------------------------------
91
92 pl.figure(2, (8, 3))
93 pl.scatter(x1[:, 0], x1[:, 1], alpha=0.5)
94 pl.scatter(x2[:, 0], x2[:, 1], alpha=0.5)
95 pl.scatter(X[:, 0], X[:, 1], s=b * 1000, marker="s", label="2-Wasserstein barycenter")
96 pl.title("Data measures and their barycenter")
97 pl.legend(loc="lower right")
98 pl.show()