You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3eceba7
Browse filesBrowse the repository at this point in the historyBrowse files
"\n# Low rank Gromov-Wasterstein between samples\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>Example added in release: 0.9.4.</p></div>\n\nComparison between entropic Gromov-Wasserstein and Low Rank Gromov Wasserstein [67]\non two curves in 2D and 3D, both sampled with 200 points.\n\nThe squared Euclidean distance is considered as the ground cost for both samples.\n\n[67] Scetbon, M., Peyr\u00e9, G. & Cuturi, M. (2022).\n\"Linear-Time GromovWasserstein Distances using Low Rank Couplings and Costs\".\nIn International Conference on Machine Learning (ICML), 2022.\n"
8
+
]
9
+
},
10
+
{
11
+
"cell_type": "code",
12
+
"execution_count": null,
13
+
"metadata": {
14
+
"collapsed": false
15
+
},
16
+
"outputs": [],
17
+
"source": [
18
+
"# Author: Laur\u00e8ne David <laurene.david@ip-paris.fr>\n#\n# License: MIT License\n#\n# sphinx_gallery_thumbnail_number = 3"
19
+
]
20
+
},
21
+
{
22
+
"cell_type": "code",
23
+
"execution_count": null,
24
+
"metadata": {
25
+
"collapsed": false
26
+
},
27
+
"outputs": [],
28
+
"source": [
29
+
"import numpy as np\nimport matplotlib.pylab as pl\nimport ot.plot\nimport time"
30
+
]
31
+
},
32
+
{
33
+
"cell_type": "markdown",
34
+
"metadata": {},
35
+
"source": [
36
+
"## Generate data\n\n"
37
+
]
38
+
},
39
+
{
40
+
"cell_type": "code",
41
+
"execution_count": null,
42
+
"metadata": {
43
+
"collapsed": false
44
+
},
45
+
"outputs": [],
46
+
"source": [
47
+
"n_samples = 200\n\n# Generate 2D and 3D curves\ntheta = np.linspace(-4 * np.pi, 4 * np.pi, n_samples)\nz = np.linspace(1, 2, n_samples)\nr = z**2 + 1\nx = r * np.sin(theta)\ny = r * np.cos(theta)\n\n# Source and target distribution\nX = np.concatenate([x.reshape(-1, 1), z.reshape(-1, 1)], axis=1)\nY = np.concatenate([x.reshape(-1, 1), y.reshape(-1, 1), z.reshape(-1, 1)], axis=1)"
"\n# Partial Wasserstein in 1D\n\nThis script demonstrates how to compute and visualize the Partial Wasserstein distance between two 1D discrete distributions using `ot.partial.partial_wasserstein_1d`.\n\nWe illustrate the intermediate transport plans for all `k = 1...n`, where `n = min(len(x_a), len(x_b))`.\n"
8
+
]
9
+
},
10
+
{
11
+
"cell_type": "code",
12
+
"execution_count": null,
13
+
"metadata": {
14
+
"collapsed": false
15
+
},
16
+
"outputs": [],
17
+
"source": [
18
+
"# sphinx_gallery_thumbnail_number = 5\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom ot.partial import partial_wasserstein_1d\n\n\ndef plot_partial_transport(\n ax, x_a, x_b, indices_a=None, indices_b=None, marginal_costs=None\n):\n y_a = np.ones_like(x_a)\n y_b = -np.ones_like(x_b)\n min_min = min(x_a.min(), x_b.min())\n max_max = max(x_a.max(), x_b.max())\n\n ax.plot([min_min - 1, max_max + 1], [1, 1], \"k-\", lw=0.5, alpha=0.5)\n ax.plot([min_min - 1, max_max + 1], [-1, -1], \"k-\", lw=0.5, alpha=0.5)\n\n # Plot transport lines\n if indices_a is not None and indices_b is not None:\n subset_a = np.sort(x_a[indices_a])\n subset_b = np.sort(x_b[indices_b])\n\n for x_a_i, x_b_j in zip(subset_a, subset_b):\n ax.plot([x_a_i, x_b_j], [1, -1], \"k--\", alpha=0.7)\n\n # Plot all points\n ax.plot(x_a, y_a, \"o\", color=\"C0\", label=\"x_a\", markersize=8)\n ax.plot(x_b, y_b, \"o\", color=\"C1\", label=\"x_b\", markersize=8)\n\n if marginal_costs is not None:\n k = len(marginal_costs)\n ax.set_title(\n f\"Partial Transport - k = {k}, Cumulative Cost = {sum(marginal_costs):.2f}\",\n fontsize=16,\n )\n else:\n ax.set_title(\"Original 1D Discrete Distributions\", fontsize=16)\n ax.legend(loc=\"upper right\", fontsize=14)\n ax.set_yticks([])\n ax.set_xticks([])\n ax.set_ylim(-2, 2)\n ax.set_xlim(min(x_a.min(), x_b.min()) - 1, max(x_a.max(), x_b.max()) + 1)\n ax.axis(\"off\")\n\n\n# Simulate two 1D discrete distributions\nnp.random.seed(0)\nn = 6\nx_a = np.sort(np.random.uniform(0, 10, size=n))\nx_b = np.sort(np.random.uniform(0, 10, size=n))\n\n# Plot original distributions\nplt.figure(figsize=(6, 2))\nplot_partial_transport(plt.gca(), x_a, x_b)\nplt.show()"
19
+
]
20
+
},
21
+
{
22
+
"cell_type": "code",
23
+
"execution_count": null,
24
+
"metadata": {
25
+
"collapsed": false
26
+
},
27
+
"outputs": [],
28
+
"source": [
29
+
"indices_a, indices_b, marginal_costs = partial_wasserstein_1d(x_a, x_b)\n\n# Compute cumulative cost\ncumulative_costs = np.cumsum(marginal_costs)\n\n# Visualize all partial transport plans\nfor k in range(n):\n plt.figure(figsize=(6, 2))\n plot_partial_transport(\n plt.gca(),\n x_a,\n x_b,\n indices_a[: k + 1],\n indices_b[: k + 1],\n marginal_costs[: k + 1],\n )\n plt.show()"
Copy file name to clipboardExpand all lines: _downloads/059a63fc6cb655dcd2f1c9a61bb9d7ec/plot_OT_2D_samples.ipynb
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
"cell_type": "markdown",
5
5
"metadata": {},
6
6
"source": [
7
-
"\n# Optimal Transport between 2D empirical distributions\n\nIllustration of 2D optimal transport between distributions that are weighted\nsum of Diracs. The OT matrix is plotted with the samples.\n"
7
+
"\n# Optimal Transport between empirical distributions\n\nIllustration of optimal transport between distributions in 2D that are weighted\nsum of Diracs. The OT matrix is plotted with the samples.\n"
0 commit comments