summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_compute_emd.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'docs/source/auto_examples/plot_compute_emd.ipynb')
-rw-r--r--docs/source/auto_examples/plot_compute_emd.ipynb168
1 files changed, 84 insertions, 84 deletions
diff --git a/docs/source/auto_examples/plot_compute_emd.ipynb b/docs/source/auto_examples/plot_compute_emd.ipynb
index b9b8bc5..562eff8 100644
--- a/docs/source/auto_examples/plot_compute_emd.ipynb
+++ b/docs/source/auto_examples/plot_compute_emd.ipynb
@@ -1,126 +1,126 @@
{
- "nbformat_minor": 0,
- "nbformat": 4,
"cells": [
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "%matplotlib inline"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "%matplotlib inline"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"\n# Plot multiple EMD\n\n\nShows how to compute multiple EMD and Sinkhorn with two differnt\nground metrics and plot their values for diffeent distributions.\n\n\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import make_1D_gauss as gauss"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Generate data\n-------------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% parameters\n\nn = 100 # nb bins\nn_target = 50 # nb target distributions\n\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\nlst_m = np.linspace(20, 90, n_target)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\n\nB = np.zeros((n, n_target))\n\nfor i, m in enumerate(lst_m):\n B[:, i] = gauss(n, m=m, s=5)\n\n# loss matrix and normalization\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean')\nM /= M.max()\nM2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean')\nM2 /= M2.max()"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "#%% parameters\n\nn = 100 # nb bins\nn_target = 50 # nb target distributions\n\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\nlst_m = np.linspace(20, 90, n_target)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\n\nB = np.zeros((n, n_target))\n\nfor i, m in enumerate(lst_m):\n B[:, i] = gauss(n, m=m, s=5)\n\n# loss matrix and normalization\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean')\nM /= M.max()\nM2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean')\nM2 /= M2.max()"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Plot data\n---------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% plot the distributions\n\npl.figure(1)\npl.subplot(2, 1, 1)\npl.plot(x, a, 'b', label='Source distribution')\npl.title('Source distribution')\npl.subplot(2, 1, 2)\npl.plot(x, B, label='Target distributions')\npl.title('Target distributions')\npl.tight_layout()"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "#%% plot the distributions\n\npl.figure(1)\npl.subplot(2, 1, 1)\npl.plot(x, a, 'b', label='Source distribution')\npl.title('Source distribution')\npl.subplot(2, 1, 2)\npl.plot(x, B, label='Target distributions')\npl.title('Target distributions')\npl.tight_layout()"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Compute EMD for the different losses\n------------------------------------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% Compute and plot distributions and loss matrix\n\nd_emd = ot.emd2(a, B, M) # direct computation of EMD\nd_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M2\n\n\npl.figure(2)\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.title('EMD distances')\npl.legend()"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "#%% Compute and plot distributions and loss matrix\n\nd_emd = ot.emd2(a, B, M) # direct computation of EMD\nd_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M2\n\n\npl.figure(2)\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.title('EMD distances')\npl.legend()"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Compute Sinkhorn for the different losses\n-----------------------------------------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%%\nreg = 1e-2\nd_sinkhorn = ot.sinkhorn2(a, B, M, reg)\nd_sinkhorn2 = ot.sinkhorn2(a, B, M2, reg)\n\npl.figure(2)\npl.clf()\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.plot(d_sinkhorn, '+', label='Euclidean Sinkhorn')\npl.plot(d_sinkhorn2, '+', label='Squared Euclidean Sinkhorn')\npl.title('EMD distances')\npl.legend()\n\npl.show()"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
+ },
+ "outputs": [],
+ "source": [
+ "#%%\nreg = 1e-2\nd_sinkhorn = ot.sinkhorn2(a, B, M, reg)\nd_sinkhorn2 = ot.sinkhorn2(a, B, M2, reg)\n\npl.figure(2)\npl.clf()\npl.plot(d_emd, label='Euclidean EMD')\npl.plot(d_emd2, label='Squared Euclidean EMD')\npl.plot(d_sinkhorn, '+', label='Euclidean Sinkhorn')\npl.plot(d_sinkhorn2, '+', label='Squared Euclidean Sinkhorn')\npl.title('EMD distances')\npl.legend()\n\npl.show()"
+ ]
}
- ],
+ ],
"metadata": {
"kernelspec": {
- "display_name": "Python 2",
- "name": "python2",
- "language": "python"
- },
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
"language_info": {
- "mimetype": "text/x-python",
- "nbconvert_exporter": "python",
- "name": "python",
- "file_extension": ".py",
- "version": "2.7.12",
- "pygments_lexer": "ipython2",
"codemirror_mode": {
- "version": 2,
- "name": "ipython"
- }
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.6.5"
}
- }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
} \ No newline at end of file