{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Depth contour visualization \n\nContour visualization with different depth notions w.r.t. a dataset.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from depth.model.DepthEucl import DepthEucl \nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom depth.plotDepth import depth_mesh, depth_plot2d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "np.random.seed(2801)\nmat1=[[1, 0],[0, 2]]\ndataset = np.random.multivariate_normal([0,0], mat1, 500)\nmodel=DepthEucl().load_dataset(dataset)\n## visualize points\nfig=plt.figure()\nplt.scatter(dataset[:,0],dataset[:,1], c=\"blue\",)\nplt.xlabel(\"First component\")\nplt.ylabel(\"Second component\")\nplt.title(\"Dataset visualization\")\nplt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1,3,figsize=(15,5))\nfor ind,notion in enumerate([\"halfspace\",\"L2\",\"mahalanobis\"]):\n xs,ys,depth=depth_mesh(model=model,notion=notion,)\n ax[ind].scatter(dataset[:,0],dataset[:,1],s=2)\n contours =ax[ind].contour(xs,ys,depth,10,)\n ax[ind].clabel(contours, inline=True, fontsize=8)\n ax[ind].set_xlabel(\"First component\")\n ax[ind].set_ylabel(\"Second component\")\n ax[ind].set_title(f\"Depth contour using {notion} depth\")\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "modify dataset\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dataset1 = np.random.multivariate_normal([3,3], mat1, 50)\ndataset2 = np.random.multivariate_normal([-3,3], mat1, 50)\n## visualize points\nfig=plt.figure()\nplt.scatter(dataset[:,0],dataset[:,1], c=\"blue\",label=\"Original\")\nplt.scatter(dataset1[:,0],dataset1[:,1], c=\"red\",label=\"New dataset 1\")\nplt.scatter(dataset2[:,0],dataset2[:,1], c=\"black\",label=\"New dataset 2\")\nplt.xlabel(\"First component\")\nplt.ylabel(\"Second component\")\nplt.title(\"Datasets visualization\")\nplt.legend()\nplt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots(1,3,figsize=(15,5))\nfor ind,DS in enumerate([None,dataset1,dataset2]):\n if ind>0:\n model.change_dataset(DS,keepOld=True)\n xs,ys,depth=depth_mesh(model=model,notion=\"halfspace\",)\n ax[ind].scatter(model.data[:,0],model.data[:,1],s=2)\n contours =ax[ind].contour(xs,ys,depth,10,)\n ax[ind].clabel(contours, inline=True, fontsize=8)\n ax[ind].set_xlabel(\"First component\")\n ax[ind].set_ylabel(\"Second component\")\nplt.suptitle(f\"Depth contour using halfspace depth\")\nplt.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.13.4" } }, "nbformat": 4, "nbformat_minor": 0 }