summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb')
-rw-r--r--docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb76
1 files changed, 74 insertions, 2 deletions
diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb b/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb
index 46283ac..2b9a364 100644
--- a/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb
+++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.ipynb
@@ -15,7 +15,7 @@
},
{
"source": [
- "\n# 2D Optimal transport for different metrics\n\n\nStole the figure idea from Fig. 1 and 2 in \nhttps://arxiv.org/pdf/1706.07650.pdf\n\n\n@author: rflamary\n\n"
+ "\n# 2D Optimal transport for different metrics\n\n\n2D OT on empirical distributio with different gound metric.\n\nStole the figure idea from Fig. 1 and 2 in\nhttps://arxiv.org/pdf/1706.07650.pdf\n\n\n\n"
],
"cell_type": "markdown",
"metadata": {}
@@ -24,7 +24,79 @@
"execution_count": null,
"cell_type": "code",
"source": [
- "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n#%% parameters and data generation\n\nfor data in range(2):\n\n if data:\n n=20 # nb samples\n xs=np.zeros((n,2))\n xs[:,0]=np.arange(n)+1\n xs[:,1]=(np.arange(n)+1)*-0.001 # to make it strictly convex...\n \n xt=np.zeros((n,2))\n xt[:,1]=np.arange(n)+1\n else:\n \n n=50 # nb samples\n xtot=np.zeros((n+1,2))\n xtot[:,0]=np.cos((np.arange(n+1)+1.0)*0.9/(n+2)*2*np.pi)\n xtot[:,1]=np.sin((np.arange(n+1)+1.0)*0.9/(n+2)*2*np.pi)\n \n xs=xtot[:n,:]\n xt=xtot[1:,:]\n \n \n \n a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples\n \n # loss matrix\n M1=ot.dist(xs,xt,metric='euclidean')\n M1/=M1.max()\n \n # loss matrix\n M2=ot.dist(xs,xt,metric='sqeuclidean')\n M2/=M2.max()\n \n # loss matrix\n Mp=np.sqrt(ot.dist(xs,xt,metric='euclidean'))\n Mp/=Mp.max()\n \n #%% plot samples\n \n pl.figure(1+3*data)\n pl.clf()\n pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n pl.axis('equal')\n pl.title('Source and traget distributions')\n \n pl.figure(2+3*data,(15,5))\n pl.subplot(1,3,1)\n pl.imshow(M1,interpolation='nearest')\n pl.title('Eucidean cost')\n pl.subplot(1,3,2)\n pl.imshow(M2,interpolation='nearest')\n pl.title('Squared Euclidean cost')\n \n pl.subplot(1,3,3)\n pl.imshow(Mp,interpolation='nearest')\n pl.title('Sqrt Euclidean cost')\n #%% EMD\n \n G1=ot.emd(a,b,M1)\n G2=ot.emd(a,b,M2)\n Gp=ot.emd(a,b,Mp)\n \n pl.figure(3+3*data,(15,5))\n \n pl.subplot(1,3,1)\n ot.plot.plot2D_samples_mat(xs,xt,G1,c=[.5,.5,1])\n pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n pl.axis('equal')\n #pl.legend(loc=0)\n pl.title('OT Euclidean')\n \n pl.subplot(1,3,2)\n \n ot.plot.plot2D_samples_mat(xs,xt,G2,c=[.5,.5,1])\n pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n pl.axis('equal')\n #pl.legend(loc=0)\n pl.title('OT squared Euclidean')\n \n pl.subplot(1,3,3)\n \n ot.plot.plot2D_samples_mat(xs,xt,Gp,c=[.5,.5,1])\n pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples')\n pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples')\n pl.axis('equal')\n #pl.legend(loc=0)\n pl.title('OT sqrt Euclidean')"
+ "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot"
+ ],
+ "outputs": [],
+ "metadata": {
+ "collapsed": false
+ }
+ },
+ {
+ "source": [
+ "Dataset 1 : uniform sampling\n----------------------------\n\n"
+ ],
+ "cell_type": "markdown",
+ "metadata": {}
+ },
+ {
+ "execution_count": null,
+ "cell_type": "code",
+ "source": [
+ "n = 20 # nb samples\nxs = np.zeros((n, 2))\nxs[:, 0] = np.arange(n) + 1\nxs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex...\n\nxt = np.zeros((n, 2))\nxt[:, 1] = np.arange(n) + 1\n\na, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM1 = ot.dist(xs, xt, metric='euclidean')\nM1 /= M1.max()\n\n# loss matrix\nM2 = ot.dist(xs, xt, metric='sqeuclidean')\nM2 /= M2.max()\n\n# loss matrix\nMp = np.sqrt(ot.dist(xs, xt, metric='euclidean'))\nMp /= Mp.max()\n\n# Data\npl.figure(1, figsize=(7, 3))\npl.clf()\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\npl.title('Source and traget distributions')\n\n\n# Cost matrices\npl.figure(2, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\npl.imshow(M1, interpolation='nearest')\npl.title('Euclidean cost')\n\npl.subplot(1, 3, 2)\npl.imshow(M2, interpolation='nearest')\npl.title('Squared Euclidean cost')\n\npl.subplot(1, 3, 3)\npl.imshow(Mp, interpolation='nearest')\npl.title('Sqrt Euclidean cost')\npl.tight_layout()"
+ ],
+ "outputs": [],
+ "metadata": {
+ "collapsed": false
+ }
+ },
+ {
+ "source": [
+ "Dataset 1 : Plot OT Matrices\n----------------------------\n\n"
+ ],
+ "cell_type": "markdown",
+ "metadata": {}
+ },
+ {
+ "execution_count": null,
+ "cell_type": "code",
+ "source": [
+ "#%% EMD\nG1 = ot.emd(a, b, M1)\nG2 = ot.emd(a, b, M2)\nGp = ot.emd(a, b, Mp)\n\n# OT matrices\npl.figure(3, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\not.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT Euclidean')\n\npl.subplot(1, 3, 2)\not.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT squared Euclidean')\n\npl.subplot(1, 3, 3)\not.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT sqrt Euclidean')\npl.tight_layout()\n\npl.show()"
+ ],
+ "outputs": [],
+ "metadata": {
+ "collapsed": false
+ }
+ },
+ {
+ "source": [
+ "Dataset 2 : Partial circle\n--------------------------\n\n"
+ ],
+ "cell_type": "markdown",
+ "metadata": {}
+ },
+ {
+ "execution_count": null,
+ "cell_type": "code",
+ "source": [
+ "n = 50 # nb samples\nxtot = np.zeros((n + 1, 2))\nxtot[:, 0] = np.cos(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\nxtot[:, 1] = np.sin(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n\nxs = xtot[:n, :]\nxt = xtot[1:, :]\n\na, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM1 = ot.dist(xs, xt, metric='euclidean')\nM1 /= M1.max()\n\n# loss matrix\nM2 = ot.dist(xs, xt, metric='sqeuclidean')\nM2 /= M2.max()\n\n# loss matrix\nMp = np.sqrt(ot.dist(xs, xt, metric='euclidean'))\nMp /= Mp.max()\n\n\n# Data\npl.figure(4, figsize=(7, 3))\npl.clf()\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\npl.title('Source and traget distributions')\n\n\n# Cost matrices\npl.figure(5, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\npl.imshow(M1, interpolation='nearest')\npl.title('Euclidean cost')\n\npl.subplot(1, 3, 2)\npl.imshow(M2, interpolation='nearest')\npl.title('Squared Euclidean cost')\n\npl.subplot(1, 3, 3)\npl.imshow(Mp, interpolation='nearest')\npl.title('Sqrt Euclidean cost')\npl.tight_layout()"
+ ],
+ "outputs": [],
+ "metadata": {
+ "collapsed": false
+ }
+ },
+ {
+ "source": [
+ "Dataset 2 : Plot OT Matrices\n-----------------------------\n\n"
+ ],
+ "cell_type": "markdown",
+ "metadata": {}
+ },
+ {
+ "execution_count": null,
+ "cell_type": "code",
+ "source": [
+ "#%% EMD\nG1 = ot.emd(a, b, M1)\nG2 = ot.emd(a, b, M2)\nGp = ot.emd(a, b, Mp)\n\n# OT matrices\npl.figure(6, figsize=(7, 3))\n\npl.subplot(1, 3, 1)\not.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT Euclidean')\n\npl.subplot(1, 3, 2)\not.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT squared Euclidean')\n\npl.subplot(1, 3, 3)\not.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.axis('equal')\n# pl.legend(loc=0)\npl.title('OT sqrt Euclidean')\npl.tight_layout()\n\npl.show()"
],
"outputs": [],
"metadata": {