summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_OT_2D_samples.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'docs/source/auto_examples/plot_OT_2D_samples.ipynb')
-rw-r--r--docs/source/auto_examples/plot_OT_2D_samples.ipynb168
1 files changed, 84 insertions, 84 deletions
diff --git a/docs/source/auto_examples/plot_OT_2D_samples.ipynb b/docs/source/auto_examples/plot_OT_2D_samples.ipynb
index 41a37f3..26831f9 100644
--- a/docs/source/auto_examples/plot_OT_2D_samples.ipynb
+++ b/docs/source/auto_examples/plot_OT_2D_samples.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# 2D Optimal transport between empirical distributions\n\n\nIllustration of 2D optimal transport between discributions that are weighted\nsum of diracs. The OT matrix is plotted with the samples.\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"
- ],
- "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\nimport ot.plot"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Generate data\n-------------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% parameters and data generation\n\nn = 50 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4])\ncov_t = np.array([[1, -.8], [-.8, 1]])\n\nxs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s)\nxt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t)\n\na, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples\n\n# loss matrix\nM = ot.dist(xs, xt)\nM /= M.max()"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "#%% parameters and data generation\n\nn = 50 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4])\ncov_t = np.array([[1, -.8], [-.8, 1]])\n\nxs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s)\nxt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t)\n\na, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples\n\n# loss matrix\nM = ot.dist(xs, xt)\nM /= M.max()"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Plot data\n---------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% plot samples\n\npl.figure(1)\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')\n\npl.figure(2)\npl.imshow(M, interpolation='nearest')\npl.title('Cost matrix M')"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "#%% plot samples\n\npl.figure(1)\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')\n\npl.figure(2)\npl.imshow(M, interpolation='nearest')\npl.title('Cost matrix M')"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Compute EMD\n-----------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3)\npl.imshow(G0, interpolation='nearest')\npl.title('OT matrix G0')\n\npl.figure(4)\not.plot.plot2D_samples_mat(xs, xt, G0, 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.legend(loc=0)\npl.title('OT matrix with samples')"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
- },
+ },
+ "outputs": [],
+ "source": [
+ "#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3)\npl.imshow(G0, interpolation='nearest')\npl.title('OT matrix G0')\n\npl.figure(4)\not.plot.plot2D_samples_mat(xs, xt, G0, 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.legend(loc=0)\npl.title('OT matrix with samples')"
+ ]
+ },
{
+ "cell_type": "markdown",
+ "metadata": {},
"source": [
"Compute Sinkhorn\n----------------\n\n"
- ],
- "cell_type": "markdown",
- "metadata": {}
- },
+ ]
+ },
{
- "execution_count": null,
- "cell_type": "code",
- "source": [
- "#%% sinkhorn\n\n# reg term\nlambd = 1e-3\n\nGs = ot.sinkhorn(a, b, M, lambd)\n\npl.figure(5)\npl.imshow(Gs, interpolation='nearest')\npl.title('OT matrix sinkhorn')\n\npl.figure(6)\not.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('OT matrix Sinkhorn with samples')\n\npl.show()"
- ],
- "outputs": [],
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {
"collapsed": false
- }
+ },
+ "outputs": [],
+ "source": [
+ "#%% sinkhorn\n\n# reg term\nlambd = 1e-3\n\nGs = ot.sinkhorn(a, b, M, lambd)\n\npl.figure(5)\npl.imshow(Gs, interpolation='nearest')\npl.title('OT matrix sinkhorn')\n\npl.figure(6)\not.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1])\npl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\npl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\npl.legend(loc=0)\npl.title('OT matrix Sinkhorn with samples')\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