{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# OT with Laplacian regularization for domain adaptation\n\n

Note

Example added in release: 0.7.0.

\n\nThis example introduces a domain adaptation in a 2D setting and OTDA\napproach with Laplacian regularization.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Ievgen Redko \n\n# License: MIT License\n\nimport matplotlib.pylab as pl\nimport ot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n_source_samples = 150\nn_target_samples = 150\n\nXs, ys = ot.datasets.make_data_classif(\"3gauss\", n_source_samples)\nXt, yt = ot.datasets.make_data_classif(\"3gauss2\", n_target_samples)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instantiate the different transport algorithms and fit them\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# EMD Transport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# Sinkhorn Transport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=0.01)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# EMD Transport with Laplacian regularization\not_emd_laplace = ot.da.EMDLaplaceTransport(reg_lap=100, reg_src=1)\not_emd_laplace.fit(Xs=Xs, Xt=Xt)\n\n# transport source samples onto target samples\ntransp_Xs_emd = ot_emd.transform(Xs=Xs)\ntransp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs)\ntransp_Xs_emd_laplace = ot_emd_laplace.transform(Xs=Xs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fig 1 : plots source and target samples\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(1, figsize=(10, 5))\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker=\"+\", label=\"Source samples\")\npl.xticks([])\npl.yticks([])\npl.legend(loc=0)\npl.title(\"Source samples\")\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker=\"o\", label=\"Target samples\")\npl.xticks([])\npl.yticks([])\npl.legend(loc=0)\npl.title(\"Target samples\")\npl.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fig 2 : plot optimal couplings and transported samples\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "param_img = {\"interpolation\": \"nearest\", \"cmap\": \"gray_r\"}\n\npl.figure(2, figsize=(15, 8))\npl.subplot(2, 3, 1)\npl.imshow(ot_emd.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title(\"Optimal coupling\\nEMDTransport\")\n\npl.figure(2, figsize=(15, 8))\npl.subplot(2, 3, 2)\npl.imshow(ot_sinkhorn.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title(\"Optimal coupling\\nSinkhornTransport\")\n\npl.subplot(2, 3, 3)\npl.imshow(ot_emd_laplace.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title(\"Optimal coupling\\nEMDLaplaceTransport\")\n\npl.subplot(2, 3, 4)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker=\"o\", label=\"Target samples\", alpha=0.3)\npl.scatter(\n transp_Xs_emd[:, 0],\n transp_Xs_emd[:, 1],\n c=ys,\n marker=\"+\",\n label=\"Transp samples\",\n s=30,\n)\npl.xticks([])\npl.yticks([])\npl.title(\"Transported samples\\nEmdTransport\")\npl.legend(loc=\"lower left\")\n\npl.subplot(2, 3, 5)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker=\"o\", label=\"Target samples\", alpha=0.3)\npl.scatter(\n transp_Xs_sinkhorn[:, 0],\n transp_Xs_sinkhorn[:, 1],\n c=ys,\n marker=\"+\",\n label=\"Transp samples\",\n s=30,\n)\npl.xticks([])\npl.yticks([])\npl.title(\"Transported samples\\nSinkhornTransport\")\n\npl.subplot(2, 3, 6)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker=\"o\", label=\"Target samples\", alpha=0.3)\npl.scatter(\n transp_Xs_emd_laplace[:, 0],\n transp_Xs_emd_laplace[:, 1],\n c=ys,\n marker=\"+\",\n label=\"Transp samples\",\n s=30,\n)\npl.xticks([])\npl.yticks([])\npl.title(\"Transported samples\\nEMDLaplaceTransport\")\npl.tight_layout()\n\npl.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.18" } }, "nbformat": 4, "nbformat_minor": 0 }