diff options
58 files changed, 797 insertions, 621 deletions
diff --git a/docs/nb_build b/docs/nb_build index e0afbcd..2523d71 100755 --- a/docs/nb_build +++ b/docs/nb_build @@ -3,8 +3,10 @@ # remove comment sed -i "s/#'sphinx\_gallery/'sphinx\_gallery/" source/conf.py +sed -i "s/sys.modules.update/#sys.modules.update/" source/conf.py make html # put comment again sed -i "s/'sphinx\_gallery/#'sphinx\_gallery/" source/conf.py +sed -i "s/#sys.modules.update/sys.modules.update/" source/conf.py diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip Binary files differindex 7c3de28..3bb8ecf 100644 --- a/docs/source/auto_examples/auto_examples_jupyter.zip +++ b/docs/source/auto_examples/auto_examples_jupyter.zip diff --git a/docs/source/auto_examples/auto_examples_python.zip b/docs/source/auto_examples/auto_examples_python.zip Binary files differindex 97377e1..273c751 100644 --- a/docs/source/auto_examples/auto_examples_python.zip +++ b/docs/source/auto_examples/auto_examples_python.zip diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_001.png Binary files differindex da42bc1..e11f5b9 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_001.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_002.png Binary files differindex 1f98598..fcab0bd 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_002.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_002.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png Binary files differindex 9e893d6..a75e649 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png Binary files differindex 3bc248b..96b42cd 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png Binary files differindex e023ab4..e675cd8 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png Binary files differindex dda21d4..8c29b7b 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_002.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png Binary files differindex f0967fb..1308674 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png Binary files differindex 809c8fc..95d947e 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png Binary files differindex 887bdde..82ab78c 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_005.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png Binary files differindex 783c594..f1d2bfe 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_2D_samples_006.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png Binary files differindex b159a6a..22dba2b 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png Binary files differindex 9f8e882..5dbf96b 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_002.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png Binary files differindex 33058fc..e1e9ba8 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_004.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_004.png Binary files differindex 9848bcb..6a21f35 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_004.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png Binary files differindex 6616d3c..79e4710 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_005.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_006.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_006.png Binary files differindex 8575d93..4860d96 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_006.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_L1_vs_L2_006.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png b/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png Binary files differindex 250155d..41ec230 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_WDA_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_001.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_001.png Binary files differindex be71674..3454396 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_001.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_002.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_002.png Binary files differindex f62240b..3b23af5 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_002.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_002.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png Binary files differindex 11f08b2..eac9230 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_004.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_004.png Binary files differindex b4e8f71..2e29ff9 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_004.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_001.png b/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_001.png Binary files differindex 4917903..9cf84c6 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_001.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_compute_emd_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_003.png b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_003.png Binary files differindex 7ffcc14..c9ed489 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_004.png b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_004.png Binary files differindex 2a72060..0af4542 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_004.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png Binary files differindex e70a6de..8a4882a 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_optim_OTreg_005.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png Binary files differindex 15c9825..63ff40c 100644 --- a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png Binary files differindex bac78f0..48e1449 100644 --- a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png Binary files differindex c67e8aa..9deebf0 100644 --- a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png Binary files differindex 84759e8..4b409a0 100644 --- a/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png Binary files differindex 86ff19f..5c17671 100644 --- a/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png Binary files differindex 67d2ca1..68cbdf7 100644 --- a/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index 1695300..b932907 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -3,7 +3,7 @@ POT Examples .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="@author: rflamary "> + <div class="sphx-glr-thumbcontainer" tooltip=""> .. only:: html @@ -23,13 +23,13 @@ POT Examples .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="@author: rflamary "> + <div class="sphx-glr-thumbcontainer" tooltip=" "> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_optim_OTreg_thumb.png - :ref:`sphx_glr_auto_examples_plot_WDA.py` + :ref:`sphx_glr_auto_examples_plot_optim_OTreg.py` .. raw:: html @@ -39,17 +39,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_WDA + /auto_examples/plot_optim_OTreg .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip=" "> + <div class="sphx-glr-thumbcontainer" tooltip=""> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_optim_OTreg_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png - :ref:`sphx_glr_auto_examples_plot_optim_OTreg.py` + :ref:`sphx_glr_auto_examples_plot_OT_2D_samples.py` .. raw:: html @@ -59,17 +59,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_optim_OTreg + /auto_examples/plot_OT_2D_samples .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="@author: rflamary "> + <div class="sphx-glr-thumbcontainer" tooltip=""> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_2D_samples_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png - :ref:`sphx_glr_auto_examples_plot_OT_2D_samples.py` + :ref:`sphx_glr_auto_examples_plot_compute_emd.py` .. raw:: html @@ -79,17 +79,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OT_2D_samples + /auto_examples/plot_compute_emd .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="@author: rflamary "> + <div class="sphx-glr-thumbcontainer" tooltip=""> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_compute_emd_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_WDA_thumb.png - :ref:`sphx_glr_auto_examples_plot_compute_emd.py` + :ref:`sphx_glr_auto_examples_plot_WDA.py` .. raw:: html @@ -99,17 +99,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_compute_emd + /auto_examples/plot_WDA .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete optima..."> + <div class="sphx-glr-thumbcontainer" tooltip="This example presents a way of transferring colors between two image with Optimal Transport as ..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OTDA_color_images_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png - :ref:`sphx_glr_auto_examples_plot_OTDA_color_images.py` + :ref:`sphx_glr_auto_examples_plot_otda_color_images.py` .. raw:: html @@ -119,7 +119,7 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OTDA_color_images + /auto_examples/plot_otda_color_images .. raw:: html @@ -127,9 +127,9 @@ POT Examples .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OTDA_classes_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png - :ref:`sphx_glr_auto_examples_plot_OTDA_classes.py` + :ref:`sphx_glr_auto_examples_plot_barycenter_1D.py` .. raw:: html @@ -139,17 +139,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OTDA_classes + /auto_examples/plot_barycenter_1D .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip=""> + <div class="sphx-glr-thumbcontainer" tooltip="Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf"> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OTDA_2D_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png - :ref:`sphx_glr_auto_examples_plot_OTDA_2D.py` + :ref:`sphx_glr_auto_examples_plot_OT_L1_vs_L2.py` .. raw:: html @@ -159,17 +159,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OTDA_2D + /auto_examples/plot_OT_L1_vs_L2 .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf"> + <div class="sphx-glr-thumbcontainer" tooltip="[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete op..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_L1_vs_L2_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png - :ref:`sphx_glr_auto_examples_plot_OT_L1_vs_L2.py` + :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` .. raw:: html @@ -179,17 +179,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OT_L1_vs_L2 + /auto_examples/plot_otda_mapping_colors_images .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip=" @author: rflamary "> + <div class="sphx-glr-thumbcontainer" tooltip="This example presents how to use MappingTransport to estimate at the same time both the couplin..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_barycenter_1D_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png - :ref:`sphx_glr_auto_examples_plot_barycenter_1D.py` + :ref:`sphx_glr_auto_examples_plot_otda_mapping.py` .. raw:: html @@ -199,17 +199,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_barycenter_1D + /auto_examples/plot_otda_mapping .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized discrete op..."> + <div class="sphx-glr-thumbcontainer" tooltip="This example introduces a domain adaptation in a 2D setting and the 4 OTDA approaches currently..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_color_images_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png - :ref:`sphx_glr_auto_examples_plot_OTDA_mapping_color_images.py` + :ref:`sphx_glr_auto_examples_plot_otda_classes.py` .. raw:: html @@ -219,17 +219,17 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OTDA_mapping_color_images + /auto_examples/plot_otda_classes .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for discrete optimal ..."> + <div class="sphx-glr-thumbcontainer" tooltip="This example introduces a domain adaptation in a 2D setting. It explicits the problem of domain..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OTDA_mapping_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png - :ref:`sphx_glr_auto_examples_plot_OTDA_mapping.py` + :ref:`sphx_glr_auto_examples_plot_otda_d2.py` .. raw:: html @@ -239,7 +239,7 @@ POT Examples .. toctree:: :hidden: - /auto_examples/plot_OTDA_mapping + /auto_examples/plot_otda_d2 .. raw:: html <div style='clear:both'></div> diff --git a/docs/source/auto_examples/plot_OT_1D.ipynb b/docs/source/auto_examples/plot_OT_1D.ipynb index 8715b97..7d3fdd1 100644 --- a/docs/source/auto_examples/plot_OT_1D.ipynb +++ b/docs/source/auto_examples/plot_OT_1D.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 1D optimal transport\n\n\n@author: rflamary\n\n" + "\n# 1D optimal transport\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss\n\n\n#%% parameters\n\nn=100 # nb bins\n\n# bin positions\nx=np.arange(n,dtype=np.float64)\n\n# Gaussian distributions\na=gauss(n,m=20,s=5) # m= mean, s= std\nb=gauss(n,m=60,s=10)\n\n# loss matrix\nM=ot.dist(x.reshape((n,1)),x.reshape((n,1)))\nM/=M.max()\n\n#%% plot the distributions\n\npl.figure(1)\npl.plot(x,a,'b',label='Source distribution')\npl.plot(x,b,'r',label='Target distribution')\npl.legend()\n\n#%% plot distributions and loss matrix\n\npl.figure(2)\not.plot.plot1D_mat(a,b,M,'Cost matrix M')\n\n#%% EMD\n\nG0=ot.emd(a,b,M)\n\npl.figure(3)\not.plot.plot1D_mat(a,b,G0,'OT matrix G0')\n\n#%% Sinkhorn\n\nlambd=1e-3\nGs=ot.sinkhorn(a,b,M,lambd,verbose=True)\n\npl.figure(4)\not.plot.plot1D_mat(a,b,Gs,'OT matrix Sinkhorn')" + "# 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\n\n#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\nb = gauss(n, m=60, s=10)\n\n# loss matrix\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))\nM /= M.max()\n\n#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\npl.plot(x, a, 'b', label='Source distribution')\npl.plot(x, b, 'r', label='Target distribution')\npl.legend()\n\n#%% plot distributions and loss matrix\n\npl.figure(2, figsize=(5, 5))\not.plot.plot1D_mat(a, b, M, 'Cost matrix M')\n\n#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3, figsize=(5, 5))\not.plot.plot1D_mat(a, b, G0, 'OT matrix G0')\n\n#%% Sinkhorn\n\nlambd = 1e-3\nGs = ot.sinkhorn(a, b, M, lambd, verbose=True)\n\npl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn')\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_OT_1D.py b/docs/source/auto_examples/plot_OT_1D.py index 6661aa3..0f3a26a 100644 --- a/docs/source/auto_examples/plot_OT_1D.py +++ b/docs/source/auto_examples/plot_OT_1D.py @@ -4,53 +4,57 @@ 1D optimal transport ==================== -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot from ot.datasets import get_1D_gauss as gauss - #%% parameters -n=100 # nb bins +n = 100 # nb bins # bin positions -x=np.arange(n,dtype=np.float64) +x = np.arange(n, dtype=np.float64) # Gaussian distributions -a=gauss(n,m=20,s=5) # m= mean, s= std -b=gauss(n,m=60,s=10) +a = gauss(n, m=20, s=5) # m= mean, s= std +b = gauss(n, m=60, s=10) # loss matrix -M=ot.dist(x.reshape((n,1)),x.reshape((n,1))) -M/=M.max() +M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) +M /= M.max() #%% plot the distributions -pl.figure(1) -pl.plot(x,a,'b',label='Source distribution') -pl.plot(x,b,'r',label='Target distribution') +pl.figure(1, figsize=(6.4, 3)) +pl.plot(x, a, 'b', label='Source distribution') +pl.plot(x, b, 'r', label='Target distribution') pl.legend() #%% plot distributions and loss matrix -pl.figure(2) -ot.plot.plot1D_mat(a,b,M,'Cost matrix M') +pl.figure(2, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') #%% EMD -G0=ot.emd(a,b,M) +G0 = ot.emd(a, b, M) -pl.figure(3) -ot.plot.plot1D_mat(a,b,G0,'OT matrix G0') +pl.figure(3, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') #%% Sinkhorn -lambd=1e-3 -Gs=ot.sinkhorn(a,b,M,lambd,verbose=True) +lambd = 1e-3 +Gs = ot.sinkhorn(a, b, M, lambd, verbose=True) + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn') -pl.figure(4) -ot.plot.plot1D_mat(a,b,Gs,'OT matrix Sinkhorn') +pl.show() diff --git a/docs/source/auto_examples/plot_OT_1D.rst b/docs/source/auto_examples/plot_OT_1D.rst index 44b715b..a36e13c 100644 --- a/docs/source/auto_examples/plot_OT_1D.rst +++ b/docs/source/auto_examples/plot_OT_1D.rst @@ -7,7 +7,6 @@ 1D optimal transport ==================== -@author: rflamary @@ -64,55 +63,60 @@ .. code-block:: python + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + import numpy as np import matplotlib.pylab as pl import ot from ot.datasets import get_1D_gauss as gauss - #%% parameters - n=100 # nb bins + n = 100 # nb bins # bin positions - x=np.arange(n,dtype=np.float64) + x = np.arange(n, dtype=np.float64) # Gaussian distributions - a=gauss(n,m=20,s=5) # m= mean, s= std - b=gauss(n,m=60,s=10) + a = gauss(n, m=20, s=5) # m= mean, s= std + b = gauss(n, m=60, s=10) # loss matrix - M=ot.dist(x.reshape((n,1)),x.reshape((n,1))) - M/=M.max() + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M /= M.max() #%% plot the distributions - pl.figure(1) - pl.plot(x,a,'b',label='Source distribution') - pl.plot(x,b,'r',label='Target distribution') + pl.figure(1, figsize=(6.4, 3)) + pl.plot(x, a, 'b', label='Source distribution') + pl.plot(x, b, 'r', label='Target distribution') pl.legend() #%% plot distributions and loss matrix - pl.figure(2) - ot.plot.plot1D_mat(a,b,M,'Cost matrix M') + pl.figure(2, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') #%% EMD - G0=ot.emd(a,b,M) + G0 = ot.emd(a, b, M) - pl.figure(3) - ot.plot.plot1D_mat(a,b,G0,'OT matrix G0') + pl.figure(3, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') #%% Sinkhorn - lambd=1e-3 - Gs=ot.sinkhorn(a,b,M,lambd,verbose=True) + lambd = 1e-3 + Gs = ot.sinkhorn(a, b, M, lambd, verbose=True) + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn') - pl.figure(4) - ot.plot.plot1D_mat(a,b,Gs,'OT matrix Sinkhorn') + pl.show() -**Total running time of the script:** ( 0 minutes 0.674 seconds) +**Total running time of the script:** ( 0 minutes 1.050 seconds) diff --git a/docs/source/auto_examples/plot_OT_2D_samples.ipynb b/docs/source/auto_examples/plot_OT_2D_samples.ipynb index fad0467..fc4ce50 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.ipynb +++ b/docs/source/auto_examples/plot_OT_2D_samples.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 2D Optimal transport between empirical distributions\n\n\n@author: rflamary\n\n" + "\n# 2D Optimal transport between empirical distributions\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,7 @@ "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\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 = ot.unif(n),ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM=ot.dist(xs,xt)\nM/=M.max()\n\n#%% 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 traget distributions')\n\npl.figure(2)\npl.imshow(M,interpolation='nearest')\npl.title('Cost matrix M')\n\n\n#%% 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')\n\n\n#%% sinkhorn\n\n# reg term\nlambd=5e-4\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')" + "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n#%% 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()\n\n#%% 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')\n\n\n#%% 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')\n\n\n#%% 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": [], "metadata": { diff --git a/docs/source/auto_examples/plot_OT_2D_samples.py b/docs/source/auto_examples/plot_OT_2D_samples.py index edfb781..2a42dc0 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.py +++ b/docs/source/auto_examples/plot_OT_2D_samples.py @@ -4,57 +4,60 @@ 2D Optimal transport between empirical distributions ==================================================== -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot #%% parameters and data generation -n=50 # nb samples +n = 50 # nb samples -mu_s=np.array([0,0]) -cov_s=np.array([[1,0],[0,1]]) +mu_s = np.array([0, 0]) +cov_s = np.array([[1, 0], [0, 1]]) -mu_t=np.array([4,4]) -cov_t=np.array([[1,-.8],[-.8,1]]) +mu_t = np.array([4, 4]) +cov_t = np.array([[1, -.8], [-.8, 1]]) -xs=ot.datasets.get_2D_samples_gauss(n,mu_s,cov_s) -xt=ot.datasets.get_2D_samples_gauss(n,mu_t,cov_t) +xs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s) +xt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t) -a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples +a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples # loss matrix -M=ot.dist(xs,xt) -M/=M.max() +M = ot.dist(xs, xt) +M /= M.max() #%% plot samples pl.figure(1) -pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') -pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) -pl.title('Source and traget distributions') +pl.title('Source and target distributions') pl.figure(2) -pl.imshow(M,interpolation='nearest') +pl.imshow(M, interpolation='nearest') pl.title('Cost matrix M') #%% EMD -G0=ot.emd(a,b,M) +G0 = ot.emd(a, b, M) pl.figure(3) -pl.imshow(G0,interpolation='nearest') +pl.imshow(G0, interpolation='nearest') pl.title('OT matrix G0') pl.figure(4) -ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) -pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') -pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') +ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) pl.title('OT matrix with samples') @@ -62,17 +65,19 @@ pl.title('OT matrix with samples') #%% sinkhorn # reg term -lambd=5e-4 +lambd = 1e-3 -Gs=ot.sinkhorn(a,b,M,lambd) +Gs = ot.sinkhorn(a, b, M, lambd) pl.figure(5) -pl.imshow(Gs,interpolation='nearest') +pl.imshow(Gs, interpolation='nearest') pl.title('OT matrix sinkhorn') pl.figure(6) -ot.plot.plot2D_samples_mat(xs,xt,Gs,color=[.5,.5,1]) -pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') -pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') +ot.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1]) +pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') +pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) pl.title('OT matrix Sinkhorn with samples') + +pl.show() diff --git a/docs/source/auto_examples/plot_OT_2D_samples.rst b/docs/source/auto_examples/plot_OT_2D_samples.rst index e05e591..c472c6a 100644 --- a/docs/source/auto_examples/plot_OT_2D_samples.rst +++ b/docs/source/auto_examples/plot_OT_2D_samples.rst @@ -7,7 +7,6 @@ 2D Optimal transport between empirical distributions ==================================================== -@author: rflamary @@ -46,69 +45,64 @@ :scale: 47 -.. rst-class:: sphx-glr-script-out - Out:: - - ('Warning: numerical errors at iteration', 0) - - - - -| .. code-block:: python + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + import numpy as np import matplotlib.pylab as pl import ot #%% parameters and data generation - n=50 # nb samples + n = 50 # nb samples - mu_s=np.array([0,0]) - cov_s=np.array([[1,0],[0,1]]) + mu_s = np.array([0, 0]) + cov_s = np.array([[1, 0], [0, 1]]) - mu_t=np.array([4,4]) - cov_t=np.array([[1,-.8],[-.8,1]]) + mu_t = np.array([4, 4]) + cov_t = np.array([[1, -.8], [-.8, 1]]) - xs=ot.datasets.get_2D_samples_gauss(n,mu_s,cov_s) - xt=ot.datasets.get_2D_samples_gauss(n,mu_t,cov_t) + xs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s) + xt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t) - a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples + a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples # loss matrix - M=ot.dist(xs,xt) - M/=M.max() + M = ot.dist(xs, xt) + M /= M.max() #%% plot samples pl.figure(1) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) - pl.title('Source and traget distributions') + pl.title('Source and target distributions') pl.figure(2) - pl.imshow(M,interpolation='nearest') + pl.imshow(M, interpolation='nearest') pl.title('Cost matrix M') #%% EMD - G0=ot.emd(a,b,M) + G0 = ot.emd(a, b, M) pl.figure(3) - pl.imshow(G0,interpolation='nearest') + pl.imshow(G0, interpolation='nearest') pl.title('OT matrix G0') pl.figure(4) - ot.plot.plot2D_samples_mat(xs,xt,G0,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) pl.title('OT matrix with samples') @@ -116,22 +110,24 @@ #%% sinkhorn # reg term - lambd=5e-4 + lambd = 1e-3 - Gs=ot.sinkhorn(a,b,M,lambd) + Gs = ot.sinkhorn(a, b, M, lambd) pl.figure(5) - pl.imshow(Gs,interpolation='nearest') + pl.imshow(Gs, interpolation='nearest') pl.title('OT matrix sinkhorn') pl.figure(6) - ot.plot.plot2D_samples_mat(xs,xt,Gs,color=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + ot.plot.plot2D_samples_mat(xs, xt, Gs, color=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.legend(loc=0) pl.title('OT matrix Sinkhorn with samples') -**Total running time of the script:** ( 0 minutes 0.623 seconds) + pl.show() + +**Total running time of the script:** ( 0 minutes 2.908 seconds) 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..04ef5c8 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\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,7 @@ "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\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(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n xtot[:, 1] = np.sin(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n\n xs = xtot[:n, :]\n xt = xtot[1:, :]\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, figsize=(7, 3))\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, figsize=(7, 3))\n\n pl.subplot(1, 3, 1)\n pl.imshow(M1, interpolation='nearest')\n pl.title('Euclidean cost')\n\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 pl.tight_layout()\n\n #%% EMD\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, figsize=(7, 3))\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 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 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')\n pl.tight_layout()\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.py b/docs/source/auto_examples/plot_OT_L1_vs_L2.py index 9bb92fe..dfc9462 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.py +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.py @@ -4,13 +4,16 @@ 2D Optimal transport for different metrics ========================================== -Stole the figure idea from Fig. 1 and 2 in +Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot @@ -20,89 +23,93 @@ import ot for data in range(2): if data: - n=20 # nb samples - xs=np.zeros((n,2)) - xs[:,0]=np.arange(n)+1 - xs[:,1]=(np.arange(n)+1)*-0.001 # to make it strictly convex... - - xt=np.zeros((n,2)) - xt[:,1]=np.arange(n)+1 + n = 20 # nb samples + xs = np.zeros((n, 2)) + xs[:, 0] = np.arange(n) + 1 + xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... + + xt = np.zeros((n, 2)) + xt[:, 1] = np.arange(n) + 1 else: - - n=50 # nb samples - xtot=np.zeros((n+1,2)) - xtot[:,0]=np.cos((np.arange(n+1)+1.0)*0.9/(n+2)*2*np.pi) - xtot[:,1]=np.sin((np.arange(n+1)+1.0)*0.9/(n+2)*2*np.pi) - - xs=xtot[:n,:] - xt=xtot[1:,:] - - - - a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples - + + n = 50 # nb samples + xtot = np.zeros((n + 1, 2)) + xtot[:, 0] = np.cos( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + xtot[:, 1] = np.sin( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + + xs = xtot[:n, :] + xt = xtot[1:, :] + + a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + # loss matrix - M1=ot.dist(xs,xt,metric='euclidean') - M1/=M1.max() - + M1 = ot.dist(xs, xt, metric='euclidean') + M1 /= M1.max() + # loss matrix - M2=ot.dist(xs,xt,metric='sqeuclidean') - M2/=M2.max() - + M2 = ot.dist(xs, xt, metric='sqeuclidean') + M2 /= M2.max() + # loss matrix - Mp=np.sqrt(ot.dist(xs,xt,metric='euclidean')) - Mp/=Mp.max() - + Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) + Mp /= Mp.max() + #%% plot samples - - pl.figure(1+3*data) + + pl.figure(1 + 3 * data, figsize=(7, 3)) pl.clf() - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') pl.title('Source and traget distributions') - - pl.figure(2+3*data,(15,5)) - pl.subplot(1,3,1) - pl.imshow(M1,interpolation='nearest') - pl.title('Eucidean cost') - pl.subplot(1,3,2) - pl.imshow(M2,interpolation='nearest') + + pl.figure(2 + 3 * data, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') + + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') pl.title('Squared Euclidean cost') - - pl.subplot(1,3,3) - pl.imshow(Mp,interpolation='nearest') + + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') pl.title('Sqrt Euclidean cost') + pl.tight_layout() + #%% EMD - - G1=ot.emd(a,b,M1) - G2=ot.emd(a,b,M2) - Gp=ot.emd(a,b,Mp) - - pl.figure(3+3*data,(15,5)) - - pl.subplot(1,3,1) - ot.plot.plot2D_samples_mat(xs,xt,G1,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + G1 = ot.emd(a, b, M1) + G2 = ot.emd(a, b, M2) + Gp = ot.emd(a, b, Mp) + + pl.figure(3 + 3 * data, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') - #pl.legend(loc=0) + # pl.legend(loc=0) pl.title('OT Euclidean') - - pl.subplot(1,3,2) - - ot.plot.plot2D_samples_mat(xs,xt,G2,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + + pl.subplot(1, 3, 2) + ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') - #pl.legend(loc=0) + # pl.legend(loc=0) pl.title('OT squared Euclidean') - - pl.subplot(1,3,3) - - ot.plot.plot2D_samples_mat(xs,xt,Gp,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + + pl.subplot(1, 3, 3) + ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') - #pl.legend(loc=0) + # pl.legend(loc=0) pl.title('OT sqrt Euclidean') + pl.tight_layout() + +pl.show() diff --git a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst index 4e94bef..ba52bfe 100644 --- a/docs/source/auto_examples/plot_OT_L1_vs_L2.rst +++ b/docs/source/auto_examples/plot_OT_L1_vs_L2.rst @@ -7,11 +7,10 @@ 2D Optimal transport for different metrics ========================================== -Stole the figure idea from Fig. 1 and 2 in +Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf -@author: rflamary @@ -56,6 +55,10 @@ https://arxiv.org/pdf/1706.07650.pdf .. code-block:: python + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + import numpy as np import matplotlib.pylab as pl import ot @@ -65,94 +68,98 @@ https://arxiv.org/pdf/1706.07650.pdf for data in range(2): if data: - n=20 # nb samples - xs=np.zeros((n,2)) - xs[:,0]=np.arange(n)+1 - xs[:,1]=(np.arange(n)+1)*-0.001 # to make it strictly convex... - - xt=np.zeros((n,2)) - xt[:,1]=np.arange(n)+1 + n = 20 # nb samples + xs = np.zeros((n, 2)) + xs[:, 0] = np.arange(n) + 1 + xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex... + + xt = np.zeros((n, 2)) + xt[:, 1] = np.arange(n) + 1 else: - - n=50 # nb samples - xtot=np.zeros((n+1,2)) - xtot[:,0]=np.cos((np.arange(n+1)+1.0)*0.9/(n+2)*2*np.pi) - xtot[:,1]=np.sin((np.arange(n+1)+1.0)*0.9/(n+2)*2*np.pi) - - xs=xtot[:n,:] - xt=xtot[1:,:] - - - - a,b = ot.unif(n),ot.unif(n) # uniform distribution on samples - + + n = 50 # nb samples + xtot = np.zeros((n + 1, 2)) + xtot[:, 0] = np.cos( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + xtot[:, 1] = np.sin( + (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi) + + xs = xtot[:n, :] + xt = xtot[1:, :] + + a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples + # loss matrix - M1=ot.dist(xs,xt,metric='euclidean') - M1/=M1.max() - + M1 = ot.dist(xs, xt, metric='euclidean') + M1 /= M1.max() + # loss matrix - M2=ot.dist(xs,xt,metric='sqeuclidean') - M2/=M2.max() - + M2 = ot.dist(xs, xt, metric='sqeuclidean') + M2 /= M2.max() + # loss matrix - Mp=np.sqrt(ot.dist(xs,xt,metric='euclidean')) - Mp/=Mp.max() - + Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean')) + Mp /= Mp.max() + #%% plot samples - - pl.figure(1+3*data) + + pl.figure(1 + 3 * data, figsize=(7, 3)) pl.clf() - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') pl.title('Source and traget distributions') - - pl.figure(2+3*data,(15,5)) - pl.subplot(1,3,1) - pl.imshow(M1,interpolation='nearest') - pl.title('Eucidean cost') - pl.subplot(1,3,2) - pl.imshow(M2,interpolation='nearest') + + pl.figure(2 + 3 * data, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + pl.imshow(M1, interpolation='nearest') + pl.title('Euclidean cost') + + pl.subplot(1, 3, 2) + pl.imshow(M2, interpolation='nearest') pl.title('Squared Euclidean cost') - - pl.subplot(1,3,3) - pl.imshow(Mp,interpolation='nearest') + + pl.subplot(1, 3, 3) + pl.imshow(Mp, interpolation='nearest') pl.title('Sqrt Euclidean cost') + pl.tight_layout() + #%% EMD - - G1=ot.emd(a,b,M1) - G2=ot.emd(a,b,M2) - Gp=ot.emd(a,b,Mp) - - pl.figure(3+3*data,(15,5)) - - pl.subplot(1,3,1) - ot.plot.plot2D_samples_mat(xs,xt,G1,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + G1 = ot.emd(a, b, M1) + G2 = ot.emd(a, b, M2) + Gp = ot.emd(a, b, Mp) + + pl.figure(3 + 3 * data, figsize=(7, 3)) + + pl.subplot(1, 3, 1) + ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') - #pl.legend(loc=0) + # pl.legend(loc=0) pl.title('OT Euclidean') - - pl.subplot(1,3,2) - - ot.plot.plot2D_samples_mat(xs,xt,G2,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + + pl.subplot(1, 3, 2) + ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') - #pl.legend(loc=0) + # pl.legend(loc=0) pl.title('OT squared Euclidean') - - pl.subplot(1,3,3) - - ot.plot.plot2D_samples_mat(xs,xt,Gp,c=[.5,.5,1]) - pl.plot(xs[:,0],xs[:,1],'+b',label='Source samples') - pl.plot(xt[:,0],xt[:,1],'xr',label='Target samples') + + pl.subplot(1, 3, 3) + ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1]) + pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples') + pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples') pl.axis('equal') - #pl.legend(loc=0) + # pl.legend(loc=0) pl.title('OT sqrt Euclidean') + pl.tight_layout() + + pl.show() -**Total running time of the script:** ( 0 minutes 1.417 seconds) +**Total running time of the script:** ( 0 minutes 1.906 seconds) diff --git a/docs/source/auto_examples/plot_WDA.ipynb b/docs/source/auto_examples/plot_WDA.ipynb index 408a605..5568128 100644 --- a/docs/source/auto_examples/plot_WDA.ipynb +++ b/docs/source/auto_examples/plot_WDA.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# Wasserstein Discriminant Analysis\n\n\n@author: rflamary\n\n" + "\n# Wasserstein Discriminant Analysis\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss\nfrom ot.dr import wda\n\n\n#%% parameters\n\nn=1000 # nb samples in source and target datasets\nnz=0.2\nxs,ys=ot.datasets.get_data_classif('3gauss',n,nz)\nxt,yt=ot.datasets.get_data_classif('3gauss',n,nz)\n\nnbnoise=8\n\nxs=np.hstack((xs,np.random.randn(n,nbnoise)))\nxt=np.hstack((xt,np.random.randn(n,nbnoise)))\n\n#%% plot samples\n\npl.figure(1)\n\n\npl.scatter(xt[:,0],xt[:,1],c=ys,marker='+',label='Source samples')\npl.legend(loc=0)\npl.title('Discriminant dimensions')\n\n\n#%% plot distributions and loss matrix\np=2\nreg=1\nk=10\nmaxiter=100\n\nP,proj = wda(xs,ys,p,reg,k,maxiter=maxiter)\n\n#%% plot samples\n\nxsp=proj(xs)\nxtp=proj(xt)\n\npl.figure(1,(10,5))\n\npl.subplot(1,2,1)\npl.scatter(xsp[:,0],xsp[:,1],c=ys,marker='+',label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples')\n\n\npl.subplot(1,2,2)\npl.scatter(xtp[:,0],xtp[:,1],c=ys,marker='+',label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples')" + "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\n\nfrom ot.dr import wda, fda\n\n\n#%% parameters\n\nn = 1000 # nb samples in source and target datasets\nnz = 0.2\n\n# generate circle dataset\nt = np.random.rand(n) * 2 * np.pi\nys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1\nxs = np.concatenate(\n (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)\nxs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2)\n\nt = np.random.rand(n) * 2 * np.pi\nyt = np.floor((np.arange(n) * 1.0 / n * 3)) + 1\nxt = np.concatenate(\n (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1)\nxt = xt * yt.reshape(-1, 1) + nz * np.random.randn(n, 2)\n\nnbnoise = 8\n\nxs = np.hstack((xs, np.random.randn(n, nbnoise)))\nxt = np.hstack((xt, np.random.randn(n, nbnoise)))\n\n#%% plot samples\npl.figure(1, figsize=(6.4, 3.5))\n\npl.subplot(1, 2, 1)\npl.scatter(xt[:, 0], xt[:, 1], c=ys, marker='+', label='Source samples')\npl.legend(loc=0)\npl.title('Discriminant dimensions')\n\npl.subplot(1, 2, 2)\npl.scatter(xt[:, 2], xt[:, 3], c=ys, marker='+', label='Source samples')\npl.legend(loc=0)\npl.title('Other dimensions')\npl.tight_layout()\n\n#%% Compute FDA\np = 2\n\nPfda, projfda = fda(xs, ys, p)\n\n#%% Compute WDA\np = 2\nreg = 1e0\nk = 10\nmaxiter = 100\n\nPwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter)\n\n#%% plot samples\n\nxsp = projfda(xs)\nxtp = projfda(xt)\n\nxspw = projwda(xs)\nxtpw = projwda(xt)\n\npl.figure(2)\n\npl.subplot(2, 2, 1)\npl.scatter(xsp[:, 0], xsp[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples FDA')\n\npl.subplot(2, 2, 2)\npl.scatter(xtp[:, 0], xtp[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples FDA')\n\npl.subplot(2, 2, 3)\npl.scatter(xspw[:, 0], xspw[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected training samples WDA')\n\npl.subplot(2, 2, 4)\npl.scatter(xtpw[:, 0], xtpw[:, 1], c=ys, marker='+', label='Projected samples')\npl.legend(loc=0)\npl.title('Projected test samples WDA')\npl.tight_layout()\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_WDA.py b/docs/source/auto_examples/plot_WDA.py index bbe3888..42789f2 100644 --- a/docs/source/auto_examples/plot_WDA.py +++ b/docs/source/auto_examples/plot_WDA.py @@ -4,60 +4,97 @@ Wasserstein Discriminant Analysis ================================= -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl -import ot -from ot.datasets import get_1D_gauss as gauss -from ot.dr import wda + +from ot.dr import wda, fda #%% parameters -n=1000 # nb samples in source and target datasets -nz=0.2 -xs,ys=ot.datasets.get_data_classif('3gauss',n,nz) -xt,yt=ot.datasets.get_data_classif('3gauss',n,nz) +n = 1000 # nb samples in source and target datasets +nz = 0.2 -nbnoise=8 +# generate circle dataset +t = np.random.rand(n) * 2 * np.pi +ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 +xs = np.concatenate( + (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) +xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2) -xs=np.hstack((xs,np.random.randn(n,nbnoise))) -xt=np.hstack((xt,np.random.randn(n,nbnoise))) +t = np.random.rand(n) * 2 * np.pi +yt = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 +xt = np.concatenate( + (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) +xt = xt * yt.reshape(-1, 1) + nz * np.random.randn(n, 2) -#%% plot samples +nbnoise = 8 -pl.figure(1) +xs = np.hstack((xs, np.random.randn(n, nbnoise))) +xt = np.hstack((xt, np.random.randn(n, nbnoise))) +#%% plot samples +pl.figure(1, figsize=(6.4, 3.5)) -pl.scatter(xt[:,0],xt[:,1],c=ys,marker='+',label='Source samples') +pl.subplot(1, 2, 1) +pl.scatter(xt[:, 0], xt[:, 1], c=ys, marker='+', label='Source samples') pl.legend(loc=0) pl.title('Discriminant dimensions') +pl.subplot(1, 2, 2) +pl.scatter(xt[:, 2], xt[:, 3], c=ys, marker='+', label='Source samples') +pl.legend(loc=0) +pl.title('Other dimensions') +pl.tight_layout() + +#%% Compute FDA +p = 2 -#%% plot distributions and loss matrix -p=2 -reg=1 -k=10 -maxiter=100 +Pfda, projfda = fda(xs, ys, p) -P,proj = wda(xs,ys,p,reg,k,maxiter=maxiter) +#%% Compute WDA +p = 2 +reg = 1e0 +k = 10 +maxiter = 100 + +Pwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter) #%% plot samples -xsp=proj(xs) -xtp=proj(xt) +xsp = projfda(xs) +xtp = projfda(xt) + +xspw = projwda(xs) +xtpw = projwda(xt) + +pl.figure(2) -pl.figure(1,(10,5)) +pl.subplot(2, 2, 1) +pl.scatter(xsp[:, 0], xsp[:, 1], c=ys, marker='+', label='Projected samples') +pl.legend(loc=0) +pl.title('Projected training samples FDA') -pl.subplot(1,2,1) -pl.scatter(xsp[:,0],xsp[:,1],c=ys,marker='+',label='Projected samples') +pl.subplot(2, 2, 2) +pl.scatter(xtp[:, 0], xtp[:, 1], c=ys, marker='+', label='Projected samples') pl.legend(loc=0) -pl.title('Projected training samples') +pl.title('Projected test samples FDA') +pl.subplot(2, 2, 3) +pl.scatter(xspw[:, 0], xspw[:, 1], c=ys, marker='+', label='Projected samples') +pl.legend(loc=0) +pl.title('Projected training samples WDA') -pl.subplot(1,2,2) -pl.scatter(xtp[:,0],xtp[:,1],c=ys,marker='+',label='Projected samples') +pl.subplot(2, 2, 4) +pl.scatter(xtpw[:, 0], xtpw[:, 1], c=ys, marker='+', label='Projected samples') pl.legend(loc=0) -pl.title('Projected test samples') +pl.title('Projected test samples WDA') +pl.tight_layout() + +pl.show() diff --git a/docs/source/auto_examples/plot_WDA.rst b/docs/source/auto_examples/plot_WDA.rst index 540555d..76ebaf5 100644 --- a/docs/source/auto_examples/plot_WDA.rst +++ b/docs/source/auto_examples/plot_WDA.rst @@ -7,13 +7,22 @@ Wasserstein Discriminant Analysis ================================= -@author: rflamary -.. image:: /auto_examples/images/sphx_glr_plot_WDA_001.png - :align: center +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_WDA_001.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_WDA_002.png + :scale: 47 .. rst-class:: sphx-glr-script-out @@ -23,26 +32,43 @@ Wasserstein Discriminant Analysis Compiling cost function... Computing gradient of cost function... iter cost val grad. norm - 1 +5.2427396265941129e-01 8.16627951e-01 - 2 +1.7904850059627236e-01 1.91366819e-01 - 3 +1.6985797253002377e-01 1.70940682e-01 - 4 +1.3903474972292729e-01 1.28606342e-01 - 5 +7.4961734618782416e-02 6.41973980e-02 - 6 +7.1900245222486239e-02 4.25693592e-02 - 7 +7.0472023318269614e-02 2.34599232e-02 - 8 +6.9917568641317152e-02 5.66542766e-03 - 9 +6.9885086242452696e-02 4.05756115e-04 - 10 +6.9884967432653489e-02 2.16836017e-04 - 11 +6.9884923649884148e-02 5.74961622e-05 - 12 +6.9884921818258436e-02 3.83257203e-05 - 13 +6.9884920459612282e-02 9.97486224e-06 - 14 +6.9884920414414409e-02 7.33567875e-06 - 15 +6.9884920388431387e-02 5.23889187e-06 - 16 +6.9884920385183902e-02 4.91959084e-06 - 17 +6.9884920373983223e-02 3.56451669e-06 - 18 +6.9884920369701245e-02 2.88858709e-06 - 19 +6.9884920361621208e-02 1.82294279e-07 - Terminated - min grad norm reached after 19 iterations, 9.65 seconds. + 1 +8.9741888001949222e-01 3.71269078e-01 + 2 +4.9103998133976140e-01 3.46687543e-01 + 3 +4.2142651893148553e-01 1.04789602e-01 + 4 +4.1573609749588841e-01 5.21726648e-02 + 5 +4.1486046805261961e-01 5.35335513e-02 + 6 +4.1315953904635105e-01 2.17803599e-02 + 7 +4.1313030162717523e-01 6.06901182e-02 + 8 +4.1301511591963386e-01 5.88598758e-02 + 9 +4.1258349404769817e-01 5.14307874e-02 + 10 +4.1139242901051226e-01 2.03198793e-02 + 11 +4.1113798965164017e-01 1.18944721e-02 + 12 +4.1103446820878486e-01 2.21783648e-02 + 13 +4.1076586830791861e-01 9.51495863e-03 + 14 +4.1036935287519144e-01 3.74973214e-02 + 15 +4.0958729714575060e-01 1.23810902e-02 + 16 +4.0898266309095005e-01 4.01999918e-02 + 17 +4.0816076944357715e-01 2.27240277e-02 + 18 +4.0788116701894767e-01 4.42815945e-02 + 19 +4.0695443744952403e-01 3.28464304e-02 + 20 +4.0293834480911150e-01 7.76000681e-02 + 21 +3.8488003705202750e-01 1.49378022e-01 + 22 +3.0767344927282614e-01 2.15432117e-01 + 23 +2.3849425361868334e-01 1.07942382e-01 + 24 +2.3845125762548214e-01 1.08953278e-01 + 25 +2.3828007730494005e-01 1.07934830e-01 + 26 +2.3760839060570119e-01 1.03822134e-01 + 27 +2.3514215179705886e-01 8.67263481e-02 + 28 +2.2978886197588613e-01 9.26609306e-03 + 29 +2.2972671019495342e-01 2.59476089e-03 + 30 +2.2972355865247496e-01 1.57205146e-03 + 31 +2.2972296662351968e-01 1.29300760e-03 + 32 +2.2972181557051569e-01 8.82375756e-05 + 33 +2.2972181277025336e-01 6.20536544e-05 + 34 +2.2972181023486152e-01 7.01884014e-06 + 35 +2.2972181020400181e-01 1.60415765e-06 + 36 +2.2972181020236590e-01 2.44290966e-07 + Terminated - min grad norm reached after 36 iterations, 13.41 seconds. @@ -53,62 +79,100 @@ Wasserstein Discriminant Analysis .. code-block:: python + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + import numpy as np import matplotlib.pylab as pl - import ot - from ot.datasets import get_1D_gauss as gauss - from ot.dr import wda + + from ot.dr import wda, fda #%% parameters - n=1000 # nb samples in source and target datasets - nz=0.2 - xs,ys=ot.datasets.get_data_classif('3gauss',n,nz) - xt,yt=ot.datasets.get_data_classif('3gauss',n,nz) + n = 1000 # nb samples in source and target datasets + nz = 0.2 - nbnoise=8 + # generate circle dataset + t = np.random.rand(n) * 2 * np.pi + ys = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 + xs = np.concatenate( + (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) + xs = xs * ys.reshape(-1, 1) + nz * np.random.randn(n, 2) - xs=np.hstack((xs,np.random.randn(n,nbnoise))) - xt=np.hstack((xt,np.random.randn(n,nbnoise))) + t = np.random.rand(n) * 2 * np.pi + yt = np.floor((np.arange(n) * 1.0 / n * 3)) + 1 + xt = np.concatenate( + (np.cos(t).reshape((-1, 1)), np.sin(t).reshape((-1, 1))), 1) + xt = xt * yt.reshape(-1, 1) + nz * np.random.randn(n, 2) - #%% plot samples + nbnoise = 8 - pl.figure(1) + xs = np.hstack((xs, np.random.randn(n, nbnoise))) + xt = np.hstack((xt, np.random.randn(n, nbnoise))) + #%% plot samples + pl.figure(1, figsize=(6.4, 3.5)) - pl.scatter(xt[:,0],xt[:,1],c=ys,marker='+',label='Source samples') + pl.subplot(1, 2, 1) + pl.scatter(xt[:, 0], xt[:, 1], c=ys, marker='+', label='Source samples') pl.legend(loc=0) pl.title('Discriminant dimensions') + pl.subplot(1, 2, 2) + pl.scatter(xt[:, 2], xt[:, 3], c=ys, marker='+', label='Source samples') + pl.legend(loc=0) + pl.title('Other dimensions') + pl.tight_layout() + + #%% Compute FDA + p = 2 - #%% plot distributions and loss matrix - p=2 - reg=1 - k=10 - maxiter=100 + Pfda, projfda = fda(xs, ys, p) - P,proj = wda(xs,ys,p,reg,k,maxiter=maxiter) + #%% Compute WDA + p = 2 + reg = 1e0 + k = 10 + maxiter = 100 + + Pwda, projwda = wda(xs, ys, p, reg, k, maxiter=maxiter) #%% plot samples - xsp=proj(xs) - xtp=proj(xt) + xsp = projfda(xs) + xtp = projfda(xt) + + xspw = projwda(xs) + xtpw = projwda(xt) - pl.figure(1,(10,5)) + pl.figure(2) - pl.subplot(1,2,1) - pl.scatter(xsp[:,0],xsp[:,1],c=ys,marker='+',label='Projected samples') + pl.subplot(2, 2, 1) + pl.scatter(xsp[:, 0], xsp[:, 1], c=ys, marker='+', label='Projected samples') pl.legend(loc=0) - pl.title('Projected training samples') + pl.title('Projected training samples FDA') + pl.subplot(2, 2, 2) + pl.scatter(xtp[:, 0], xtp[:, 1], c=ys, marker='+', label='Projected samples') + pl.legend(loc=0) + pl.title('Projected test samples FDA') + + pl.subplot(2, 2, 3) + pl.scatter(xspw[:, 0], xspw[:, 1], c=ys, marker='+', label='Projected samples') + pl.legend(loc=0) + pl.title('Projected training samples WDA') - pl.subplot(1,2,2) - pl.scatter(xtp[:,0],xtp[:,1],c=ys,marker='+',label='Projected samples') + pl.subplot(2, 2, 4) + pl.scatter(xtpw[:, 0], xtpw[:, 1], c=ys, marker='+', label='Projected samples') pl.legend(loc=0) - pl.title('Projected test samples') + pl.title('Projected test samples WDA') + pl.tight_layout() + + pl.show() -**Total running time of the script:** ( 0 minutes 16.902 seconds) +**Total running time of the script:** ( 0 minutes 19.853 seconds) diff --git a/docs/source/auto_examples/plot_barycenter_1D.ipynb b/docs/source/auto_examples/plot_barycenter_1D.ipynb index 36f3975..239b8b8 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.ipynb +++ b/docs/source/auto_examples/plot_barycenter_1D.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 1D Wasserstein barycenter demo\n\n\n\n@author: rflamary\n\n" + "\n# 1D Wasserstein barycenter demo\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom mpl_toolkits.mplot3d import Axes3D #necessary for 3d plot even if not used\nfrom matplotlib.collections import PolyCollection\n\n\n#%% parameters\n\nn=100 # nb bins\n\n# bin positions\nx=np.arange(n,dtype=np.float64)\n\n# Gaussian distributions\na1=ot.datasets.get_1D_gauss(n,m=20,s=5) # m= mean, s= std\na2=ot.datasets.get_1D_gauss(n,m=60,s=8)\n\n# creating matrix A containing all distributions\nA=np.vstack((a1,a2)).T\nnbd=A.shape[1]\n\n# loss matrix + normalization\nM=ot.utils.dist0(n)\nM/=M.max()\n\n#%% plot the distributions\n\npl.figure(1)\nfor i in range(nbd):\n pl.plot(x,A[:,i])\npl.title('Distributions')\n\n#%% barycenter computation\n\nalpha=0.2 # 0<=alpha<=1\nweights=np.array([1-alpha,alpha])\n\n# l2bary\nbary_l2=A.dot(weights)\n\n# wasserstein\nreg=1e-3\nbary_wass=ot.bregman.barycenter(A,M,reg,weights)\n\npl.figure(2)\npl.clf()\npl.subplot(2,1,1)\nfor i in range(nbd):\n pl.plot(x,A[:,i])\npl.title('Distributions')\n\npl.subplot(2,1,2)\npl.plot(x,bary_l2,'r',label='l2')\npl.plot(x,bary_wass,'g',label='Wasserstein')\npl.legend()\npl.title('Barycenters')\n\n\n#%% barycenter interpolation\n\nnbalpha=11\nalphalist=np.linspace(0,1,nbalpha)\n\n\nB_l2=np.zeros((n,nbalpha))\n\nB_wass=np.copy(B_l2)\n\nfor i in range(0,nbalpha):\n alpha=alphalist[i]\n weights=np.array([1-alpha,alpha])\n B_l2[:,i]=A.dot(weights)\n B_wass[:,i]=ot.bregman.barycenter(A,M,reg,weights)\n\n#%% plot interpolation\n\npl.figure(3,(10,5))\n\n#pl.subplot(1,2,1)\ncmap=pl.cm.get_cmap('viridis')\nverts = []\nzs = alphalist\nfor i,z in enumerate(zs):\n ys = B_l2[:,i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts,facecolors=[cmap(a) for a in alphalist])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\n\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0,1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max()*1.01)\npl.title('Barycenter interpolation with l2')\n\npl.show()\n\npl.figure(4,(10,5))\n\n#pl.subplot(1,2,1)\ncmap=pl.cm.get_cmap('viridis')\nverts = []\nzs = alphalist\nfor i,z in enumerate(zs):\n ys = B_wass[:,i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts,facecolors=[cmap(a) for a in alphalist])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\n\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0,1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max()*1.01)\npl.title('Barycenter interpolation with Wasserstein')\n\npl.show()" + "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n# necessary for 3d plot even if not used\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nfrom matplotlib.collections import PolyCollection\n\n\n#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na1 = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.get_1D_gauss(n, m=60, s=8)\n\n# creating matrix A containing all distributions\nA = np.vstack((a1, a2)).T\nn_distributions = A.shape[1]\n\n# loss matrix + normalization\nM = ot.utils.dist0(n)\nM /= M.max()\n\n#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\npl.tight_layout()\n\n#%% barycenter computation\n\nalpha = 0.2 # 0<=alpha<=1\nweights = np.array([1 - alpha, alpha])\n\n# l2bary\nbary_l2 = A.dot(weights)\n\n# wasserstein\nreg = 1e-3\nbary_wass = ot.bregman.barycenter(A, M, reg, weights)\n\npl.figure(2)\npl.clf()\npl.subplot(2, 1, 1)\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\n\npl.subplot(2, 1, 2)\npl.plot(x, bary_l2, 'r', label='l2')\npl.plot(x, bary_wass, 'g', label='Wasserstein')\npl.legend()\npl.title('Barycenters')\npl.tight_layout()\n\n#%% barycenter interpolation\n\nn_alpha = 11\nalpha_list = np.linspace(0, 1, n_alpha)\n\n\nB_l2 = np.zeros((n, n_alpha))\n\nB_wass = np.copy(B_l2)\n\nfor i in range(0, n_alpha):\n alpha = alpha_list[i]\n weights = np.array([1 - alpha, alpha])\n B_l2[:, i] = A.dot(weights)\n B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights)\n\n#%% plot interpolation\n\npl.figure(3)\n\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_l2[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with l2')\npl.tight_layout()\n\npl.figure(4)\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_wass[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with Wasserstein')\npl.tight_layout()\n\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_barycenter_1D.py b/docs/source/auto_examples/plot_barycenter_1D.py index 30eecbf..875f44c 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.py +++ b/docs/source/auto_examples/plot_barycenter_1D.py @@ -4,135 +4,134 @@ 1D Wasserstein barycenter demo ============================== - -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot -from mpl_toolkits.mplot3d import Axes3D #necessary for 3d plot even if not used +# necessary for 3d plot even if not used +from mpl_toolkits.mplot3d import Axes3D # noqa from matplotlib.collections import PolyCollection #%% parameters -n=100 # nb bins +n = 100 # nb bins # bin positions -x=np.arange(n,dtype=np.float64) +x = np.arange(n, dtype=np.float64) # Gaussian distributions -a1=ot.datasets.get_1D_gauss(n,m=20,s=5) # m= mean, s= std -a2=ot.datasets.get_1D_gauss(n,m=60,s=8) +a1 = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std +a2 = ot.datasets.get_1D_gauss(n, m=60, s=8) # creating matrix A containing all distributions -A=np.vstack((a1,a2)).T -nbd=A.shape[1] +A = np.vstack((a1, a2)).T +n_distributions = A.shape[1] # loss matrix + normalization -M=ot.utils.dist0(n) -M/=M.max() +M = ot.utils.dist0(n) +M /= M.max() #%% plot the distributions -pl.figure(1) -for i in range(nbd): - pl.plot(x,A[:,i]) +pl.figure(1, figsize=(6.4, 3)) +for i in range(n_distributions): + pl.plot(x, A[:, i]) pl.title('Distributions') +pl.tight_layout() #%% barycenter computation -alpha=0.2 # 0<=alpha<=1 -weights=np.array([1-alpha,alpha]) +alpha = 0.2 # 0<=alpha<=1 +weights = np.array([1 - alpha, alpha]) # l2bary -bary_l2=A.dot(weights) +bary_l2 = A.dot(weights) # wasserstein -reg=1e-3 -bary_wass=ot.bregman.barycenter(A,M,reg,weights) +reg = 1e-3 +bary_wass = ot.bregman.barycenter(A, M, reg, weights) pl.figure(2) pl.clf() -pl.subplot(2,1,1) -for i in range(nbd): - pl.plot(x,A[:,i]) +pl.subplot(2, 1, 1) +for i in range(n_distributions): + pl.plot(x, A[:, i]) pl.title('Distributions') -pl.subplot(2,1,2) -pl.plot(x,bary_l2,'r',label='l2') -pl.plot(x,bary_wass,'g',label='Wasserstein') +pl.subplot(2, 1, 2) +pl.plot(x, bary_l2, 'r', label='l2') +pl.plot(x, bary_wass, 'g', label='Wasserstein') pl.legend() pl.title('Barycenters') - +pl.tight_layout() #%% barycenter interpolation -nbalpha=11 -alphalist=np.linspace(0,1,nbalpha) +n_alpha = 11 +alpha_list = np.linspace(0, 1, n_alpha) -B_l2=np.zeros((n,nbalpha)) +B_l2 = np.zeros((n, n_alpha)) -B_wass=np.copy(B_l2) +B_wass = np.copy(B_l2) -for i in range(0,nbalpha): - alpha=alphalist[i] - weights=np.array([1-alpha,alpha]) - B_l2[:,i]=A.dot(weights) - B_wass[:,i]=ot.bregman.barycenter(A,M,reg,weights) +for i in range(0, n_alpha): + alpha = alpha_list[i] + weights = np.array([1 - alpha, alpha]) + B_l2[:, i] = A.dot(weights) + B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights) #%% plot interpolation -pl.figure(3,(10,5)) +pl.figure(3) -#pl.subplot(1,2,1) -cmap=pl.cm.get_cmap('viridis') +cmap = pl.cm.get_cmap('viridis') verts = [] -zs = alphalist -for i,z in enumerate(zs): - ys = B_l2[:,i] +zs = alpha_list +for i, z in enumerate(zs): + ys = B_l2[:, i] verts.append(list(zip(x, ys))) ax = pl.gcf().gca(projection='3d') -poly = PolyCollection(verts,facecolors=[cmap(a) for a in alphalist]) +poly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list]) poly.set_alpha(0.7) ax.add_collection3d(poly, zs=zs, zdir='y') - ax.set_xlabel('x') ax.set_xlim3d(0, n) ax.set_ylabel('$\\alpha$') -ax.set_ylim3d(0,1) +ax.set_ylim3d(0, 1) ax.set_zlabel('') -ax.set_zlim3d(0, B_l2.max()*1.01) +ax.set_zlim3d(0, B_l2.max() * 1.01) pl.title('Barycenter interpolation with l2') +pl.tight_layout() -pl.show() - -pl.figure(4,(10,5)) - -#pl.subplot(1,2,1) -cmap=pl.cm.get_cmap('viridis') +pl.figure(4) +cmap = pl.cm.get_cmap('viridis') verts = [] -zs = alphalist -for i,z in enumerate(zs): - ys = B_wass[:,i] +zs = alpha_list +for i, z in enumerate(zs): + ys = B_wass[:, i] verts.append(list(zip(x, ys))) ax = pl.gcf().gca(projection='3d') -poly = PolyCollection(verts,facecolors=[cmap(a) for a in alphalist]) +poly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list]) poly.set_alpha(0.7) ax.add_collection3d(poly, zs=zs, zdir='y') - ax.set_xlabel('x') ax.set_xlim3d(0, n) ax.set_ylabel('$\\alpha$') -ax.set_ylim3d(0,1) +ax.set_ylim3d(0, 1) ax.set_zlabel('') -ax.set_zlim3d(0, B_l2.max()*1.01) +ax.set_zlim3d(0, B_l2.max() * 1.01) pl.title('Barycenter interpolation with Wasserstein') +pl.tight_layout() -pl.show()
\ No newline at end of file +pl.show() diff --git a/docs/source/auto_examples/plot_barycenter_1D.rst b/docs/source/auto_examples/plot_barycenter_1D.rst index 1b15c77..af88e80 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.rst +++ b/docs/source/auto_examples/plot_barycenter_1D.rst @@ -8,8 +8,6 @@ ============================== -@author: rflamary - @@ -43,135 +41,137 @@ .. code-block:: python + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + import numpy as np import matplotlib.pylab as pl import ot - from mpl_toolkits.mplot3d import Axes3D #necessary for 3d plot even if not used + # necessary for 3d plot even if not used + from mpl_toolkits.mplot3d import Axes3D # noqa from matplotlib.collections import PolyCollection #%% parameters - n=100 # nb bins + n = 100 # nb bins # bin positions - x=np.arange(n,dtype=np.float64) + x = np.arange(n, dtype=np.float64) # Gaussian distributions - a1=ot.datasets.get_1D_gauss(n,m=20,s=5) # m= mean, s= std - a2=ot.datasets.get_1D_gauss(n,m=60,s=8) + a1 = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std + a2 = ot.datasets.get_1D_gauss(n, m=60, s=8) # creating matrix A containing all distributions - A=np.vstack((a1,a2)).T - nbd=A.shape[1] + A = np.vstack((a1, a2)).T + n_distributions = A.shape[1] # loss matrix + normalization - M=ot.utils.dist0(n) - M/=M.max() + M = ot.utils.dist0(n) + M /= M.max() #%% plot the distributions - pl.figure(1) - for i in range(nbd): - pl.plot(x,A[:,i]) + pl.figure(1, figsize=(6.4, 3)) + for i in range(n_distributions): + pl.plot(x, A[:, i]) pl.title('Distributions') + pl.tight_layout() #%% barycenter computation - alpha=0.2 # 0<=alpha<=1 - weights=np.array([1-alpha,alpha]) + alpha = 0.2 # 0<=alpha<=1 + weights = np.array([1 - alpha, alpha]) # l2bary - bary_l2=A.dot(weights) + bary_l2 = A.dot(weights) # wasserstein - reg=1e-3 - bary_wass=ot.bregman.barycenter(A,M,reg,weights) + reg = 1e-3 + bary_wass = ot.bregman.barycenter(A, M, reg, weights) pl.figure(2) pl.clf() - pl.subplot(2,1,1) - for i in range(nbd): - pl.plot(x,A[:,i]) + pl.subplot(2, 1, 1) + for i in range(n_distributions): + pl.plot(x, A[:, i]) pl.title('Distributions') - pl.subplot(2,1,2) - pl.plot(x,bary_l2,'r',label='l2') - pl.plot(x,bary_wass,'g',label='Wasserstein') + pl.subplot(2, 1, 2) + pl.plot(x, bary_l2, 'r', label='l2') + pl.plot(x, bary_wass, 'g', label='Wasserstein') pl.legend() pl.title('Barycenters') - + pl.tight_layout() #%% barycenter interpolation - nbalpha=11 - alphalist=np.linspace(0,1,nbalpha) + n_alpha = 11 + alpha_list = np.linspace(0, 1, n_alpha) - B_l2=np.zeros((n,nbalpha)) + B_l2 = np.zeros((n, n_alpha)) - B_wass=np.copy(B_l2) + B_wass = np.copy(B_l2) - for i in range(0,nbalpha): - alpha=alphalist[i] - weights=np.array([1-alpha,alpha]) - B_l2[:,i]=A.dot(weights) - B_wass[:,i]=ot.bregman.barycenter(A,M,reg,weights) + for i in range(0, n_alpha): + alpha = alpha_list[i] + weights = np.array([1 - alpha, alpha]) + B_l2[:, i] = A.dot(weights) + B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights) #%% plot interpolation - pl.figure(3,(10,5)) + pl.figure(3) - #pl.subplot(1,2,1) - cmap=pl.cm.get_cmap('viridis') + cmap = pl.cm.get_cmap('viridis') verts = [] - zs = alphalist - for i,z in enumerate(zs): - ys = B_l2[:,i] + zs = alpha_list + for i, z in enumerate(zs): + ys = B_l2[:, i] verts.append(list(zip(x, ys))) ax = pl.gcf().gca(projection='3d') - poly = PolyCollection(verts,facecolors=[cmap(a) for a in alphalist]) + poly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list]) poly.set_alpha(0.7) ax.add_collection3d(poly, zs=zs, zdir='y') - ax.set_xlabel('x') ax.set_xlim3d(0, n) ax.set_ylabel('$\\alpha$') - ax.set_ylim3d(0,1) + ax.set_ylim3d(0, 1) ax.set_zlabel('') - ax.set_zlim3d(0, B_l2.max()*1.01) + ax.set_zlim3d(0, B_l2.max() * 1.01) pl.title('Barycenter interpolation with l2') + pl.tight_layout() - pl.show() - - pl.figure(4,(10,5)) - - #pl.subplot(1,2,1) - cmap=pl.cm.get_cmap('viridis') + pl.figure(4) + cmap = pl.cm.get_cmap('viridis') verts = [] - zs = alphalist - for i,z in enumerate(zs): - ys = B_wass[:,i] + zs = alpha_list + for i, z in enumerate(zs): + ys = B_wass[:, i] verts.append(list(zip(x, ys))) ax = pl.gcf().gca(projection='3d') - poly = PolyCollection(verts,facecolors=[cmap(a) for a in alphalist]) + poly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list]) poly.set_alpha(0.7) ax.add_collection3d(poly, zs=zs, zdir='y') - ax.set_xlabel('x') ax.set_xlim3d(0, n) ax.set_ylabel('$\\alpha$') - ax.set_ylim3d(0,1) + ax.set_ylim3d(0, 1) ax.set_zlabel('') - ax.set_zlim3d(0, B_l2.max()*1.01) + ax.set_zlim3d(0, B_l2.max() * 1.01) pl.title('Barycenter interpolation with Wasserstein') + pl.tight_layout() pl.show() -**Total running time of the script:** ( 0 minutes 2.274 seconds) + +**Total running time of the script:** ( 0 minutes 0.546 seconds) diff --git a/docs/source/auto_examples/plot_compute_emd.ipynb b/docs/source/auto_examples/plot_compute_emd.ipynb index 4162144..ce3f8c6 100644 --- a/docs/source/auto_examples/plot_compute_emd.ipynb +++ b/docs/source/auto_examples/plot_compute_emd.ipynb @@ -15,7 +15,7 @@ }, { "source": [ - "\n# 1D optimal transport\n\n\n@author: rflamary\n\n" + "\n# 1D optimal transport\n\n\n\n" ], "cell_type": "markdown", "metadata": {} @@ -24,7 +24,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\nfrom ot.datasets import get_1D_gauss as gauss\n\n\n#%% 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()\n#%% 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')\n\n#%% 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 M3\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()\n\n#%%\nreg=1e-2\nd_sinkhorn=ot.sinkhorn(a,B,M,reg)\nd_sinkhorn2=ot.sinkhorn(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()" + "# 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\n\n\n#%% 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()\n#%% 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()\n\n#%% 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 M3\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()\n\n#%%\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": [], "metadata": { diff --git a/docs/source/auto_examples/plot_compute_emd.py b/docs/source/auto_examples/plot_compute_emd.py index c7063e8..893eecf 100644 --- a/docs/source/auto_examples/plot_compute_emd.py +++ b/docs/source/auto_examples/plot_compute_emd.py @@ -4,9 +4,12 @@ 1D optimal transport ==================== -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot @@ -15,60 +18,63 @@ from ot.datasets import get_1D_gauss as gauss #%% parameters -n=100 # nb bins -n_target=50 # nb target distributions +n = 100 # nb bins +n_target = 50 # nb target distributions # bin positions -x=np.arange(n,dtype=np.float64) +x = np.arange(n, dtype=np.float64) -lst_m=np.linspace(20,90,n_target) +lst_m = np.linspace(20, 90, n_target) # Gaussian distributions -a=gauss(n,m=20,s=5) # m= mean, s= std +a = gauss(n, m=20, s=5) # m= mean, s= std -B=np.zeros((n,n_target)) +B = np.zeros((n, n_target)) -for i,m in enumerate(lst_m): - B[:,i]=gauss(n,m=m,s=5) +for i, m in enumerate(lst_m): + B[:, i] = gauss(n, m=m, s=5) # loss matrix and normalization -M=ot.dist(x.reshape((n,1)),x.reshape((n,1)),'euclidean') -M/=M.max() -M2=ot.dist(x.reshape((n,1)),x.reshape((n,1)),'sqeuclidean') -M2/=M2.max() +M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean') +M /= M.max() +M2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean') +M2 /= M2.max() #%% plot the distributions pl.figure(1) -pl.subplot(2,1,1) -pl.plot(x,a,'b',label='Source distribution') +pl.subplot(2, 1, 1) +pl.plot(x, a, 'b', label='Source distribution') pl.title('Source distribution') -pl.subplot(2,1,2) -pl.plot(x,B,label='Target distributions') +pl.subplot(2, 1, 2) +pl.plot(x, B, label='Target distributions') pl.title('Target distributions') +pl.tight_layout() #%% Compute and plot distributions and loss matrix -d_emd=ot.emd2(a,B,M) # direct computation of EMD -d_emd2=ot.emd2(a,B,M2) # direct computation of EMD with loss M3 +d_emd = ot.emd2(a, B, M) # direct computation of EMD +d_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M3 pl.figure(2) -pl.plot(d_emd,label='Euclidean EMD') -pl.plot(d_emd2,label='Squared Euclidean EMD') +pl.plot(d_emd, label='Euclidean EMD') +pl.plot(d_emd2, label='Squared Euclidean EMD') pl.title('EMD distances') pl.legend() #%% -reg=1e-2 -d_sinkhorn=ot.sinkhorn(a,B,M,reg) -d_sinkhorn2=ot.sinkhorn(a,B,M2,reg) +reg = 1e-2 +d_sinkhorn = ot.sinkhorn2(a, B, M, reg) +d_sinkhorn2 = ot.sinkhorn2(a, B, M2, reg) pl.figure(2) pl.clf() -pl.plot(d_emd,label='Euclidean EMD') -pl.plot(d_emd2,label='Squared Euclidean EMD') -pl.plot(d_sinkhorn,'+',label='Euclidean Sinkhorn') -pl.plot(d_sinkhorn2,'+',label='Squared Euclidean Sinkhorn') +pl.plot(d_emd, label='Euclidean EMD') +pl.plot(d_emd2, label='Squared Euclidean EMD') +pl.plot(d_sinkhorn, '+', label='Euclidean Sinkhorn') +pl.plot(d_sinkhorn2, '+', label='Squared Euclidean Sinkhorn') pl.title('EMD distances') -pl.legend()
\ No newline at end of file +pl.legend() + +pl.show() diff --git a/docs/source/auto_examples/plot_compute_emd.rst b/docs/source/auto_examples/plot_compute_emd.rst index 4c7445b..f2e2005 100644 --- a/docs/source/auto_examples/plot_compute_emd.rst +++ b/docs/source/auto_examples/plot_compute_emd.rst @@ -7,7 +7,6 @@ 1D optimal transport ==================== -@author: rflamary @@ -32,6 +31,10 @@ .. code-block:: python + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + import numpy as np import matplotlib.pylab as pl import ot @@ -40,64 +43,68 @@ #%% parameters - n=100 # nb bins - n_target=50 # nb target distributions + n = 100 # nb bins + n_target = 50 # nb target distributions # bin positions - x=np.arange(n,dtype=np.float64) + x = np.arange(n, dtype=np.float64) - lst_m=np.linspace(20,90,n_target) + lst_m = np.linspace(20, 90, n_target) # Gaussian distributions - a=gauss(n,m=20,s=5) # m= mean, s= std + a = gauss(n, m=20, s=5) # m= mean, s= std - B=np.zeros((n,n_target)) + B = np.zeros((n, n_target)) - for i,m in enumerate(lst_m): - B[:,i]=gauss(n,m=m,s=5) + for i, m in enumerate(lst_m): + B[:, i] = gauss(n, m=m, s=5) # loss matrix and normalization - M=ot.dist(x.reshape((n,1)),x.reshape((n,1)),'euclidean') - M/=M.max() - M2=ot.dist(x.reshape((n,1)),x.reshape((n,1)),'sqeuclidean') - M2/=M2.max() + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'euclidean') + M /= M.max() + M2 = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)), 'sqeuclidean') + M2 /= M2.max() #%% plot the distributions pl.figure(1) - pl.subplot(2,1,1) - pl.plot(x,a,'b',label='Source distribution') + pl.subplot(2, 1, 1) + pl.plot(x, a, 'b', label='Source distribution') pl.title('Source distribution') - pl.subplot(2,1,2) - pl.plot(x,B,label='Target distributions') + pl.subplot(2, 1, 2) + pl.plot(x, B, label='Target distributions') pl.title('Target distributions') + pl.tight_layout() #%% Compute and plot distributions and loss matrix - d_emd=ot.emd2(a,B,M) # direct computation of EMD - d_emd2=ot.emd2(a,B,M2) # direct computation of EMD with loss M3 + d_emd = ot.emd2(a, B, M) # direct computation of EMD + d_emd2 = ot.emd2(a, B, M2) # direct computation of EMD with loss M3 pl.figure(2) - pl.plot(d_emd,label='Euclidean EMD') - pl.plot(d_emd2,label='Squared Euclidean EMD') + pl.plot(d_emd, label='Euclidean EMD') + pl.plot(d_emd2, label='Squared Euclidean EMD') pl.title('EMD distances') pl.legend() #%% - reg=1e-2 - d_sinkhorn=ot.sinkhorn(a,B,M,reg) - d_sinkhorn2=ot.sinkhorn(a,B,M2,reg) + reg = 1e-2 + d_sinkhorn = ot.sinkhorn2(a, B, M, reg) + d_sinkhorn2 = ot.sinkhorn2(a, B, M2, reg) pl.figure(2) pl.clf() - pl.plot(d_emd,label='Euclidean EMD') - pl.plot(d_emd2,label='Squared Euclidean EMD') - pl.plot(d_sinkhorn,'+',label='Euclidean Sinkhorn') - pl.plot(d_sinkhorn2,'+',label='Squared Euclidean Sinkhorn') + pl.plot(d_emd, label='Euclidean EMD') + pl.plot(d_emd2, label='Squared Euclidean EMD') + pl.plot(d_sinkhorn, '+', label='Euclidean Sinkhorn') + pl.plot(d_sinkhorn2, '+', label='Squared Euclidean Sinkhorn') pl.title('EMD distances') pl.legend() -**Total running time of the script:** ( 0 minutes 0.521 seconds) + + pl.show() + +**Total running time of the script:** ( 0 minutes 0.906 seconds) diff --git a/docs/source/auto_examples/plot_optim_OTreg.ipynb b/docs/source/auto_examples/plot_optim_OTreg.ipynb index 5ded922..0cb6ef2 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.ipynb +++ b/docs/source/auto_examples/plot_optim_OTreg.ipynb @@ -24,7 +24,7 @@ "execution_count": null, "cell_type": "code", "source": [ - "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n\n\n#%% parameters\n\nn=100 # nb bins\n\n# bin positions\nx=np.arange(n,dtype=np.float64)\n\n# Gaussian distributions\na=ot.datasets.get_1D_gauss(n,m=20,s=5) # m= mean, s= std\nb=ot.datasets.get_1D_gauss(n,m=60,s=10)\n\n# loss matrix\nM=ot.dist(x.reshape((n,1)),x.reshape((n,1)))\nM/=M.max()\n\n#%% EMD\n\nG0=ot.emd(a,b,M)\n\npl.figure(3)\not.plot.plot1D_mat(a,b,G0,'OT matrix G0')\n\n#%% Example with Frobenius norm regularization\n\ndef f(G): return 0.5*np.sum(G**2)\ndef df(G): return G\n\nreg=1e-1\n\nGl2=ot.optim.cg(a,b,M,reg,f,df,verbose=True)\n\npl.figure(3)\not.plot.plot1D_mat(a,b,Gl2,'OT matrix Frob. reg')\n\n#%% Example with entropic regularization\n\ndef f(G): return np.sum(G*np.log(G))\ndef df(G): return np.log(G)+1\n\nreg=1e-3\n\nGe=ot.optim.cg(a,b,M,reg,f,df,verbose=True)\n\npl.figure(4)\not.plot.plot1D_mat(a,b,Ge,'OT matrix Entrop. reg')\n\n#%% Example with Frobenius norm + entropic regularization with gcg\n\ndef f(G): return 0.5*np.sum(G**2)\ndef df(G): return G\n\nreg1=1e-3\nreg2=1e-1\n\nGel2=ot.optim.gcg(a,b,M,reg1,reg2,f,df,verbose=True)\n\npl.figure(5)\not.plot.plot1D_mat(a,b,Gel2,'OT entropic + matrix Frob. reg')\npl.show()" + "import numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n\n#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std\nb = ot.datasets.get_1D_gauss(n, m=60, s=10)\n\n# loss matrix\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))\nM /= M.max()\n\n#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3, figsize=(5, 5))\not.plot.plot1D_mat(a, b, G0, 'OT matrix G0')\n\n#%% Example with Frobenius norm regularization\n\n\ndef f(G):\n return 0.5 * np.sum(G**2)\n\n\ndef df(G):\n return G\n\n\nreg = 1e-1\n\nGl2 = ot.optim.cg(a, b, M, reg, f, df, verbose=True)\n\npl.figure(3)\not.plot.plot1D_mat(a, b, Gl2, 'OT matrix Frob. reg')\n\n#%% Example with entropic regularization\n\n\ndef f(G):\n return np.sum(G * np.log(G))\n\n\ndef df(G):\n return np.log(G) + 1.\n\n\nreg = 1e-3\n\nGe = ot.optim.cg(a, b, M, reg, f, df, verbose=True)\n\npl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Ge, 'OT matrix Entrop. reg')\n\n#%% Example with Frobenius norm + entropic regularization with gcg\n\n\ndef f(G):\n return 0.5 * np.sum(G**2)\n\n\ndef df(G):\n return G\n\n\nreg1 = 1e-3\nreg2 = 1e-1\n\nGel2 = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True)\n\npl.figure(5, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gel2, 'OT entropic + matrix Frob. reg')\npl.show()" ], "outputs": [], "metadata": { diff --git a/docs/source/auto_examples/plot_optim_OTreg.py b/docs/source/auto_examples/plot_optim_OTreg.py index 8abb426..276b250 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.py +++ b/docs/source/auto_examples/plot_optim_OTreg.py @@ -12,63 +12,80 @@ import matplotlib.pylab as pl import ot - #%% parameters -n=100 # nb bins +n = 100 # nb bins # bin positions -x=np.arange(n,dtype=np.float64) +x = np.arange(n, dtype=np.float64) # Gaussian distributions -a=ot.datasets.get_1D_gauss(n,m=20,s=5) # m= mean, s= std -b=ot.datasets.get_1D_gauss(n,m=60,s=10) +a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std +b = ot.datasets.get_1D_gauss(n, m=60, s=10) # loss matrix -M=ot.dist(x.reshape((n,1)),x.reshape((n,1))) -M/=M.max() +M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) +M /= M.max() #%% EMD -G0=ot.emd(a,b,M) +G0 = ot.emd(a, b, M) -pl.figure(3) -ot.plot.plot1D_mat(a,b,G0,'OT matrix G0') +pl.figure(3, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') #%% Example with Frobenius norm regularization -def f(G): return 0.5*np.sum(G**2) -def df(G): return G -reg=1e-1 +def f(G): + return 0.5 * np.sum(G**2) + + +def df(G): + return G -Gl2=ot.optim.cg(a,b,M,reg,f,df,verbose=True) + +reg = 1e-1 + +Gl2 = ot.optim.cg(a, b, M, reg, f, df, verbose=True) pl.figure(3) -ot.plot.plot1D_mat(a,b,Gl2,'OT matrix Frob. reg') +ot.plot.plot1D_mat(a, b, Gl2, 'OT matrix Frob. reg') #%% Example with entropic regularization -def f(G): return np.sum(G*np.log(G)) -def df(G): return np.log(G)+1 -reg=1e-3 +def f(G): + return np.sum(G * np.log(G)) + -Ge=ot.optim.cg(a,b,M,reg,f,df,verbose=True) +def df(G): + return np.log(G) + 1. -pl.figure(4) -ot.plot.plot1D_mat(a,b,Ge,'OT matrix Entrop. reg') + +reg = 1e-3 + +Ge = ot.optim.cg(a, b, M, reg, f, df, verbose=True) + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Ge, 'OT matrix Entrop. reg') #%% Example with Frobenius norm + entropic regularization with gcg -def f(G): return 0.5*np.sum(G**2) -def df(G): return G -reg1=1e-3 -reg2=1e-1 +def f(G): + return 0.5 * np.sum(G**2) + + +def df(G): + return G + + +reg1 = 1e-3 +reg2 = 1e-1 -Gel2=ot.optim.gcg(a,b,M,reg1,reg2,f,df,verbose=True) +Gel2 = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True) -pl.figure(5) -ot.plot.plot1D_mat(a,b,Gel2,'OT entropic + matrix Frob. reg') -pl.show()
\ No newline at end of file +pl.figure(5, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gel2, 'OT entropic + matrix Frob. reg') +pl.show() diff --git a/docs/source/auto_examples/plot_optim_OTreg.rst b/docs/source/auto_examples/plot_optim_OTreg.rst index 70cd26c..f417158 100644 --- a/docs/source/auto_examples/plot_optim_OTreg.rst +++ b/docs/source/auto_examples/plot_optim_OTreg.rst @@ -503,67 +503,85 @@ Regularized OT with generic solver import ot - #%% parameters - n=100 # nb bins + n = 100 # nb bins # bin positions - x=np.arange(n,dtype=np.float64) + x = np.arange(n, dtype=np.float64) # Gaussian distributions - a=ot.datasets.get_1D_gauss(n,m=20,s=5) # m= mean, s= std - b=ot.datasets.get_1D_gauss(n,m=60,s=10) + a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n, m=60, s=10) # loss matrix - M=ot.dist(x.reshape((n,1)),x.reshape((n,1))) - M/=M.max() + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M /= M.max() #%% EMD - G0=ot.emd(a,b,M) + G0 = ot.emd(a, b, M) - pl.figure(3) - ot.plot.plot1D_mat(a,b,G0,'OT matrix G0') + pl.figure(3, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') #%% Example with Frobenius norm regularization - def f(G): return 0.5*np.sum(G**2) - def df(G): return G - reg=1e-1 + def f(G): + return 0.5 * np.sum(G**2) + + + def df(G): + return G - Gl2=ot.optim.cg(a,b,M,reg,f,df,verbose=True) + + reg = 1e-1 + + Gl2 = ot.optim.cg(a, b, M, reg, f, df, verbose=True) pl.figure(3) - ot.plot.plot1D_mat(a,b,Gl2,'OT matrix Frob. reg') + ot.plot.plot1D_mat(a, b, Gl2, 'OT matrix Frob. reg') #%% Example with entropic regularization - def f(G): return np.sum(G*np.log(G)) - def df(G): return np.log(G)+1 - reg=1e-3 + def f(G): + return np.sum(G * np.log(G)) + + + def df(G): + return np.log(G) + 1. - Ge=ot.optim.cg(a,b,M,reg,f,df,verbose=True) - pl.figure(4) - ot.plot.plot1D_mat(a,b,Ge,'OT matrix Entrop. reg') + reg = 1e-3 + + Ge = ot.optim.cg(a, b, M, reg, f, df, verbose=True) + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, Ge, 'OT matrix Entrop. reg') #%% Example with Frobenius norm + entropic regularization with gcg - def f(G): return 0.5*np.sum(G**2) - def df(G): return G - reg1=1e-3 - reg2=1e-1 + def f(G): + return 0.5 * np.sum(G**2) + + + def df(G): + return G - Gel2=ot.optim.gcg(a,b,M,reg1,reg2,f,df,verbose=True) - pl.figure(5) - ot.plot.plot1D_mat(a,b,Gel2,'OT entropic + matrix Frob. reg') + reg1 = 1e-3 + reg2 = 1e-1 + + Gel2 = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True) + + pl.figure(5, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, Gel2, 'OT entropic + matrix Frob. reg') pl.show() -**Total running time of the script:** ( 0 minutes 2.319 seconds) + +**Total running time of the script:** ( 0 minutes 2.720 seconds) diff --git a/docs/source/conf.py b/docs/source/conf.py index b9f1f9d..ffdb1a2 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -261,7 +261,7 @@ latex_elements = { # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, 'POT.tex', u'POT Python Optimal Transport library', - u'Rémi Flamary, Nicolas Courty', 'manual'), + author, 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -305,7 +305,7 @@ man_pages = [ # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'POT', u'POT Python Optimal Transport library Documentation', - author, 'POT', 'One line description of project.', + author, 'POT', 'Python Optimal Transport librar.', 'Miscellaneous'), ] @@ -326,7 +326,7 @@ texinfo_documents = [ intersphinx_mapping = {'https://docs.python.org/': None} sphinx_gallery_conf = { - 'examples_dirs': '../../examples', + 'examples_dirs': ['../../examples','../../examples/da'], 'gallery_dirs': 'auto_examples', 'mod_example_dir': '../modules/generated/', 'reference_url': { diff --git a/docs/source/readme.rst b/docs/source/readme.rst index c1e0017..e5c61f5 100644 --- a/docs/source/readme.rst +++ b/docs/source/readme.rst @@ -187,6 +187,9 @@ The contributors to this library are: - `Michael Perrot <http://perso.univ-st-etienne.fr/pem82055/>`__ (Mapping estimation) - `Léo Gautheron <https://github.com/aje>`__ (GPU implementation) +- `Nathalie + Gayraud <https://www.linkedin.com/in/nathalie-t-h-gayraud/?ppe=1>`__ +- `Stanislas Chambon <https://slasnista.github.io/>`__ This toolbox benefit a lot from open source research and we would like to thank the following persons for providing some code (in various |