uczenie-maszynowe/wyk/09_NB_i_KNN.ipynb

2208 lines
975 KiB
Plaintext
Raw Normal View History

2022-12-09 15:06:17 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Uczenie maszynowe\n",
"# 9. Przegląd metod uczenia nadzorowanego część 1"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 9.1. Naiwny klasyfikator bayesowski"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"* Naiwny klasyfikator bayesowski jest algorytmem dla problemu klasyfikacji wieloklasowej.\n",
"* Naszym celem jest znalezienie funkcji uczącej $f \\colon x \\mapsto y$, gdzie $y$ oznacza jedną ze zdefiniowanych wcześniej klas.\n",
"* Klasyfikacja probabilistyczna polega na wskazaniu klasy o najwyższym prawdopodobieństwie:\n",
"$$ \\hat{y} = \\mathop{\\arg \\max}_y P( y \\,|\\, x ) $$\n",
"* Naiwny klasyfikator bayesowski należy do rodziny klasyfikatorów probabilistycznych"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img style=\"float: right;\" src=\"https://upload.wikimedia.org/wikipedia/commons/d/d4/Thomas_Bayes.gif\">\n",
"\n",
"**Thomas Bayes** (wymowa: /beɪz/) (17021761) angielski matematyk i duchowny"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Twierdzenie Bayesa wzór ogólny\n",
"\n",
"$$ P( Y \\,|\\, X ) = \\frac{ P( X \\,|\\, Y ) \\cdot P( Y ) }{ P ( X ) } $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Twierdzenie Bayesa opisuje związek między prawdopodobieństwami warunkowymi dwóch zdarzeń warunkujących się nawzajem."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Twierdzenie Bayesa\n",
"(po zastosowaniu wzoru na prawdopodobieństwo całkowite)\n",
"\n",
"$$ \\underbrace{P( y_k \\,|\\, x )}_\\textrm{ prawd. a posteriori } = \\frac{ \\overbrace{ P( x \\,|\\, y_k )}^\\textrm{ model klasy } \\cdot \\overbrace{P( y_k )}^\\textrm{ prawd. a priori } }{ \\underbrace{\\sum_{i} P( x \\,|\\, y_i ) \\, P( y_i )}_\\textrm{wyrażenie normalizacyjne} } $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
" * W tym przypadku „zdarzenie $x$” oznacza, że cechy wejściowe danej obserwacji przyjmują wartości opisane wektorem $x$.\n",
" * „Zdarzenie $y_k$” oznacza, że dana obserwacja należy do klasy $y_k$.\n",
" * **Model klasy** $y_k$ opisuje rozkład prawdopodobieństwa cech obserwacji należących do tej klasy.\n",
" * **Prawdopodobieństwo *a priori*** to prawdopodobienstwo, że losowa obserwacja należy do klasy $y_k$.\n",
" * **Prawdopodobieństwo *a posteriori*** to prawdopodobieństwo, którego szukamy: że obserwacja opisana wektorem cech $x$ należy do klasy $y_k$."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Rola wyrażenia normalizacyjnego w twierdzeniu Bayesa\n",
"\n",
" * Wartość wyrażenia normalizacyjnego nie wpływa na wynik klasyfikacji.\n",
"\n",
"**Przykład**: obserwacja nietypowa ma małe prawdopodobieństwo względem dowolnej klasy, wyrażenie normalizacyjne sprawia, że to prawdopodobieństwo staje się porównywalne z prawdopodobieństwami typowych obserwacji, ale nie wpływa na klasyfikację!"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Klasyfikatory dyskryminatywne a generatywne\n",
"\n",
"* Klasyfikatory generatywne tworzą model rozkładu prawdopodobieństwa dla każdej z klas.\n",
"* Klasyfikatory dyskryminatywne wyznaczają granicę klas (*decision boundary*) bezpośrednio.\n",
"* Naiwny klasyfikator bayesowski jest klasyfikatorem generatywnym (ponieważ wyznacza $P( x \\,|\\, y )$).\n",
"* Wszystkie klasyfikatory generatywne są probabilistyczne, ale nie na odwrót.\n",
"* Regresja logistyczna jest przykładem klasyfikatora dyskryminatywnego."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Założenie niezależności dla naiwnego klasyfikatora bayesowskiego\n",
"\n",
"* Naiwny klasyfikator bayesowski jest *naiwny*, ponieważ zakłada, że poszczególne cechy są niezależne od siebie:\n",
"$$ P( x_1, \\ldots, x_n \\,|\\, y ) \\,=\\, \\prod_{i=1}^n P( x_i \\,|\\, x_1, \\ldots, x_{i-1}, y ) \\,=\\, \\prod_{i=1}^n P( x_i \\,|\\, y ) $$\n",
"* To założenie jest bardzo przydatne ze względów obliczeniowych, ponieważ bardzo często mamy do czynienia z ogromną liczbą cech (bitmapy, słowniki itp.)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Naiwny klasyfikator bayesowski przykład"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 2,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Przydtne importy\n",
"\n",
"import ipywidgets as widgets\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 3,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Wczytanie danych (gatunki kosaćców)\n",
"\n",
"data_iris = pandas.read_csv('iris.csv')\n",
"data_iris_setosa = pandas.DataFrame()\n",
"data_iris_setosa['dł. płatka'] = data_iris['pl'] # \"pl\" oznacza \"petal length\"\n",
"data_iris_setosa['szer. płatka'] = data_iris['pw'] # \"pw\" oznacza \"petal width\"\n",
"data_iris_setosa['Iris setosa?'] = data_iris['Gatunek'].apply(lambda x: 1 if x=='Iris-setosa' else 0)\n",
"\n",
"m, n_plus_1 = data_iris_setosa.values.shape\n",
"n = n_plus_1 - 1\n",
"Xn = data_iris_setosa.values[:, 0:n].reshape(m, n)\n",
"\n",
"X = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
"Y = np.matrix(data_iris_setosa.values[:, 2]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 4,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"liczba przykładów: {0: 100, 1: 50}\n",
"prior probability: {0: 0.6666666666666666, 1: 0.3333333333333333}\n"
]
}
],
"source": [
"classes = [0, 1]\n",
"count = [sum(1 if y == c else 0 for y in Y.T.tolist()[0]) for c in classes]\n",
"prior_prob = [float(count[c]) / float(Y.shape[0]) for c in classes]\n",
"\n",
"print('liczba przykładów: ', {c: count[c] for c in classes})\n",
"print('prior probability:', {c: prior_prob[c] for c in classes})"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 5,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wykres danych (wersja macierzowa)\n",
"def plot_data_for_classification(X, Y, xlabel, ylabel): \n",
" fig = plt.figure(figsize=(16*.6, 9*.6))\n",
" ax = fig.add_subplot(111)\n",
" fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)\n",
" X = X.tolist()\n",
" Y = Y.tolist()\n",
" X1n = [x[1] for x, y in zip(X, Y) if y[0] == 0]\n",
" X1p = [x[1] for x, y in zip(X, Y) if y[0] == 1]\n",
" X2n = [x[2] for x, y in zip(X, Y) if y[0] == 0]\n",
" X2p = [x[2] for x, y in zip(X, Y) if y[0] == 1]\n",
" ax.scatter(X1n, X2n, c='r', marker='x', s=50, label='Dane')\n",
" ax.scatter(X1p, X2p, c='g', marker='o', s=50, label='Dane')\n",
" \n",
" ax.set_xlabel(xlabel)\n",
" ax.set_ylabel(ylabel)\n",
" ax.margins(.05, .05)\n",
" return fig"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 6,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0cAAAHvCAYAAACfaqQpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABVmElEQVR4nO3de3xU1b3///dMJgnRkGis3GSiRAheQKVgY7goHmO9cDRof9XyrUCpWuVSuaht7bfVU2qLtYo9fWBA+/VS47FE2yJWLcjFUAgEuVbwhoglqVy0ggnBNJNk9u+PfRIyyWQyO5k9s2fm9Xw85qGz92dWPrN2bNcna++1XIZhGAIAAACAJOeOdQIAAAAA4AQURwAAAAAgiiMAAAAAkERxBAAAAACSKI4AAAAAQBLFEQAAAABIojgCAAAAAEkURwAAAAAgSfLEOoFo8/v9OnDggHr37i2XyxXrdAAAAADYyDAMHTt2TAMGDJDbHXpuKOmKowMHDsjr9cY6DQAAAABRVF1drYEDB4aMSbriqHfv3pLMzsnKyopxNgAAAADsVFtbK6/X21oHhJJ0xVHLrXRZWVkURwAAAECSCOeRGhZkAAAAAABRHAEAAACAJIojAAAAAJBEcQQAAAAAkiiOAAAAAEASxREAAAAASKI4AgAAAABJFEcAAAAAIIniCAAAAAAkURwBAAAAgCSKIwAAALTn8/XsfKRZzcdp+SNuUBwBAADghLIyafhwqbo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zFe4bVsdbFv9rkk
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 7,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"średnia: [matrix([[1. , 4.906, 1.676]]), matrix([[1. , 1.464, 0.244]])]\n",
"odchylenie standardowe: [matrix([[0. , 0.8214402 , 0.42263933]]), matrix([[0. , 0.17176728, 0.10613199]])]\n",
"(1, 3)\n"
]
}
],
"source": [
"XY = np.column_stack((X, Y))\n",
"XY_split = [XY[np.where(XY[:,3] == c)[0]] for c in classes]\n",
"X_split = [XY_split[c][:,0:3] for c in classes]\n",
"Y_split = [XY_split[c][:,3] for c in classes]\n",
"\n",
"X_mean = [np.mean(X_split[c], axis=0) for c in classes]\n",
"X_std = [np.std(X_split[c], axis=0) for c in classes]\n",
"print('średnia: ', X_mean) \n",
"print('odchylenie standardowe: ', X_std)\n",
"\n",
"print(X_std[0].shape)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 8,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Rysowanie średnich\n",
"def draw_means(fig, means, xmin=0.0, xmax=7.0, ymin=0.0, ymax=7.0):\n",
" class_color = {0: 'r', 1: 'g'}\n",
" classes = range(len(means))\n",
" ax = fig.axes[0]\n",
" mean_x1 = [means[c].item(0, 1) for c in classes]\n",
" mean_x2 = [means[c].item(0, 2) for c in classes]\n",
" for c in classes:\n",
" ax.plot([mean_x1[c], mean_x1[c]], [xmin, xmax],\n",
" color=class_color.get(c, 'c'), linestyle='dashed')\n",
" ax.plot([ymin, ymax], [mean_x2[c], mean_x2[c]],\n",
" color=class_color.get(c, 'c'), linestyle='dashed') "
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 9,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"from scipy.stats import norm\n",
"\n",
"# Prawdopodobieństwo klasy dla pojedynczej cechy\n",
"# Uwaga: jeżeli odchylenie standardowe dla danej cechy jest równe 0, \n",
"# to nie można określić prawdopodbieństwa klasy!\n",
"def prob(x, c, feature, mean, std):\n",
" sd = std[c].item(0, feature)\n",
" if sd == 0:\n",
" print('Nie można określić prawdopodobieństwa klasy dla cechy {}.!'.format(feature))\n",
" return norm(mean[c].item(0, feature), sd).pdf(x)\n",
"\n",
"# Prawdopodobieństwo klasy\n",
"# Uwaga: tu bierzemy iloczyn dwóch cech (1. i 2.), w ogólności może być ich więcej\n",
"def class_prob(x, c, mean, std, features=[1, 2]):\n",
" result = 1\n",
" for feature in features:\n",
" result *= prob(x[feature], c, feature, mean, std)\n",
" return result"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 10,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 3)\n",
"[matrix([[0. , 0.8214402 , 0.42263933]]), matrix([[0. , 0.17176728, 0.10613199]])]\n",
"[matrix([[1. , 4.906, 1.676]]), matrix([[1. , 1.464, 0.244]])]\n",
"[[1.57003335e-06 1.61965173e-23 3.09005273e-08]]\n"
]
}
],
"source": [
"print(X_std[0].shape)\n",
"print(X_std)\n",
"print(X_mean)\n",
"\n",
"X_prob_0=class_prob(X, 0, X_mean, X_std)\n",
"print(X_prob_0)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 11,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wykres prawdopodobieństw klas\n",
"def plot_prob(fig, X_mean, X_std, classes, xmin=0.0, xmax=7.0, ymin=0.0, ymax=7.0):\n",
" class_color = {0: 'r', 1: 'g'}\n",
" ax = fig.axes[0]\n",
" x1, x2 = np.meshgrid(np.arange(xmin, xmax, 0.02),\n",
" np.arange(xmin, xmax, 0.02))\n",
" for c in classes:\n",
" fun1 = lambda x: prob(x, c, 1, X_mean, X_std)\n",
" fun2 = lambda x: prob(x, c, 2, X_mean, X_std)\n",
" p = fun1(x1) * fun2(x2)\n",
" plt.contour(x1, x2, p, levels=np.arange(0.0, 1.0, 0.1),\n",
" colors=class_color.get(c, 'c'), lw=3)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 12,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/1042079336.py:11: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p, levels=np.arange(0.0, 1.0, 0.1),\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAYAAACc3aoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAACaQklEQVR4nOzdd3xTVf8H8E9Gd2lLaQutFFr23kNAlmxRQRQVUXD/RBQUQUUfBRygKIgTHI/geBCVR0BxIFsECpShsimrZbeMtkl3cn5/nOc2SZu0SZv0punn/XrdV9Obk3tPbsrlfHPO+R6NEEKAiIiIiIjIh2jVrgAREREREZG7MdAhIiIiIiKfw0CHiIiIiIh8DgMdIiIiIiLyOQx0iIiIiIjI5zDQISIiIiIin8NAh4iIiIiIfA4DHSIiIiIi8jl6tStQGWazGefOnUOtWrWg0WjUrg4REREREXmQEALZ2dmIi4uDVlt2n021DnTOnTuH+Ph4tatBRERERERVKC0tDfXr1y+zTLUOdGrVqgVAvtGwsDCVa0NERERERJ6UlZWF+Pj44jigLNU60FGGq4WFhTHQISIiIiKqIZyZtsJkBERERERE5HMY6BARERERkc9hoENERERERD6HgQ4REREREfkcBjpERERERORzGOgQEREREZHPYaBDREREREQ+h4EOERERERH5HAY6RERERETkcxjoEBERERGRz2GgQ0REREREPoeBDhERERER+RwGOkRERERE5HNUDXQSEhKg0WhKbRMnTlSzWkREREREVM3p1Tz5rl27YDKZin/fv38/Bg0ahNGjR6tYKyIiIiIiqu5UDXSio6Ntfn/jjTfQuHFj9O3bV6UaERERERGRL1A10LFWUFCAr7/+GlOmTIFGo7FbJj8/H/n5+cW/Z2VlVVX1iIiIiIioGvGaZAQrV67EtWvXcP/99zssM2fOHISHhxdv8fHxVVdBqnGMBUZoZmmgmaWBscCodnWIiEhhNAIajdyMvD8TkX1eE+j8+9//xrBhwxAXF+ewzPTp05GZmVm8paWlVWENiYiIiIiouvCKoWunT5/GunXr8MMPP5RZLiAgAAEBAVVUKyIiIiIiqq68ItBZvHgxYmJiMHz4cLWrQlTMT+eHuQPnFj8mIiIv4ecHzJ1reUxEZIdGCCHUrIDZbEZiYiLGjBmDN954w6XXZmVlITw8HJmZmQgLC/NQDYmIiIiIyBu40v5XfY7OunXrkJqaigcffFDtqhARERERkY9Qfeja4MGDoXKnEpFdJrMJe87vAQB0iu0EnVanco2IiAgAYDIBe+T9GZ06ATren4moNNUDHSJvlVeUh26fdQMAGKYbEOIfonKNiIgIAJCXB3ST92cYDEAI789EVJrqQ9eIiIiIiIjcjYEOERERERH5HAY6RERERETkcxjoEBERERGRz2GgQ0REREREPoeBDhERERER+RymlyZywE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l0sj4mI7NAIIYTalaiorKwshIeHIzMzE2FhYWpXh4iIiIiIPMiV9j+/BiEiIiIiIp/DQIeIiIiIiHwOAx0iB3IKc5CwIAEJCxKQU5ijdnWIiEiRkwMkJMgth/dnIrKPyQiIHBBC4HTm6eLHRETkJYQATp+2PCYisoM9OkRERERE5HMY6BARERERkc9hoENERERERD6HgQ4REREREfkcBjpERERERORzmHWNyAGNRoNW0a2KHxMRkZfQaIBWrSyPiYjsYKBD5ECwXzAOPH5A7WoQEVFJwcHAAd6fiahsHLpGREREREQ+h4EOERERERH5HAY6RA7kFOag9Uet0fqj1sgpzFG7OkREpMjJAVq3llsO789EZB/n6BA5IITAwfS
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"draw_means(fig, X_mean)\n",
"plot_prob(fig, X_mean, X_std, classes)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 13,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Prawdopodobieństwo a posteriori\n",
"def posterior_prob(x, c):\n",
" normalizer = sum(class_prob(x, c, X_mean, X_std)\n",
" * prior_prob[c]\n",
" for c in classes)\n",
" return (class_prob(x, c, X_mean, X_std) \n",
" * prior_prob[c]\n",
" / normalizer)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Aby teraz przewidzieć klasę $y$ dla dowolnego zestawu cech $x$, wystarczy sprawdzić, dla której klasy prawdopodobieństwo *a posteriori* jest większe:"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 14,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# Funkcja klasyfikująca (funkcja predykcji)\n",
"def predict_class(x):\n",
" p = [posterior_prob(x, c) for c in classes]\n",
" if p[1] > p[0]:\n",
" return 1\n",
" else:\n",
" return 0"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 15,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"0\n"
]
}
],
"source": [
"x = [1, 2.0, 0.5] # długość płatka: 2.0, szerokość płatka: 0.5\n",
"y = predict_class(x)\n",
"print(y) # 1 To prawdopodobnie jest Iris setosa\n",
"\n",
"x = [1, 2.5, 1.0] # długość płatka: 2.5, szerokość płatka: 1.0\n",
"y = predict_class(x)\n",
"print(y) # 0 To prawdopodobnie nie jest Iris setosa"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Zobaczmy, jak to wygląda na wykresie. Narysujemy w tym celu granicę między klasą 1 a 0:"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 16,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# Wykres granicy klas dla naiwnego Bayesa\n",
"def plot_decision_boundary_bayes(fig, X_mean, X_std, xmin=0.0, xmax=7.0, ymin=0.0, ymax=7.0):\n",
" ax = fig.axes[0]\n",
" x1, x2 = np.meshgrid(np.arange(xmin, xmax, 0.02),\n",
" np.arange(ymin, ymax, 0.02))\n",
" p = [posterior_prob([1, x1, x2], c) for c in classes]\n",
" p_diff = p[1] - p[0]\n",
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 17,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/4022158666.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAYAAACc3aoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABPpElEQVR4nO3deXxU5d3///ckk4SEkEBYg0mAyKaiiKCIYMWCVfC2qP1WS91qvXsrYlFxuW+9W1zaitWK3vfvFlBr3VqE3vZG6CKoKJQtkbWCVRZFEtlCCGQjTLbz+2OYZJJMkjnDnJwzh9fz8ZiHzjnXXPnMmdied65zPuMxDMMQAAAAALhInN0FAAAAAEC0EXQAAAAAuA5BBwAAAIDrEHQAAAAAuA5BBwAAAIDrEHQAAAAAuA5BBwAAAIDrEHQAAAAAuI7X7gJORX19vfbv368uXbrI4/HYXQ4AAAAACxmGofLycvXt21dxcW2v2cR00Nm/f7+ys7PtLgMAAABAByosLFRWVlabY2I66HTp0kWS/42mpaXZXA0AAAAAK5WVlSk7O7shB7QlpoNO4HK1tLQ0gg4AAABwmgjnthWaEQAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHduDzr59+3TzzTere/fuSk5O1rnnnquNGzfaXRYAAACAGOa184cfPXpUY8eO1eWXX6733ntPPXv21K5du9StWzc7ywIAAAAQ42wNOr/+9a+VnZ2t1157rWHbgAEDbKwIAAAAgBvYeuna0qVLNWrUKH3/+99Xr169NGLECL3yyiutjvf5fCorK2vyAAAAAIDmbA06X331lebNm6dBgwZp+fLlmjZtmmbMmKE33ngj5PjZs2crPT294ZGdnd3BFQMAAACIBR7DMAy7fnhiYqJGjRqldevWNWybMWOGNmzYoPXr17cY7/P55PP5Gp6XlZUpOztbpaWlSktL65CaAQAAANijrKxM6enpYZ3/27qik5mZqbPPPrvJtrPOOksFBQUhxyclJSktLa3JAwAAAACaszXojB07Vjt27GiybefOnerXr59NFQEAAABwA1uDzv3336+8vDw99dRT2r17txYsWKCXX35Z06dPt7MsAAAAADHO1qBz4YUXavHixXr77bc1bNgw/eIXv9ALL7ygm266yc6yAAAAAMQ4W5sRnCozNyMBAAAAiG0x04wAAAAAAKxA0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQdAAAAAK5D0AEAAADgOgQ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g4AAAAA1yHoAAAAAHAdgg4AAAAA1yHoAAAAAHAdgg4AAAAA1yHoAAAAAHAdgg4AAAAA1yHoAAAAAHAdbyQvqqys1KpVq1RQUKDq6uom+2bMmBGVwgAAAAAgUqaDzpYtWzR58mQdP35clZWVysjIUHFxsVJSUtSrVy+CDgAAAADbmb507f7779c111yjo0ePKjk5WXl5edq7d69Gjhyp3/zmNxEX8vTTT8vj8ei+++6LeA4AAAAAkCIIOlu3btUDDzyguLg4xcfHy+fzKTs7W88884weffTRiIrYsGGDXnrpJZ133nkRvR4AAAAAgpkOOgkJCYqL87+sV69eKigokCSlp6ersLDQdAEVFRW66aab9Morr6hbt26mXw8AAAAAzZkOOiNGjNCGDRskSZdddplmzZqlP/zhD7rvvvs0bNgw0wVMnz5dV199tSZOnNjuWJ/Pp7KysiYPAAAAAGjOdNB56qmnlJmZKUn61a9+pW7dumnatGk6fPiwXnrpJVNzLVy4UJs3b9bs2bPDGj979mylp6c3PLKzs82WDwAAAOA04DEMw4jWZFVVVUpOTg5rbGFhoUaNGqUPPvig4d6c8ePH6/zzz9cLL7wQ8jU+n08+n6/heVlZmbKzs1VaWqq0tLRTrh8AAACAc5WVlSk9PT2s83/TKzqttY+urKzU5MmTw55n06ZNKioq0gUXXCCv1yuv16tVq1bpv//7v+X1elVXV9fiNUlJSUpLS2vyAAAAAIDmTH+Pzl//+ld169ZNTzzxRMO2iooKTZo0ydQ8EyZM0LZt25psu/322zV06FD9+7//u+Lj482WBgAAAACSIgg677//vi699FJ169ZN9913n8rLy3XllVfK6/XqvffeC3ueLl26tGhe0LlzZ3Xv3j2ipgYAAAAAEGA66Jx55platmyZLr/8csXFxentt99WUlKS/vrXv6pz585W1AgAAAAApkTcjGD9+vW64oorNHr
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_decision_boundary_bayes(fig, X_mean, X_std)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Dla porównania: regresja logistyczna na tych samych danych"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 18,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"def powerme(x1,x2,n):\n",
" X = []\n",
" for m in range(n+1):\n",
" for i in range(m+1):\n",
" X.append(np.multiply(np.power(x1,i),np.power(x2,(m-i))))\n",
" return np.hstack(X)\n",
"\n",
"# Funkcja logistyczna\n",
"def safeSigmoid(x, eps=0):\n",
" y = 1.0/(1.0 + np.exp(-x))\n",
" if eps > 0:\n",
" y[y < eps] = eps\n",
" y[y > 1 - eps] = 1 - eps\n",
" return y\n",
"\n",
"# Funkcja hipotezy dla regresji logistycznej\n",
"def h(theta, X, eps=0.0):\n",
" return safeSigmoid(X*theta, eps)\n",
"\n",
"# Funkcja kosztu dla regresji logistycznej\n",
"def J(h,theta,X,y, lamb=0):\n",
" m = len(y)\n",
" f = h(theta, X, eps=10**-7)\n",
" j = -np.sum(np.multiply(y, np.log(f)) + \n",
" np.multiply(1 - y, np.log(1 - f)), axis=0)/m\n",
" if lamb > 0:\n",
" j += lamb/(2*m) * np.sum(np.power(theta[1:],2))\n",
" return j\n",
"\n",
"# Gradient funkcji kosztu\n",
"def dJ(h,theta,X,y,lamb=0):\n",
" g = 1.0/y.shape[0]*(X.T*(h(theta,X)-y))\n",
" if lamb > 0:\n",
" g[1:] += lamb/float(y.shape[0]) * theta[1:] \n",
" return g\n",
"\n",
"# Funkcja klasyfikująca\n",
"def classifyBi(theta, X):\n",
" prob = h(theta, X)\n",
" return prob"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 19,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Przygotowanie danych dla wielomianowej regresji logistycznej\n",
"\n",
"data = np.matrix(data_iris_setosa)\n",
"\n",
"Xpl = powerme(data[:, 1], data[:, 0], n)\n",
"Ypl = np.matrix(data[:, 2]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 20,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Metoda gradientu prostego dla regresji logistycznej\n",
"def GD(h, fJ, fdJ, theta, X, y, alpha=0.01, eps=10**-3, maxSteps=10000):\n",
" errorCurr = fJ(h, theta, X, y)\n",
" errors = [[errorCurr, theta]]\n",
" while True:\n",
" # oblicz nowe theta\n",
" theta = theta - alpha * fdJ(h, theta, X, y)\n",
" # raportuj poziom błędu\n",
" errorCurr, errorPrev = fJ(h, theta, X, y), errorCurr\n",
" # kryteria stopu\n",
" if abs(errorPrev - errorCurr) <= eps:\n",
" break\n",
" if len(errors) > maxSteps:\n",
" break\n",
" errors.append([errorCurr, theta]) \n",
" return theta, errors"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 21,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"theta = [[ 4.01960795]\n",
" [ 3.89499137]\n",
" [ 0.18747599]\n",
" [-1.3524039 ]\n",
" [-2.00123783]\n",
" [-0.87625505]]\n"
]
}
],
"source": [
"# Uruchomienie metody gradientu prostego dla regresji logistycznej\n",
"theta_start = np.matrix(np.zeros(Xpl.shape[1])).reshape(Xpl.shape[1], 1)\n",
"theta, errors = GD(h, J, dJ, theta_start, Xpl, Ypl, \n",
" alpha=0.1, eps=10**-7, maxSteps=100000)\n",
"print(r'theta = {}'.format(theta))"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 22,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wykres granicy klas\n",
"def plot_decision_boundary(fig, theta, Xpl, xmin=0.0, xmax=7.0):\n",
" ax = fig.axes[0]\n",
" xx, yy = np.meshgrid(np.arange(xmin, xmax, 0.02),\n",
" np.arange(xmin, xmax, 0.02))\n",
" l = len(xx.ravel())\n",
" C = powerme(yy.reshape(l, 1), xx.reshape(l, 1), n)\n",
" z = classifyBi(theta, C).reshape(int(np.sqrt(l)), int(np.sqrt(l)))\n",
"\n",
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 23,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/2795780436.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAYAAACc3aoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABZtElEQVR4nO3deXhU5d3/8c8kk4RAFghLSEjCHlBxQVBAULFgFa0Faa1S99rWBeqCS6tPi9r2EYsVrT8F1Fq3VqG1UqitoCJEtsSwKaAQ9oSQhCWQjTBZ5vz+mGeykWVOyOTMnLxf1zWX5Mw9d75zJuD55j7ncxyGYRgCAAAAABsJsboAAAAAAGhrNDoAAAAAbIdGBwAAAIDt0OgAAAAAs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VGRnp09icnByNHDlSn376ac21OePHj9cFF1ygF198sdHXuFwuuVyumq+Li4uVnJysoqIixcTEnHH9AAAAAAJXcXGxYmNjfTr+N72i01R8dFlZma655hqf59m4caMOHz6sCy+8UE6nU06nU2lpaXrppZfkdDpVXV192msiIiIUExNT7wEAAAAADZm+j85//vMfdevWTU8//XTNttLSUk2aNMnUPBMmTNDWrVvrbbvzzjs1dOhQ/fKXv1RoaKjZ0gAAAABAUisanU8++USXXnqpunXrpgcffFAlJSW66qqr5HQ69fHHH/s8T3R09GnhBV26dFH37t1bFWoAAAAAAF6mG52BAwdq2bJluuKKKxQSEqL3339fERER+s9//qMuXbr4o0YAAAA
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xpl, Ypl, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_decision_boundary(fig, theta, Xpl)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 24,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/2795780436.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);\n",
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/4022158666.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAY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d66trdd9+tyy+/XMePH1doaKjS09N15MgRTZw4UX/4wx+6Hcjjjz8um82mu+66q9t9AAAAAIDUjURn+/btuueeexQQEKDAwEA5HA6lpKToiSee0IMPPtitIDIyMvTiiy/qnHPO6dbrAQAAAKAl04lOUFCQAgJcL4uLi1NWVpYkKTo6WtnZ2aYDqKys1NVXX62XX35Z/fv3N/16AAAAAGjLdKIzfvx4ZWRkSJIuuOACLVq0SP/4xz901113aezYsaYDWLBggS677DLNmTOny7YOh0Pl5eWtHgAAAADQlulE59FHH1ViYqIk6X//93/Vv39/3XrrrTp27JhefPFFU30tWbJEW7du1WOPPeZW+8cee0zR0dFNj5SUFLPhAwAAAOgDbIZhGD3VWU1NjUJDQ91qm52drUmTJumjjz5qWpsza9YsjRs3Ts8++2y7r3E4HHI4HE3Py8vLlZKSorKyMkVFRZ12/AAAAAB8V3l5uaKjo926/jc9otNR+eiqqipdeumlbvezZcsWFRYWasKECbLb7bLb7VqzZo3+9Kc/yW63q6Gh4ZTXhISEKCoqqtUDAAAAANoyfR+d//znP+rfv78eeeSRpm2VlZW65JJLTPUze/Zs7dy5s9W2G2+8UWPGjNEvfvELBQYGmg0NAAAAACR1I9H58MMPdf7556t///666667VFFRoYsuukh2u10ffPCB2/1ERkaeUrwgPDxcAwYM6FZRAwAAAABoZDrRGTFihFauXKkLL7xQAQEBeuuttxQSEqL//Oc/Cg8P90SMAAAAAGB
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xpl, Ypl, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_decision_boundary(fig, theta, Xpl)\n",
"plot_decision_boundary_bayes(fig, X_mean, X_std)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Inny przykład"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 25,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Wczytanie danych (gatunki kosaćców)\n",
"\n",
"data_iris = pandas.read_csv('iris.csv')\n",
"data_iris_versicolor = pandas.DataFrame()\n",
"data_iris_versicolor['dł. płatka'] = data_iris['pl'] # \"pl\" oznacza \"petal length\"\n",
"data_iris_versicolor['szer. płatka'] = data_iris['pw'] # \"pw\" oznacza \"petal width\"\n",
"data_iris_versicolor['Iris versicolor?'] = data_iris['Gatunek'].apply(lambda x: 1 if x=='Iris-versicolor' else 0)\n",
"\n",
"m, n_plus_1 = data_iris_versicolor.values.shape\n",
"n = n_plus_1 - 1\n",
"Xn = data_iris_versicolor.values[:, 0:n].reshape(m, n)\n",
"\n",
"X = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
"Y = np.matrix(data_iris_setosa.values[:, 2]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 26,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"liczba przykładów: {0: 100, 1: 50}\n",
"prior probability: {0: 0.6666666666666666, 1: 0.3333333333333333}\n"
]
}
],
"source": [
"classes = [0, 1]\n",
"count = [sum(1 if y == c else 0 for y in Y.T.tolist()[0]) for c in classes]\n",
"prior_prob = [float(count[c]) / float(Y.shape[0]) for c in classes]\n",
"\n",
"print('liczba przykładów: ', {c: count[c] for c in classes})\n",
"print('prior probability:', {c: prior_prob[c] for c in classes})"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 27,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
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ggAACA5+Hw9Ox+rthFZXKuQKI4AAAASXVmZNHx455twVleb58vKnNU2Iotr1SVWqwMAAEhkPp854N2zR8rLk8rLJa/3xPnqamn8eGnfPik/X9q1K/wH5+1sG5GVxNeK1eoAAABgSkuTVq82B8T79pkD4JaZg7YD4rw8M87KgNjOthFZXKuwUBwBAAAkOq/XnCloOzDeuDFwQNx+JsEJbSOyuFZd4rY6AACAZNF2hqBFpAbEdraNyEqya8VtdQAAAOjI65VKSwOPlZZGZkBsZ9uILK5VpyiOAAAAkkV1tTR5cuCxyZM7X73MKW0jsrhWnaI4AgAASAbtH7qvqAj+cL7T2kZkca1CojgCAABIdO0HxOXl0ujRHR/O787A2M62EVlcqy5RHAEAACQyn08qKgq+Gln71cuKisx4J7SNyOJahYXiCAAAIJGlpUnz55sbewZbjaxlYJyfb8ZZ3efIrrYRWVyrsLCUNwAAQDLw+UIPeLs6H6u2EVlJeK1YyhsAAACBuhrw9mRAbGfbiCyuVUgURwAAAAAgiiMAAAAAkERxBAAAAACSKI4AAACcoaulk9uftzPeattW2d1+MqEvI4riCAAAINbKyqThwzvffLO62jxfVmZ/vNW2rbK7/WRCX0YcS3kDAADEks9nDmD37Om4OadkDnDHjzc358zPl7Ztk0aOtCd+yBDz2Icfhtf2rl3WVjez+l2ttp9M6MuwsZQ3AABAvEhLk1avNge4+/aZA9qWmYC2A9y8PDMuM9O++DVrzFe4bVsdbFv9rkk
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 28,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"średnia: [matrix([[1. , 4.906, 1.676]]), matrix([[1. , 1.464, 0.244]])]\n",
"odchylenie standardowe: [matrix([[0. , 0.8214402 , 0.42263933]]), matrix([[0. , 0.17176728, 0.10613199]])]\n"
]
}
],
"source": [
"XY = np.column_stack((X, Y))\n",
"XY_split = [XY[np.where(XY[:,3] == c)[0]] for c in classes]\n",
"X_split = [XY_split[c][:,0:3] for c in classes]\n",
"Y_split = [XY_split[c][:,3] for c in classes]\n",
"\n",
"X_mean = [np.mean(X_split[c], axis=0) for c in classes]\n",
"X_std = [np.std(X_split[c], axis=0) for c in classes]\n",
"print('średnia: ', X_mean) \n",
"print('odchylenie standardowe: ', X_std)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 29,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/1042079336.py:11: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p, levels=np.arange(0.0, 1.0, 0.1),\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAYAAACc3aoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAACaQklEQVR4nOzdd3xTVf8H8E9Gd2lLaQutFFr23kNAlmxRQRQVUXD/RBQUQUUfBRygKIgTHI/geBCVR0BxIFsECpShsimrZbeMtkl3cn5/nOc2SZu0SZv0punn/XrdV9Obk3tPbsrlfHPO+R6NEEKAiIiIiIjIh2jVrgAREREREZG7MdAhIiIiIiKfw0CHiIiIiIh8DgMdIiIiIiLyOQx0iIiIiIjI5zDQISIiIiIin8NAh4iIiIiIfA4DHSIiIiIi8jl6tStQGWazGefOnUOtWrWg0WjUrg4REREREXmQEALZ2dmIi4uDVlt2n021DnTOnTuH+Ph4tatBRERERERVKC0tDfXr1y+zTLUOdGrVqgVAvtGwsDCVa0NERERERJ6UlZWF+Pj44jigLNU60FGGq4WFhTHQISIiIiKqIZyZtsJkBERERERE5HMY6BARERERkc9hoENERERERD6HgQ4REREREfkcBjpERERERORzGOgQEREREZHPYaBDREREREQ+h4EOERERERH5HAY6RERERETkcxjoEBERERGRz2GgQ0REREREPoeBDhERERER+RwGOkRERERE5HNUDXQSEhKg0WhKbRMnTlSzWkREREREVM3p1Tz5rl27YDKZin/fv38/Bg0ahNGjR6tYKyIiIiIiqu5UDXSio6Ntfn/jjTfQuHFj9O3bV6UaERERERGRL1A10LFWUFCAr7/+GlOmTIFGo7FbJj8/H/n5+cW/Z2VlVVX1iIiIiIioGvGaZAQrV67EtWvXcP/99zssM2fOHISHhxdv8fHxVVdBqnGMBUZoZmmgmaWBscCodnWIiEhhNAIajdyMvD8TkX1eE+j8+9//xrBhwxAXF+ewzPTp05GZmVm8paWlVWENiYiIiIiouvCKoWunT5/GunXr8MMPP5RZLiAgAAEBAVVUKyIiIiIiqq68ItBZvHgxYmJiMHz4cLWrQlTMT+eHuQPnFj8mIiIv4ecHzJ1reUxEZIdGCCHUrIDZbEZiYiLGjBmDN954w6XXZmVlITw8HJmZmQgLC/NQDYmIiIiIyBu40v5XfY7OunXrkJqaigcffFDtqhARERERkY9Qfeja4MGDoXKnEpFdJrMJe87vAQB0iu0EnVanco2IiAgAYDIBe+T9GZ06ATren4moNNUDHSJvlVeUh26fdQMAGKYbEOIfonKNiIgIAJCXB3ST92cYDEAI789EVJrqQ9eIiIiIiIjcjYEOERERERH5HAY6RERERETkcxjoEBERERGRz2GgQ0REREREPoeBDhERERER+RymlyZywE/nhxl9ZxQ/JiIiL+HnB8yYYXlMRGSHRlTj1TqzsrIQHh6OzMxMhIWFqV0dIiIiIiLyIFfa/xy6RkREREREPodD14gcMAszDqUfAgC0jG4JrYbfCxAReQWzGTgk789o2RLQ8v5MRKUx0CFyILcwF20WtgEAGKYbEOIfonKNiIgIAJCbC7SR92cYDEAI789EVBq/AiEiIiIiIp/DQIeIiIiIiHwOAx0iIiIiIvI5DHSIiIiIiMjnMNAhIiIiIiKfw0CHiIiIiIh8DtNLEzngp/PD1B5Tix8TEZGX8PMDpk61PCYiskMjhBBqV6KisrKyEB4ejszMTISFhaldHSIiIiIi8iBX2v8cukZERERERD6HQ9eIHDALM1IzUwEADcIbQKvh9wJERF7BbAZS5f0ZDRoAWt6fiag0BjpEDuQW5iLx3UQAgGG6ASH+ISrXiIiIAAC5uUCivD/DYABCeH8motL4FQgREREREfkcBjpERERERORzGOgQEREREZHPYaBDREREREQ+h4EOERERERH5HAY6RERERETkc5hemsgBvVaPx7s8XvyYiIi8hF4PPP645TERkR0aIYRQuxIVlZWVhfDwcGRmZiIsLEzt6hARERERkQe50v7n0DUiIiIiIvI57O8lckAIgYycDABAVHAUNBqNyjUiIiIAgBBAhrw/IyoK4P2ZiOxgoEPkQE5hDmLejgEAGKYbEOIfonKNiIgIAJCTA8TI+zMMBiCE92ciKo1D14iIiIiIyOcw0CEiIiIiIp/DQIeIiIiIiHwOAx0iIiIiIvI5DHSIiIiIiMjnMNAhIiIiIiKfw/TSRA7otXqMbz+++DEREXkJvR4YP97ymIjIDo0QQqhdiYrKyspCeHg4MjMzERYWpnZ1iIiIiIjIg1xp/3PoGhERERER+Rz29xI5IIRATmEOACDYLxgajUblGhEREQBACCBH3p8RHAzw/kxEdrBHh8iBnMIchM4JReic0OKAh4iIvEBODhAaKrcc3p+JyD4GOkRERERE5HMY6BARERERkc9hoENERERERD5H9UDn7NmzuPfee1GnTh0EBQWhbdu2SE5OVrtaRERERERUjamade3q1avo1asX+vfvj19//RXR0dE4duwYateurWa1iIiIiIiomlM10HnzzTcRHx+PxYsXF+9LTExUsU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iIPMiV9j+/BiEiIiIiIp/DQIeIiIiIiHwOAx0iB3IKc5CwIAEJCxKQU5ijdnWIiEiRkwMkJMgth/dnIrKPyQiIHBBC4HTm6eLHRETkJYQATp+2PCYisoM9OkRERERE5HMY6BARERERkc9hoENERERERD6HgQ4REREREfkcBjpERERERORzmHWNyAGNRoNW0a2KHxMRkZfQaIBWrSyPiYjsYKBD5ECwXzAOPH5A7WoQEVFJwcHAAd6fiahsHLpGREREREQ+h4EOERERERH5HAY6RA7kFOag9Uet0fqj1sgpzFG7OkREpMjJAVq3llsO789EZB/n6BA5IITAwfS
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"draw_means(fig, X_mean)\n",
"plot_prob(fig, X_mean, X_std, classes)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 30,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/4022158666.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAYAAACc3aoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABPpElEQVR4nO3deXxU5d3///ckk4SEkEBYg0mAyKaiiKCIYMWCVfC2qP1WS91qvXsrYlFxuW+9W1zaitWK3vfvFlBr3VqE3vZG6CKoKJQtkbWCVRZFEtlCCGQjTLbz+2OYZJJMkjnDnJwzh9fz8ZiHzjnXXPnMmdied65zPuMxDMMQAAAAALhInN0FAAAAAEC0EXQAAAAAuA5BBwAAAIDrEHQAAAAAuA5BBwAAAIDrEHQAAAAAuA5BBwAAAIDrEHQAAAAAuI7X7gJORX19vfbv368uXbrI4/HYXQ4AAAAACxmGofLycvXt21dxcW2v2cR00Nm/f7+ys7PtLgMAAABAByosLFRWVlabY2I66HTp0kWS/42mpaXZXA0AAAAAK5WVlSk7O7shB7QlpoNO4HK1tLQ0gg4AAABwmgjnthWaEQAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHYIOAAAAANch6AAAAABwHduDzr59+3TzzTere/fuSk5O1rnnnquNGzfaXRYAAACAGOa184cfPXpUY8eO1eWXX6733ntPPXv21K5du9StWzc7ywIAAAAQ42wNOr/+9a+VnZ2t1157rWHbgAEDbKwIAAAAgBvYeuna0qVLNWrUKH3/+99Xr169NGLECL3yyiutjvf5fCorK2vyAAAAAIDmbA06X331lebNm6dBgwZp+fLlmjZtmmbM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5dV199tSZOnNjuWJ/Pp7KysiYPAAAAAGjOdNB56qmnlJmZKUn61a9+pW7dumnatGk6fPiwXnrpJVNzLVy4UJs3b9bs2bPDGj979mylp6c3PLKzs82WDwAAAOA04DEMw4jWZFVVVUpOTg5rbGFhoUaNGqUPPvig4d6c8ePH6/zzz9cLL7wQ8jU+n08+n6/heVlZmbKzs1VaWqq0tLRTrh8AAACAc5WVlSk9PT2s83/TKzqttY+urKzU5MmTw55n06ZNKioq0gUXXCCv1yuv16tVq1bpv//7v+X1elVXV9fiNUlJSUpLS2vyAAAAAIDmTH+Pzl//+ld169ZNTzzxRMO2iooKTZo0ydQ8EyZM0LZt25psu/322zV06FD9+7//u+Lj482WBgAAAACSIgg677//vi699FJ169ZN9913n8rLy3XllVfK6/XqvffeC3ueLl26tGhe0LlzZ3Xv3j2ipgYAAAAAEGA66Jx55platmyZLr/8csXFxentt99WUlKS/vrXv6pz585W1AgAAAAApkTcjGD9+vW64oorNHr
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_decision_boundary_bayes(fig, X_mean, X_std)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 31,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Przygotowanie danych dla wielomianowej regresji logistycznej\n",
"\n",
"data = np.matrix(data_iris_versicolor)\n",
"\n",
"Xpl = powerme(data[:, 1], data[:, 0], n)\n",
"Ypl = np.matrix(data[:, 2]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 32,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"theta = [[-10.68893855]\n",
" [ 5.52671372]\n",
" [ 5.83188857]\n",
" [ -0.60754604]\n",
" [ -0.46068339]\n",
" [ -2.83369636]]\n"
2022-12-09 15:06:17 +01:00
]
}
],
"source": [
"# Uruchomienie metody gradientu prostego dla regresji logistycznej\n",
"theta_start = np.matrix(np.zeros(Xpl.shape[1])).reshape(Xpl.shape[1], 1)\n",
"theta, errors = GD(h, J, dJ, theta_start, Xpl, Ypl, \n",
" alpha=0.05, eps=10**-7, maxSteps=100000)\n",
"print(r'theta = {}'.format(theta))"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 33,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/2795780436.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzoAAAHvCAYAAACc3aoBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABjPUlEQVR4nO3deXhU5d3/8c8kM9kXkpBAAmETEJVFBIyIVFTUqlUU2/pDq9baVhH3pa22dasVH61L+1RBq49ba6HVIqgVXEFAg2wRUJGdBMKWfWWyzPn9ccgyWeeETGbm8H5d11wh59w5+eaMwvnkPvf3OAzDMAQAAAAANhIW6AIAAAAAoLsRdAAAAADYDkEHAAAAgO0QdAAAAADYDkEHAAAAgO0QdAAAA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DHAYhmF018Gqq6sVHR3t09i8vDyNHz9eH374YePanClTpujkk0/WM8880+bXuN1uud3uxs/LysqUmZmp0tJSJSQkHHX9AAAAAIJXWVmZEhMTfbr+tzyj01776MrKSl144YU+H2ft2rU6ePCgTjnlFDmdTjmdTi1btkx/+ctf5HQ6VV9f3+prIiMjlZCQ4PUCAAAAgJYsP0fnvffeU1JSkh566KHGbRUVFbrgggssHeecc87Rxo0bvbZdd911GjFihH79618rPDzcamkAAAAAIKkLQeeDDz7Q5MmTlZSUpNtvv13l5eU6//zz5XQ69f777/t8nPj4+FbNC2JjY5WSktKlpgYAAAAA0MBy0DnuuOO0ePFinXXWWQoLC9M///lPRUZG6r333lNsbKw/agQAAAAAS7rcjOCLL77Queeeq6ysLL377rs+NyH
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xpl, Ypl, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_decision_boundary(fig, theta, Xpl)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 34,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/2795780436.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);\n",
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/4022158666.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);\n"
]
},
{
"data": {
2023-01-13 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txud7Njd9xxR7cEBgAAAABdZTrR2bRpky677DJVVFSovLxcKSkpKigoUExMjNLT00l0AAAAAASc6alrd999t6644goVFRUpOjpa2dnZOnDggMaPH6/f//73XQ7kySeflMVi0V133dXlPgAAAABA6kKik5OTo3vvvVdWq1U2m00ul0tZWVl66qmn9MADD3QpiHXr1unll1/WmWee2aXXAwAAAEBTphMdh8Mhq9X7svT0dOXm5kqSEhMTlZeXZzqAsrIyXXvttfrLX/6i5ORk068HAAAAgJZMJzrjxo3TunXrJEnnnXeeHnzwQf3973/XXXfdpVGjRpkOYO7cubr88ss1Y8aMTtu6XC6VlJQ02wAAAACgJdOJzuOPP66MjAxJ0u9+9zslJydrzpw5On78uF5++WVTfc2fP18bN27UE0884VP7J554QomJiQ1bVlaW2fABAAAAnAIshmEY3dVZZWWloqOjfWqbl5enCRMm6OOPP25YmzN9+nSNHTtWzz//fJuvcblccrlcDc9LSkqUlZUlp9OphISEk44fAAAAQPAqKSlRYmKiT9f/pkd02isfXV5erssuu8znfjZs2KBjx47prLPOkt1ul91u1/Lly/XHP/5RdrtdtbW1rV4TGRmphISEZhsAAAAAtGT6PjoffPCBkpOT9cgjjzTsKysr06WXXmqqnwsvvFBbt25ttu+mm27SiBEj9Itf/EI2m81saAAAAAAgqQuJzkcffaRp06YpOTlZd911l0pLS3XJJZfIbrfrww8/9Lmf+Pj4VsULYmNjlZqa2qWiBgAAAABQz3SiM2TIEC1evFjnn3++rFar/vGPfygyMlI
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xpl, Ypl, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_decision_boundary(fig, theta, Xpl)\n",
"plot_decision_boundary_bayes(fig, X_mean, X_std)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Kiedy naiwny Bayes nie działa?"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 35,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Wczytanie danych\n",
"import pandas\n",
"import numpy as np\n",
"\n",
"alldata = pandas.read_csv('bayes_nasty.tsv', sep='\\t')\n",
"data = np.matrix(alldata)\n",
"\n",
"m, n_plus_1 = data.shape\n",
"n = n_plus_1 - 1\n",
"Xn = data[:, 1:]\n",
"\n",
"Xbn = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
"Xbnp = powerme(data[:, 1], data[:, 2], n)\n",
"Ybn = np.matrix(data[:, 0]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 36,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAHvCAYAAABAJN42AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABrZ0lEQVR4nO3dfXxU5Z338e9MQkI0DsgCibRBpZqgWxSFEkMAuW9mBc1uI7gttDYoS3URtBWsD/ReoWK7sT7AvmoNtN6KNttqqrcUo5YKaGoSwoMoFSmQUrSJDwlVlgzBNJNkzv3HNCOTTCYzyTycM/N5v17zUs65ZnLNmZOT85vfdf0um2EYhgAAAAAAEWePdwcAAAAAIFERcAEAAABAlBBwAQAAAECUEHABAAAAQJQQcAEAAABAlBBwAQAAAECUEHABAAAAQJSkxrsDicDj8eijjz7SWWedJZvNFu/uAAAAAIg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ACQkUlOURbY6NUUuK/raSk73MOAACYHgEXEAoqySFc4WZDexbIqK0NXEgDAABYCgEXEAoqySEc4WZDe55DVVXS1Km9C2kQdAEAYDkEXECoqCSHUISbDW1tlZzOwOdQz3PO6SR7CgAYtLaONjW3Nqutoy3eXUkKBFxAOHreABcWEmxFmtULk4SbDc3MlNaskXJzA59D3edcbq63HdlTAMAA1TTUaF7FPGWWZir7kWxllmZqXsU81TbUxrtrCY2ACwgXleSiJ1EKk4SbDZ0/X9q/v+9zKCfHu3/+/Nj0HwC
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xbn, Ybn, xlabel=r'$x_1$', ylabel=r'$x_2$')"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 37,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"liczba przykładów: {0: 69, 1: 30}\n",
"prior probability: {0: 0.696969696969697, 1: 0.30303030303030304}\n"
]
}
],
"source": [
"classes = [0, 1]\n",
"count = [sum(1 if y == c else 0 for y in Ybn.T.tolist()[0]) for c in classes]\n",
"prior_prob = [float(count[c]) / float(Ybn.shape[0]) for c in classes]\n",
"\n",
"print('liczba przykładów: ', {c: count[c] for c in classes})\n",
"print('prior probability:', {c: prior_prob[c] for c in classes})"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 38,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"średnia: [matrix([[1. , 0.03949835, 0.02825019]]), matrix([[1. , 0.09929617, 0.06206227]])]\n",
"odchylenie standardowe: [matrix([[0. , 0.52318432, 0.60106092]]), matrix([[0. , 0.61370281, 0.6081128 ]])]\n"
]
}
],
"source": [
"XY = np.column_stack((Xbn, Ybn))\n",
"XY_split = [XY[np.where(XY[:,3] == c)[0]] for c in classes]\n",
"X_split = [XY_split[c][:,0:3] for c in classes]\n",
"Y_split = [XY_split[c][:,3] for c in classes]\n",
"\n",
"X_mean = [np.mean(X_split[c], axis=0) for c in classes]\n",
"X_std = [np.std(X_split[c], axis=0) for c in classes]\n",
"print('średnia: ', X_mean) \n",
"print('odchylenie standardowe: ', X_std)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 39,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/1042079336.py:11: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p, levels=np.arange(0.0, 1.0, 0.1),\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAHvCAYAAABAJN42AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydd1gUVxfG3116t6CAIHbsvaCx916wa6yxRo1RY6IxGtEYW2yfGnsvETv22CtiRbGLKFIERED6svV+f5ywC4IIuJ37e555Znd2ytnZ2Zn73nPuOQLGGAOHw+FwOBwOh8PhcNSOUNcGcDgcDofD4XA4HI6xwgUXh8PhcDgcDofD4WgILrg4HA6Hw+FwOBwOR0NwwcXhcDgcDofD4XA4GoILLg6Hw+FwOBwOh8PREFxwcTgcDofD4XA4HI6G4IKLw+FwOBwOh8PhcDSEqa4NMAY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E4/yGTASdPAuvXA+fOAQCkQuBiXQfs6+SKo1ahSJapRJa7gzv6Vu2LPtX6oLFbYwgFRl2pgmPocMHFMRZSUmj87ObNgL+/armbGzBuHHm+cqlHqgkYY3gS8wSHnh3C4eeH8fSDyhtnbmKOzhU7Y1CNQejm0e2r/iOFLUPf+5T3cF6e998y+qdoONk6adAi44ELLi3DBRen0PP+PfWabtwIhIdDIQCulwH2dXLDIddExMmTlauWti+NAdUHoH/1/mhQqgFPE8wxHLjgIvg4QuPi6VO6f+/aBcTH0zJTUxrrNXEi0Lw5oIP79IvYFzj49CB8nvrg2YdnyuU2ZjboUbkHBtUYhI4
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xbn, Ybn, xlabel=r'$x_1$', ylabel=r'$x_2$')\n",
"draw_means(fig, X_mean, xmin=-1.0, xmax=1.0, ymin=-1.0, ymax=1.0)\n",
"plot_prob(fig, X_mean, X_std, classes, xmin=-1.0, xmax=1.0, ymin=-1.0, ymax=1.0)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 40,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/4022158666.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0YAAAHvCAYAAABwqM8XAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zL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+3uxwgbiMKC/WTceMkST/Ztk3tjO8GkOMIRgCQZKuam/X83r1ySPr5hAl2lwMk7JbDDlNtcbF2dnbqFw0NdpcDAClFMAKAJPL5fPrhli2SpG+NHq1jyspsrghIXJHDoZ8Fwv1/1NerqbPT5ooAIHUIRgCQRC/s3au3WlpU6nDop+PH210OMGCXVVbqlMGD1e716p7AlEUAyEUEIwBIkm6vV3ds2yZJmjd2rMYUFdlcETBwhmHo3wNdo//ZuVNbOPQVQI7KqmD0yCOP6Nhjj1V5ebnKy8s1bdo0vfTSS3aXBQCSpCeamrTh4EENc7n0w5oau8sBkmbm0KE6f9gwdft8ujMQ/gEg12RVMBo7dqx+9rOfae3atXrvvfd01llnafbs2frkk0/sLg1AnnN7vfppYJnRj8eNU4XLZW9BQJItqK2VJP1u926ta221uRoASL6sCkZf+9rXdOGFF+qII47QxIkTdd9996msrExvv/122Me73W61tLT0+QCAVPhVY6Ma3G4dVliomznMFTno+MGDdUVlpSTpLvYaAchBWRWMgnk8Hj3zzDNqb2/XtGnTwj5mwYIFqqio6Pmorq5Oc5UA8sFBj0f/9vnnkqQ7x49XsdNpc0VAavy0tlYOSUu//FLv8GYjgByTdcHoo48+UllZmYqKijRnzhwtWbJEkydPDvvY+fP
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xbn, Ybn, xlabel=r'$x_1$', ylabel=r'$x_2$')\n",
"plot_decision_boundary_bayes(fig, X_mean, X_std, xmin=-4.0, xmax=4.0, ymin=-4.0, ymax=4.0)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 41,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"theta = [[-0.31582268]\n",
" [ 0.43496774]\n",
" [-0.21840373]\n",
" [-7.88802319]\n",
" [22.73897346]\n",
" [-4.43682364]]\n"
]
}
],
"source": [
"# Uruchomienie metody gradientu prostego dla regresji logistycznej\n",
"theta_start = np.matrix(np.zeros(Xbnp.shape[1])).reshape(Xbnp.shape[1], 1)\n",
"theta, errors = GD(h, J, dJ, theta_start, Xbnp, Ybn, \n",
" alpha=0.05, eps=10**-7, maxSteps=100000)\n",
"print(r'theta = {}'.format(theta))"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 42,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/2795780436.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0YAAAHvCAYAAABwqM8XAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB2UklEQVR4nO3deXhb5Zn//49k2fIiyYkTLwmxIQk7hCUrIZRCoaxtA7RAWcJShhmGtP2W0M5AZ1papp10CgP9lZaUmWmBpoSEQgOlQBughRJCdsISCJANGxIv2SzJi2RJ5/fHsWzZlmzJln0k6/26Ll/U9rF0y06T8/H9PPdjMwzDEAAAAADkMLvVBQAAAACA1QhGAAAAAHIewQgAAABAziMYAQAAAMh5BCMAAAAAOY9gBAAAACDnEYwAAAAA5DyH1QWMpEgkoj179sjtdstms1ldDgAAAIBhZBiGfD6fJk6cKLu9/55QTgWjPXv2qLq62uoy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1LTU02SpKobmUYHAMOJYAQAQIZqerJJkbaIio8tlnuW2+pyAGBUIxgBAJChGh4zp9FVXlspm81mcTUAMLoRjAAAyEDtde069LdDksTZRQAwAghGAABkoIalDZIhlX62VEVHFFldDgCMegQjAAAyjGEYXdPoqm5g6AIAjASCEQAAGca7zqu27W2yl9hV/pVyq8sBgJxAMAIAIMM0LDWHLpRfVi6Hy2FxNQCQG7IqGC1evFizZs2S2+1WRUWFLrnkEn3wwQdWlwUAQNpEghE1rmiUJFVeU2lxNQCQO7I
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xbnp, Ybn, xlabel=r'$x_1$', ylabel=r'$x_2$')\n",
"plot_decision_boundary(fig, theta, Xbnp, xmin=-4.0, xmax=4.0)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 43,
2022-12-09 15:06:17 +01:00
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/4022158666.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(x1, x2, p_diff, levels=[0.0], colors='c', lw=3);\n",
2023-01-13 14:18:12 +01:00
"/tmp/ipykernel_22218/2795780436.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
2022-12-09 15:06:17 +01:00
" plt.contour(xx, yy, z, levels=[0.5], colors='m', lw=3);\n"
]
},
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0YAAAHvCAYAAABwqM8XAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZl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HAOtuKexfL+2f9/PhxHHe7VR4ZEZE6GIyIiNLkyaYm7HQ6UazTcSNXUl3ri60A5KYLt1VX46KiIrhFEd87fFjlkRERqYPBiIgoDU55PPjpsWMAgOX19agyGFQeEeWz7oPdsG+2Axqg8oZKCIKAJydMgE4QsOb0afy5vV3tIRIRpR2DERFRGvzz0aNwBAKYY7XiWyNGqD0cynOn/u8UAKD08lIYa4wAgMlmM35YWwsA+P7hw3AHAqqNj4hIDQxGREQptr6zEy8E9yx6Yvx4aARB7SFRHhN9IpqfawYAjPhW35D+09GjMcpoxPGeHvymoUGN4RERqYbBiIgohQKShO8eOgQA+GZNDWbZbCqPiPLdmffPwNfqg75Cj9Ir+zYAMWu1eGzcOADAbxob2YiBiPIKgxERUQr9b1MTPnG5UKzT4V+5ZxFlgJYXWwAAFddXQKOPPA34WkUFvlBcjB5RxI+OHk338IiIVMNgRESUIp0+H352/DgA4JExY1DOhgukMr/Dj/Y35MYKVTdVRb2PIAj497POggbAq21tWNvZmb4BEhGpiMGIiChF/t/nn6Pd58PZhYW4mw0XKAO0rmqF6BJhmmCC7fzY0zqnWiz4dvDv7PcPHUJAktI1RCIi1TAYERGlwOHubvzXyZMAgMfGjYNew8MtqU/pRldzZw2EAZqAPDJmDIp1OnzicuG55uZ0DI+ISFX8TU1ElAIPHD0KnyT
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xbn, Ybn, xlabel=r'$x_1$', ylabel=r'$x_2$')\n",
"plot_decision_boundary_bayes(fig, X_mean, X_std, xmin=-4.0, xmax=4.0, ymin=-4.0, ymax=4.0)\n",
"plot_decision_boundary(fig, theta, Xbnp, xmin=-4.0, xmax=4.0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"* Naiwny klasyfikator Bayesa nie działa, jeżeli dane nie różnią się ani średnią, ani odchyleniem standardowym."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 9.2. Algorytm $k$ najbliższych sąsiadów"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### KNN intuicja"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Do której kategorii powinien należeć punkt oznaczony gwiazdką?"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 44,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Przydatne importy\n",
"\n",
"import ipywidgets as widgets\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 45,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wczytanie danych (gatunki kosaćców)\n",
"\n",
"data_iris = pandas.read_csv('iris.csv')\n",
"data_iris_setosa = pandas.DataFrame()\n",
"data_iris_setosa['dł. płatka'] = data_iris['pl'] # \"pl\" oznacza \"petal length\"\n",
"data_iris_setosa['szer. płatka'] = data_iris['pw'] # \"pw\" oznacza \"petal width\"\n",
"data_iris_setosa['Iris setosa?'] = data_iris['Gatunek'].apply(lambda x: 1 if x=='Iris-setosa' else 0)\n",
"\n",
"m, n_plus_1 = data_iris_setosa.values.shape\n",
"n = n_plus_1 - 1\n",
"Xn = data_iris_setosa.values[:, 0:n].reshape(m, n)\n",
"\n",
"X = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
"Y = np.matrix(data_iris_setosa.values[:, 2]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 46,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wykres danych (wersja macierzowa)\n",
"def plot_data_for_classification(X, Y, xlabel, ylabel): \n",
" fig = plt.figure(figsize=(16*.6, 9*.6))\n",
" ax = fig.add_subplot(111)\n",
" fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)\n",
" X = X.tolist()\n",
" Y = Y.tolist()\n",
" X1n = [x[1] for x, y in zip(X, Y) if y[0] == 0]\n",
" X1p = [x[1] for x, y in zip(X, Y) if y[0] == 1]\n",
" X2n = [x[2] for x, y in zip(X, Y) if y[0] == 0]\n",
" X2p = [x[2] for x, y in zip(X, Y) if y[0] == 1]\n",
" ax.scatter(X1n, X2n, c='r', marker='x', s=50, label='Dane')\n",
" ax.scatter(X1p, X2p, c='g', marker='o', s=50, label='Dane')\n",
" \n",
" ax.set_xlabel(xlabel)\n",
" ax.set_ylabel(ylabel)\n",
" ax.margins(.05, .05)\n",
" return fig"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 47,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"def plot_new_example(fig, x, y):\n",
" ax = fig.axes[0]\n",
" ax.scatter([x], [y], c='k', marker='*', s=100, label='?')"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 48,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0cAAAHvCAYAAACfaqQpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABXFklEQVR4nO3de3xU1b3///dMJgnRkGis3GSiRAheQEWwMVwUj1gvHA3aU5FvBaRqFaFyUdvaX4+eUlusVezpAwPa46XGo0RrEasW5GIohItcFVRExJJULlrBhGCa2+zfH/skZCaTyexk9syemdfz8ZiHzt6fWfnM2rFdn6y913IZhmEIAAAAAJKcO9YJAAAAAIATUBwBAAAAgCiOAAAAAEASxREAAAAASKI4AgAAAABJFEcAAAAAIIniCAAAAAAkURwBAAAAgCTJE+sEos3n82n//v3q3r27XC5XrNMBAAAAYCPDMHT06FH16dNHbnfouaGkK472798vr9cb6zQAAAAARFFlZaX69u0bMibpiqPu3btLMjsnKysrxtkAAAAAsFN1dbW8Xm9LHRBK0hVHzbfSZWVlURwBAAAASSKcR2pYkAEAAAAARHEEAAAAAJIojgAAAABAEsURAAAAAEiiOAIAAAAASRRHAAAAACCJ4ggAAAAAJFEcAQAAAIAkiiMAAAAAkERxBAAAAACSKI4AAAAQqL6+a+cjzWo+TssfcYPiCAAAAMeVlkqDB0uVlcHPV1aa50tLnZmP0/JHXIlpcTR37lxddNFF6t69u3r06KFx48bp448/DvmZ5557Ti6Xy+/VrVu3KGUMAACQwOrrpQcekHbvlkaPbltgVFaax3fvNuPsnoGxmk9NjbPyR9yJaXG0evVqTZs2TRs2bNDy5cvV0NCg73znOzp27FjIz2VlZenAgQMtr3379kUpYwAAgASWliatWCHl5Ul79/oXGM2Fxd695vkVK8x4J+WTmems/BF3XIZhGLFOotmXX36pHj16aPXq1brkkkuCxjz33HOaOXOmvv766079jOrqamVnZ6uqqkpZWVldyBYAACBBBRYSJSXSxInH35eVSV6vc/NxWv6IKSvjf0c9c1RVVSVJysnJCRlXU1Oj008/XV6vV0VFRfrggw/aja2rq1N1dbXfCwAAACF4vWYB0TwDM2JEbAsLq/k4LX/EDccURz6fTzNnztSIESM0aNCgduMGDhyoZ555RkuWLNELL7wgn8+n4cOH6x//+EfQ+Llz5yo7O7vl5eU/BgAAgI55veaM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ABIdF6vOVPQemC8bp3/gDhwJsEJbSOyuFYd4rY6AACAZNF6hqBZpAbEdraNyEqya8VtdQAAAGjL65VKSvyPlZREZkBsZ9uILK5VuyiOAAAAkkVlpTRxov+xiRPbX73MKW0jsrhW7aI4AgAASAaBD92Xlwd/ON9pbSOyuFYhURwBAAAkusABcVmZNHx424fzOzMwtrNtRBbXqkMURwAAAImsvl4aMyb4amSBq5eNGWPGO6FtRBbXKiwURwAAAIksLU2aM8fc2DPYamTNA+P8fDPO6j5HdrWNyOJahYWlvAEAAJJBfX3oAW9H52PVNiIrCa8VS3kDAADAX0cD3q4MiO1sG5HFtQqJ4ggAAAAARHEEAAAAAJIojgAAAABAEsURAACAM3S0dHLgeTvjrbZtld3tJxP6MqIojgAAAGKttFQaPLj9zTcrK83zpaX2x1tt2yq7208m9GXEsZQ3AABALNXXmwPY3bvbbs4pmQPc0aPNzTnz86UtW6ShQ+2JHzDAPPbJJ+G1vWOHtdXNrH5Xq+0nE/oybCzlDQAAEC/S0qQVK8wB7t695oC2eSag9QA3L8+My8y0L37lSvMVbttWB9tWv2u
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_new_example(fig, 2.8, 0.9)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"* Wydaje się sensownym przyjąć, że punkt oznaczony gwiazdką powinien być czerwony, ponieważ sąsiednie punkty są czerwone. Najbliższe czerwone punkty są położone bliżej niż najbliższe zielone."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Algorytm oparty na tej intuicji nazywamy algorytmem **$k$ najbliższych sąsiadów** (*$k$ nearest neighbors*, KNN)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Idea (KNN dla $k = 1$):\n",
" 1. Dla nowego przykładu $x'$ znajdź najbliższy przykład $x$ ze zbioru uczącego.\n",
" 1. Jego klasa $y$ to szukana klasa $y'$."
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 49,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"from scipy.spatial import Voronoi, voronoi_plot_2d\n",
"\n",
"def plot_voronoi(fig, points):\n",
" ax = fig.axes[0]\n",
" vor = Voronoi(points)\n",
" ax.scatter(vor.vertices[:, 0], vor.vertices[:, 1], s=1)\n",
" \n",
" for simplex in vor.ridge_vertices:\n",
" simplex = np.asarray(simplex)\n",
" if np.all(simplex >= 0):\n",
" ax.plot(vor.vertices[simplex, 0], vor.vertices[simplex, 1],\n",
" color='orange', linewidth=1)\n",
" \n",
" xmin, ymin = points.min(axis=0).tolist()[0]\n",
" xmax, ymax = points.max(axis=0).tolist()[0]\n",
" pad = 0.1\n",
" ax.set_xlim(xmin - pad, xmax + pad)\n",
" ax.set_ylim(ymin - pad, ymax + pad)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 50,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0sAAAHvCAYAAACFVkSnAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydd3hUZfbHP5MeSCEBMgkQSoDQew8giRJEXHvBhsrqrvITV+xlXVSsq65l1UXdVVQ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LTHkjzx1otFSUwL5Z0H6COCmO4I6Ja5NWkDRLVGu3Pt9w+/h4SE+v7WgsW1bbwUhPl3aewhGbjDgOR9EqJZet7XUQGOb687efAJWlIh7RSDFUzlJhoST+RkdHW21XXFxMu3btiI+P54orrmDr1q0W25aUlFBUVFTrpTAYJtM5CXGVt6RwgpYjJT/E6Css7uCPvKV0Pa1oGHMyoJ2vDNfzSSmsu2u6HlZ5ju6PQWAz2PCY685pToFxm6DTX2Hdg5A2Egp3uO78Cs9w8AcoPeZ8mFSHCVLU1pUT19iLoNfTsPlpcZoaoq6jMXy4/g6GIzYZcRyOcPh3eYjo6hC8Kpq0grixjToUzzDOUmVlJVOmTGH48OH07NnTYrsuXbrw4Ycf8v333/Ppp59SWVlJUlISBw4cqLf9iy++SGRk5B+veG95c/sacalSKO10/fdRoWiQoEiI6u+bzlJonMSNG33sYR1khaWunWEd5B/5tn9KVfjGSmA49Hke9n8OBctdd96ApjDgDRj9O5QcgXl9YdvLUFnuuj4U7mXPDGg+FCK7Onee0Dj3TFx7/B1iR8Oym+D0oYbbx8fDzJm1t82cqa+D4YhNRhyHvWTNgLCO8kDRXSR
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X, Y, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_new_example(fig, 2.8, 0.9)\n",
"plot_voronoi(fig, X[:, 1:])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Podział płaszczyzny jak na powyższym wykresie nazywamy **diagramem Woronoja** (*Voronoi diagram*)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"* Taki algorytm wyznacza dość efektowne granice klas, zwłaszcza jak na tak prosty algorytm. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"* Niestety jest bardzo podatny na obserwacje odstające:"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 51,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"X_outliers = np.vstack((X, np.matrix([[1.0, 3.9, 1.7]])))\n",
"Y_outliers = np.vstack((Y, np.matrix([[1]])))"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 52,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0sAAAHvCAYAAACFVkSnAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOydd3hU1daH30klkEICpAChBAg19JpQEiE0u6LYULleP+WKir2jYi/XrqD3KnpRFMGGUoQIQUIndBACBEiAJIQAKRBSz/fHSkwhmUw/k2S/zzNPMufsffba55yZ2evstX/LoGmahkKhUCgUCoVCoVAoquCitwEKhUKhUCgUCoVC4YwoZ0mhUCgUCoVCoVAoakA5SwqFQqFQKBQKhUJRA8pZUigUCoVCoVAoFIoaUM6SQqFQKBQKhUKhUNSAcpYUCoVCoVAoFAqFogaUs6RQKBQKhUKhUCgUNaCcJYVCoVAoFAqFQqGoATe9DXA0paWlnDx5Eh8fHwwGg97mKBQKhUKhUCgUCjuiaRq5ubm0bt0aFxfz5ooanbN08uRJQkND9TZDoVAoFAqFQqFQOJDU1FTatm1rVp1G5yz5+PgAcrJ8fX11tqYRc/gr2PYgXH0EPPz1tkZhT9LiYO31MGIRhMTq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x9fWt8lI4IZ4BEDBQheI1RgIGiFTu7lnqSbiicdDtERnMHrXDAwI3L+j3FsSul8Siy/vDnpclNEvhnByZBy6ekrPGFrS5Ejxb2H7g2iQQor6TfGG7ZtZerroIwrp1NYslOBJLbHLGfphLyvdQki+JaO2Jqyd0uFXu5Qb4XaOrs6RpGtOnT+enn35i1apVdOzY0exjlJSUsHv3bkJCQuxgocKhhMRCepySwW2
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X_outliers, Y_outliers, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
"plot_new_example(fig, 2.8, 0.9)\n",
"plot_voronoi(fig, X_outliers[:, 1:])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"* Pojedyncza obserwacja odstająca dramatycznie zmienia granice klas."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Aby temu zaradzić, użyjemy więcej niż jednego najbliższego sąsiada ($k > 1$)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Algorytm $k$ najbliższych sąsiadów dla problemu klasyfikacji\n",
"\n",
"1. Dany jest zbiór uczący zawierajacy przykłady $(x_i, y_i)$, gdzie: $x_i$ zestaw cech, $y_i$ klasa.\n",
"1. Dany jest przykład testowy $x'$, dla którego chcemy określić klasę.\n",
"1. Oblicz odległość $d(x', x_i)$ dla każdego przykładu $x_i$ ze zbioru uczącego.\n",
"1. Wybierz $k$ przykładów $x_{i_1}, \\ldots, x_{i_k}$, dla których wyliczona odległość jest najmniejsza.\n",
"1. Jako wynik $y'$ zwróć tę spośrod klas $y_{i_1}, \\ldots, y_{i_k}$, która występuje najczęściej."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Algorytm $k$ najbliższych sąsiadów dla problemu klasyfikacji przykład"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 53,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# Odległość euklidesowa\n",
"def euclidean_distance(x1, x2):\n",
" return np.linalg.norm(x1 - x2)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 54,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# Algorytm k najbliższych sąsiadów\n",
"def knn(X, Y, x_new, k, distance=euclidean_distance):\n",
" data = np.concatenate((X, Y), axis=1)\n",
" nearest = sorted(\n",
" data, key=lambda xy:distance(xy[0, :-1], x_new))[:k]\n",
" y_nearest = [xy[0, -1] for xy in nearest]\n",
" return max(y_nearest, key=lambda y:y_nearest.count(y))"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 55,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wykres klas dla KNN\n",
"def plot_knn(fig, X, Y, k, distance=euclidean_distance):\n",
" ax = fig.axes[0]\n",
" x1min, x2min = X.min(axis=0).tolist()[0]\n",
" x1max, x2max = X.max(axis=0).tolist()[0]\n",
" pad1 = (x1max - x1min) / 10\n",
" pad2 = (x2max - x2min) / 10\n",
" step1 = (x1max - x1min) / 50\n",
" step2 = (x2max - x2min) / 50\n",
" x1grid, x2grid = np.meshgrid(\n",
" np.arange(x1min - pad1, x1max + pad1, step1),\n",
" np.arange(x2min - pad2, x2max + pad2, step2))\n",
" z = np.matrix([[knn(X, Y, [x1, x2], k, distance) \n",
" for x1, x2 in zip(x1row, x2row)] \n",
" for x1row, x2row in zip(x1grid, x2grid)])\n",
" plt.contour(x1grid, x2grid, z, levels=[0.5]);"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 56,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Przygotowanie interaktywnego wykresu\n",
"\n",
"slider_k = widgets.IntSlider(min=1, max=10, step=1, value=1, description=r'$k$', width=300)\n",
"\n",
"def interactive_knn_1(k):\n",
" fig = plot_data_for_classification(X_outliers, Y_outliers, xlabel=u'dł. płatka', ylabel=u'szer. płatka')\n",
" plot_voronoi(fig, X_outliers[:, 1:])\n",
" plot_knn(fig, X_outliers[:, 1:], Y_outliers, k)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 57,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-01-13 14:18:12 +01:00
"model_id": "34daa3fb4fee4514afac780d8cbcd791",
2022-12-09 15:06:17 +01:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=1, description='$k$', max=10, min=1), Button(description='Run Interact',…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.interactive_knn_1(k)>"
]
},
2023-01-13 14:18:12 +01:00
"execution_count": 57,
2022-12-09 15:06:17 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"widgets.interact_manual(interactive_knn_1, k=slider_k)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 59,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wczytanie danych (inny przykład)\n",
"\n",
"alldata = pandas.read_csv('classification.tsv', sep='\\t')\n",
"data = np.matrix(alldata)\n",
"\n",
"m, n_plus_1 = data.shape\n",
"n = n_plus_1 - 1\n",
"Xn = data[:, 1:].reshape(m, n)\n",
"\n",
"X2 = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
"Y2 = np.matrix(data[:, 0]).reshape(m, 1)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 60,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2023-01-13 14:18:12 +01:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAHvCAYAAABAJN42AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAA9hAAAPYQGoP6dpAABF60lEQVR4nO3de3wU9b3/8fduLpsgbAIHyJJjUCICUoMglBiCyqPsgWjOaRDbgkUQDoWDolaDF/CnUMEWb0UfWpDWA4hUJcUjKlVTIZrKJYINUhGBiKAEIUGhZLnEbJKd3x95ZHVzT8jsLa/n47EPyHe+M/lMhmH3ne/MdyyGYRgCAAAAALQ7a6ALAAAAAIBwReACAAAAAJMQuAAAAADAJAQuAAAAADAJgQsAAAAATELgAgAAAACTELgAAAAAwCSRgS4gHHg8Hh09elRdunSRxWIJdDkAAAAATGQYhk6fPq3ExERZrU2PYRG42sHRo0eVlJQU6DIAAAAA+FFxcbEuvPDCJvsQuNpBly5dJNX8wO12e4CrAQAAAGAml8ulpKQkbw5oCoGrHdReRmi32wlcAAAAQAfRktuJmDQDAAAAAExC4AIAAAAAkxC4AAAAAMAkBC4AAAAAMAmBCwAAAABMQuACAAAAAJMQuAAAAADAJAQuAAAAADAJgQsAAAAATELgAgAAAACTELgAAAAAwCQELgAAAABt53af3/IwR+ACAAAA0DY5OVJKilRc3PDy4uKa5Tk5/q0riBC4AAAAALSe2y3Nny8VFUmjRtUPXcXFNe1FRTX9OuhIF4ELAAAAQOtFR0ubNknJydLBg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iEwAUAAAAAJiFwAQAAAIBJCFwAAAAAYBICFwAAAACYhMAFAAAAACYhcAEAAACASQhcAAAAAGASAhcABJHyynKVnilVeWV5oEsBAADtICQD19KlS3XxxRcrJiZGqamp2rFjR6N9X3jhBVksFp9XTEyMTx/DMDR//nz16tVLsbGxcjqd+vzzz83eDQDw2nJ4i8bnjFfnxZ3l+L1DnRd31vic8dp6eGugSwMAAOch5AJXTk6OsrOztWDBAu3cuVNXXHGFxo4dq+PHjze6jt1u17Fjx7yvr776ymf5448/rmeeeUbLly/X9u3bdcEFF2js2LH67rvvzN4dANBzHz2na1Zdow1FG+QxPJIkj+HRhqINunrV1Vr+j+UBrhAAALRVyAWuJUuWaMaMGZo2bZoGDhyo5cuXq1OnTlq5cmWj61gsFjkcDu8rISHBu8wwDD399NN68MEHlZWVpUGDBunFF1/U0aNH9frrr/thjwB0ZFsOb9Hst2fLkKEqT5XPsipPlQwZuu2t2xjpAgAgRIVU4HK73SosLJTT6fS2Wa1WOZ1OFRQUNLremTNndNFFFykpKUlZWVnas2ePd9mhQ4dUUlLis824uDilpqY2us2Kigq5XC6fFwC0xZKCJYqwRjTZJ8Iaoac+fMpPFQEAgPYUUoHr22+/VXV1tc8IlSQlJCSopKSkwXX69++vlStX6o033tCf//xneTwejRgxQkeOHJEk73qt2ebixYsVFxfnfSUlJZ3vrgHogMory/XG/jfqjWzVVeWp0vp965lIAwCAEBRSgast0tLSNGXKFA0ePFjXXnutXnvtNfXo0UN//OMf27zNefPmqayszPsqLi5ux4oBdBSuCpf3nq3meAyPXBWMpgMAEGpCKnB1795dERERKi0t9WkvLS2Vw+Fo0TaioqI0ZMgQHThwQJK867VmmzabTXa73ecFAK1lt9lltbTsv2GrxSq7jf9rAAAINSEVuKKjozV06FDl5eV52zwej/Ly8pSWltaibVRXV2v37t3q1auXJKlPnz5yOBw+23S
2022-12-09 15:06:17 +01:00
"text/plain": [
2023-01-13 14:18:12 +01:00
"<Figure size 960x540 with 1 Axes>"
2022-12-09 15:06:17 +01:00
]
},
2023-01-13 14:18:12 +01:00
"metadata": {},
2022-12-09 15:06:17 +01:00
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(X2, Y2, xlabel=r'$x_1$', ylabel=r'$x_2$')"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 61,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Przygotowanie interaktywnego wykresu\n",
"\n",
"slider_k = widgets.IntSlider(min=1, max=10, step=1, value=1, description=r'$k$', width=300)\n",
"\n",
"def interactive_knn_2(k):\n",
" fig = plot_data_for_classification(X2, Y2, xlabel=r'$x_1$', ylabel=r'$x_2$')\n",
" plot_voronoi(fig, X2[:, 1:])\n",
" plot_knn(fig, X2[:, 1:], Y2, k)"
]
},
{
"cell_type": "code",
2023-01-13 14:18:12 +01:00
"execution_count": 62,
2022-12-09 15:06:17 +01:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2023-01-13 14:18:12 +01:00
"model_id": "2bc697f1f57244a9b1c3864e98b21538",
2022-12-09 15:06:17 +01:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=1, description='$k$', max=10, min=1), Button(description='Run Interact',…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.interactive_knn_2(k)>"
]
},
2023-01-13 14:18:12 +01:00
"execution_count": 62,
2022-12-09 15:06:17 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"widgets.interact_manual(interactive_knn_2, k=slider_k)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Algorytm $k$ najbliższych sąsiadów dla problemu regresji\n",
"\n",
"1. Dany jest zbiór uczący zawierajacy przykłady $(x_i, y_i)$, gdzie: $x_i$ zestaw cech, $y_i$ liczba rzeczywista.\n",
"1. Dany jest przykład testowy $x'$, dla którego chcemy określić klasę.\n",
"1. Oblicz odległość $d(x', x_i)$ dla każdego przykładu $x_i$ ze zbioru uczącego.\n",
"1. Wybierz $k$ przykładów $x_{i_1}, \\ldots, x_{i_k}$, dla których wyliczona odległość jest najmniejsza.\n",
"1. Jako wynik $y'$ zwróć średnią liczb $y_{i_1}, \\ldots, y_{i_k}$:\n",
" $$ y' = \\frac{1}{k} \\sum_{j=1}^{k} y_{i_j} $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Wybór $k$\n",
"\n",
"* Wartość $k$ ma duży wpływ na wynik działania algorytmu KNN:\n",
" * Jeżeli $k$ jest zbyt duże, wszystkie nowe przykłady są klasyfikowane jako klasa większościowa.\n",
" * Jeżeli $k$ jest zbyt małe, granice klas są niestabilne, a algorytm jest bardzo podatny na obserwacje odstające.\n",
"* Aby dobrać optymalną wartość $k$, najlepiej użyć zbioru walidacyjnego."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Miary podobieństwa"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Odległość euklidesowa\n",
"$$ d(x, x') = \\sqrt{ \\sum_{i=1}^n \\left( x_i - x'_i \\right) ^2 } $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Dobry wybór w przypadku numerycznych cech.\n",
"* Symetryczna, traktuje wszystkie wymiary jednakowo.\n",
"* Wrażliwa na duże wahania jednej cechy."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Odległość Hamminga\n",
"$$ d(x, x') = \\sum_{i=1}^n \\mathbf{1}_{x_i \\neq x'_i} $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Dobry wybór w przypadku cech zero-jedynkowych.\n",
"* Liczba cech, którymi różnią się dane przykłady."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Odległość Minkowskiego ($p$-norma)\n",
"$$ d(x, x') = \\sqrt[p]{ \\sum_{i=1}^n \\left| x_i - x'_i \\right| ^p } $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Dla $p = 2$ jest to odległość euklidesowa.\n",
"* Dla $p = 1$ jest to odległość taksówkowa.\n",
"* Jeżeli $p \\to \\infty$, to $p$-norma zbliża się do logicznej alternatywy.\n",
"* Jeżeli $p \\to 0$, to $p$-norma zbliża się do logicznej koniunkcji."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### KNN praktyczne porady"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* Co zrobić z remisami?\n",
" * Można wybrać losową klasę.\n",
" * Można wybrać klasę o wyższym prawdopodobieństwie _a priori_.\n",
" * Można wybrać klasę wskazaną przez algorytm 1NN."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* KNN źle radzi sobie z brakującymi wartościami cech (nie można wówczas sensownie wyznaczyć odległości)."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.6"
},
"livereveal": {
"start_slideshow_at": "selected",
"theme": "white"
}
},
"nbformat": 4,
"nbformat_minor": 4
}