{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f413d164-9481-45c8-aeac-c3b78227f3b1", "metadata": { "slideshow": { "slide_type": "skip" }, "tags": [] }, "outputs": [], "source": [ "import altair\n", "from IPython.display import display\n", "from sklearn.neighbors import LocalOutlierFactor\n", "from sklearn.datasets import load_iris, load_linnerud" ] }, { "cell_type": "markdown", "id": "3ed09647-68e0-4e8f-a40b-ae477261c9a2", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Example A" ] }, { "cell_type": "code", "execution_count": 2, "id": "60876124-3c7f-4e90-87f8-f2c0e4ff6526", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)Outlier
05.13.51.40.2False
14.93.01.40.2False
24.73.21.30.2False
\n", "
" ], "text/plain": [ " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \\\n", "0 5.1 3.5 1.4 0.2 \n", "1 4.9 3.0 1.4 0.2 \n", "2 4.7 3.2 1.3 0.2 \n", "\n", " Outlier \n", "0 False \n", "1 False \n", "2 False " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = load_iris(as_frame=True).data\n", "data[\"Outlier\"] = LocalOutlierFactor(50).fit_predict(data) == -1\n", "display(data[:3])\n", "altair.Chart(data).mark_point().encode(\n", " x=\"sepal length (cm)\",\n", " y=\"petal length (cm)\",\n", " color=\"Outlier\"\n", ")" ] }, { "cell_type": "markdown", "id": "91c6216b-fe6b-4d08-a207-97605accd0fc", "metadata": { "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Example B" ] }, { "cell_type": "code", "execution_count": 3, "id": "74cf033f-d65c-485d-8fef-75414a6799c8", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ChinsSitupsJumpsOutlier
05.0162.060.0False
12.0110.060.0False
212.0101.0101.0False
\n", "
" ], "text/plain": [ " Chins Situps Jumps Outlier\n", "0 5.0 162.0 60.0 False\n", "1 2.0 110.0 60.0 False\n", "2 12.0 101.0 101.0 False" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = load_linnerud(as_frame=True).data\n", "data[\"Outlier\"] = LocalOutlierFactor(10).fit_predict(data) == -1\n", "display(data[:3])\n", "altair.Chart(data).mark_point().encode(\n", " x=\"Chins\",\n", " y=\"Jumps\",\n", " color=\"Outlier\"\n", ")" ] } ], "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.7.3" }, "toc-autonumbering": false, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 5 }