diff --git a/ch_01/concept_image.ipynb b/ch_01/concept_image.ipynb index 5661495..5a22fbf 100644 --- a/ch_01/concept_image.ipynb +++ b/ch_01/concept_image.ipynb @@ -1,141 +1,174 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Conceptual Overview\n", - "\n", - "This paints a picture showing the k-NN concept.\n", - "\n", - "An unknown sample, shown as a ◆ is surrounded by two groups of known items, shown as ⚫︎ and ◼︎.\n", - "\n", - "In this example, we're using the Euclidean distance between points, computed by `math.hypot`. There are 3 neighbors within a distance of 2.0. Among these, two are ⚫︎, and one is ◼︎. For a k-value of 3, the most common neighbor is ⚫︎.\n", - "\n", - "If we switch to k-value of 5, we dicover 3 ◼︎ and 2 ⚫︎, flipping the outcome.\n", - "\n", - "This situation is relatively rare and requires an unknown sample to be carefully perched almost midway between two populations." - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ + "cells": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Using matplotlib backend: MacOSX\n" - ] - } - ], - "source": [ - "%matplotlib" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ + "cell_type": "markdown", + "metadata": { + "id": "VDSB-CF72k2v" + }, + "source": [ + "# Conceptual Overview\n", + "\n", + "This paints a picture showing the k-NN concept.\n", + "\n", + "An unknown sample, shown as a ◆ is surrounded by two groups of known items, shown as ⚫︎ and ◼︎.\n", + "\n", + "In this example, we're using the Euclidean distance between points, computed by `math.hypot`. There are 3 neighbors within a distance of 2.0. Among these, two are ⚫︎, and one is ◼︎. For a k-value of 3, the most common neighbor is ⚫︎.\n", + "\n", + "If we switch to k-value of 5, we dicover 3 ◼︎ and 2 ⚫︎, flipping the outcome.\n", + "\n", + "This situation is relatively rare and requires an unknown sample to be carefully perched almost midway between two populations." + ] + }, { - "name": "stderr", - "output_type": "stream", - "text": [ - ":21: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.\n", - " plt.axes().add_patch(circle3)\n", - ":26: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.\n", - " plt.axes().add_patch(circle5)\n" - ] - } - ], - "source": [ - "import matplotlib.pyplot as plt\n", - "import matplotlib.path\n", - "from matplotlib.patches import Circle\n", - "\n", - "sample = (5, 5)\n", - "circles = [(2, 8), (4, 6), (5, 7)]\n", - "squares = [(4, 2), (6, 2), (6, 4)]\n", - "\n", - "plt.figure(\"Concept\", (5, 5))\n", - "plt.set_cmap('gray')\n", - "\n", - "# plt.axis((0, 10, 0, 10), option='equal')\n", - "plt.axis('equal')\n", - "plt.scatter(*sample, marker=\"D\", label=\"??\", color='0.0')\n", - "plt.scatter([x for x, y in circles], [y for x, y in circles], marker=\"o\", color='.20')\n", - "plt.scatter([x for x, y in squares], [y for x, y in squares], marker=\"s\", color='.33')\n", - "\n", - "# k = 3 nearest neighbors\n", - "circle3 = Circle((5, 5), 2, facecolor='none',\n", - " edgecolor='black', linestyle='--', alpha=0.8)\n", - "plt.axes().add_patch(circle3)\n", - "\n", - "# k = 5 nearest neighbors\n", - "circle5 = Circle((5, 5), 3.2, facecolor='none',\n", - " edgecolor='black', linestyle=':', alpha=1.0)\n", - "plt.axes().add_patch(circle5)\n", - "\n", - "\n", - "plt.grid(True)\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ + "cell_type": "code", + "execution_count": 9, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wJ6DPFtR2k21", + "outputId": "26eeeb3b-1e8d-4589-c439-71e6ef6810cc" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Using matplotlib backend: agg\n" + ] + } + ], + "source": [ + "%matplotlib" + ] + }, { - "data": { - "text/plain": [ - "[1.4142135623730951,\n", - " 1.4142135623730951,\n", - " 2.0,\n", - " 3.1622776601683795,\n", - " 3.1622776601683795,\n", - " 4.242640687119286]" + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "m0ZOxz182k23" + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import matplotlib.path\n", + "from matplotlib.patches import Circle\n", + "\n", + "sample = (5, 5)\n", + "circles = [(2, 8), (4, 6), (5, 7)]\n", + "squares = [(4, 2), (6, 2), (6, 4)]\n", + "\n", + "plt.figure(\"Concept\", (5, 5))\n", + "plt.set_cmap('gray')\n", + "\n", + "# plt.axis((0, 10, 0, 10), option='equal')\n", + "plt.axis('equal')\n", + "plt.scatter(*sample, marker=\"D\", label=\"??\", color='0.0')\n", + "plt.scatter([x for x, y in circles], [y for x, y in circles], marker=\"o\", color='.20')\n", + "plt.scatter([x for x, y in squares], [y for x, y in squares], marker=\"s\", color='.33')\n", + "\n", + "# k = 3 nearest neighbors\n", + "circle3 = Circle((5, 5), 2, facecolor='none',\n", + " edgecolor='black', linestyle='--', alpha=0.8)\n", + "plt.axes().add_patch(circle3)\n", + "\n", + "# k = 5 nearest neighbors\n", + "circle5 = Circle((5, 5), 3.2, facecolor='none',\n", + " edgecolor='black', linestyle=':', alpha=1.0)\n", + "plt.axes().add_patch(circle5)\n", + "\n", + "\n", + "plt.grid(True)\n", + "plt.show()" ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "aGjCVVI92k24", + "outputId": "f66db412-c609-4e7b-80a2-9dcad227797f" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1.4142135623730951,\n", + " 1.4142135623730951,\n", + " 2.0,\n", + " 3.1622776601683795,\n", + " 3.1622776601683795,\n", + " 4.242640687119286]" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ], + "source": [ + "import math\n", + "sorted(math.hypot((sample[0]-t[0]), (sample[1]-t[1])) for t in circles+squares)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e590QXLy2k24", + "outputId": "04796ff9-94b7-432c-cbfc-f35079a5cea9" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "pokus\n" + ] + } + ], + "source": [ + "print(\"pokus\")" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "2JSkV7RhoiBE" + }, + "execution_count": null, + "outputs": [] + } + ], + "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.9.0" + }, + "colab": { + "provenance": [] } - ], - "source": [ - "import math\n", - "sorted(math.hypot((sample[0]-t[0]), (sample[1]-t[1])) for t in circles+squares)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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.9.0" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file