{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Halfspace depth \n\nSample usage of halfspace depth computation.\nIt will plot samples and dataset based on halfspace depth values.\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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "## Creating dataset and samples\nnp.random.seed(2801)\nmat1=[[1, 0],[0, 2]]\nmat2=[[1, 0],[0, 1]]\nX = np.random.multivariate_normal([1,1], mat2, 50)\ndataset = np.random.multivariate_normal([0,0], mat1, 500)\n\n## visualize points\nfig=plt.figure()\nplt.scatter(dataset[:,0],dataset[:,1], c=\"blue\",label=\"Dataset\")\nplt.scatter(X[:,0],X[:,1], c=\"red\",label=\"New points\")\nplt.xlabel(\"First component\")\nplt.ylabel(\"Second component\")\nplt.title(\"Dataset and Sample visualization\")\nplt.legend()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create model and load dataset for depth computation \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model=DepthEucl().load_dataset(dataset)\ndepthX=model.halfspace(X,exact=True) # Compute X Depth w.r.t. the dataset \ndepthDataset=model.halfspace(exact=True, evaluate_dataset=True) # evaluate the dataset itself\n\nfig, (ax1,ax2)=plt.subplots(1,2,figsize=(10,5))\nax2.scatter(dataset[:,0],dataset[:,1],s=2, label=\"Dataset\",marker=\"D\")\nfor x, depth, name, ax in zip([dataset,X],[depthDataset,depthX],\n [\"Dataset\", \"Samples\"],[ax1,ax2]):\n ax.scatter(x[:,0],x[:,1], c=depth,label=name)\n ax.set_xlabel(\"First component\")\n ax.set_ylabel(\"Second component\")\n ax.set_title(f\"{name} halfspace depth visualization\")\nax2.legend()\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 }