2021-03-02 08:32:40 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
2021-04-14 08:03:54 +02:00
"## Uczenie maszynowe – zastosowania\n",
2021-03-02 08:32:40 +01:00
"# 6. 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",
2021-04-14 10:13:55 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
2021-03-02 08:32:40 +01:00
"source": [
"<img style=\"float: right;\" src=\"https://upload.wikimedia.org/wikipedia/commons/d/d4/Thomas_Bayes.gif\">\n",
"\n",
"**Thomas Bayes** (wymowa: /beɪ z/) (1702– 1761) – 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",
2022-04-08 09:24:18 +02:00
"*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ę!"
2021-03-02 08:32:40 +01:00
]
},
{
"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",
2022-04-08 09:24:18 +02:00
"* Klasyfikatory dyskryminatywne wyznaczają granicę klas (*decision boundary*) bezpośrednio.\n",
"* Naiwny klasyfikator bayesowski jest klasyfikatorem generatywnym (ponieważ wyznacza $P( x \\,|\\, y )$).\n",
2021-03-02 08:32:40 +01:00
"* 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",
2022-04-08 09:24:18 +02:00
"* Naiwny klasyfikator bayesowski jest *naiwny*, ponieważ zakłada, że poszczególne cechy są niezależne od siebie:\n",
2021-03-02 08:32:40 +01:00
"$$ 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",
"execution_count": 1,
"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",
"execution_count": 2,
"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",
"execution_count": 3,
"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",
"execution_count": 4,
"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",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlwAAAFkCAYAAAD13eXtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3de5hddX3v8c83Fy5NhqIkNUiIEYj4CAeHTIppC0XwjjZMpkBQT4XWFm21DsE2EM55WvWcGk/a43SqeKFowR4vARkCtVQq4jniaVGTkAARERRSYkAuXphETyaz9/f8sfZ29szsvdea2eu31tp7v1/Ps56Zdfv9vusXH+fLWr+1vubuAgAAQDhz8g4AAACg05FwAQAABEbCBQAAEBgJFwAAQGAkXAAAAIGRcAEAAAQ2L+8AZmrRokW+fPnyvMMAAACYZPv27c+4++J6+9ou4Vq+fLm2bduWdxgAAACTmNmeRvt4pAgAABAYCRcAAEBgJFwAAACBkXABAAAERsIFAAAQGAkXAABAYCRcAAAAgZFwAQAABEbCBQAAEBgJFwAArXCXbrkl+plke6g+sogDsxYs4TKz483sa2b2oJntNrPBOse80sx+ZmY7K8tfhIoHAIAgtm6VBgak9esnkhr3aH1gINqfRR9ZxIFZC1lLcVzSe919h5n1SNpuZl9x9+9MOe5ud39TwDgAAAinv18aHJSGh6P1oaEoyRkejrb392fXR+g4MGvBEi53f0LSE5XfR83sQUnHSZqacAEA0L7MouRGipKbasIzOBhtN8uuj9BxYNbMM3ima2bLJX1d0qnu/lzN9ldKulnSXkn7JP2Zu++uc/5lki6TpGXLlvXt2dOwGDcAAPlwl+bUzNQpl9NPcpL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UiaL+kOSVc02M8jRQBoN8zh6l7M4WpKeczhUnT36jOS/rbJMW/U5Enz34prl4QLAHKWRcmcJCV1kL2syvK0afmfZglXsDlcZnampLsl3S+pXNl8taRllTtrnzAzk/RRSa+X9HNJv+9T5m9NxZfmASBnnqA0i9RayZwkJXUKPJenYyX5t0/j3yWrflJGaR8AAIDAKO0DAACQIxIuAACAwEi4AADp85haeKWSdOWV0XeVapXL9bfPpo82mzLTMsaj0Ei4AADpi6uFd8EF0ubNUl/fRHJVLkfrmzdHtRRb7aMd6+21gvEotkavLxZ14bMQANAG4r6jND7u3tsbrff2Rt/dmrreah8F/VZTMIxH7pR3aZ808ZYiALSJ6t2V4eGJbYOD0tBQ9Ep/9Y7Wzp0T+3t7pe3bJ5dzaaWPbsN45IrPQgAA8uExtfDKZWnu3In1Uil5spW0j27DeOSGz0IAALJXvdtSq3Z+UfUOV63aOV1p9NFtGI/CIuECAKSv9tHW4GCURA0ORuvr10d3sqqPE3t7o/Xe3mg9adIV10e3JRmMR7E1mtxV1IVJ8wDQBuJq4fX3T58gXztxfsOG1vsoaL29YBiP3IlJ8wCATHlMLbw1a6Srr5Y2bZo+32jjxunbZ9NHQevtBcN45I5J8wAAAIExaR4AACBHJFwAAACBkXABACYrlaS1a6Of9bYfOtQ5dRAbxVzdXiq1Hmca15rVeBXl36UTNZpNX9SFtxQBILDqG4SLFkUleNyjn4sWRdtPOqlz3jDcsKH5tVTHopU407jWrMarKP8ubUpN3lLMPYGa6ULCBQCB1SZX1aSrdn1srHPqINaLvXZ9fLz1ONO41qzGqyj/Lm2KhAsAMDO1SVZ1qb3jVZuYVJekyVZV7R/z6pLHH/W4a0kjzqK0UaR+OlCzhIvPQgAA6iuVpHnzJtbHxyfXPeykOohx15JGnEVpo0j9dBg+CwEAmJlSSVqyZPK2JUsmJtJ3Uh3EuGtJI86
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 6,
"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",
"execution_count": 7,
"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",
"execution_count": 8,
"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",
"execution_count": 9,
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
2021-03-02 08:32:40 +01:00
"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",
"execution_count": 10,
"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",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" if sys.path[0] == '':\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3gU5fYH8O8kEBJKaKFDqIYuAQLSlAiIICBIEQveHzYsV6SoKIjl2lAExQJe9ArYG02xIio2LICggNIFAgIh9ARI2T2/Pw7DluxuNtsm5ft5nnmSndmZeXezzB7e97xnDBEBERERERVelNUNICIiIiquGEgRERERBYiBFBEREVGAGEgRERERBYiBFBEREVGAGEgRERERBaiM1Q1wlpCQII0aNbK6GUREREQu1q5dmyEiNdzXF6lAqlGjRlizZo3VzSAiIiJyYRjGbk/rObRHREREFCAGUkREREQBYiBFREREFCAGUkREREQBYiBFREREFCAGUkREREQBYiBFREREFCAGUkREREQBYiBFREREFKCwBVKGYTQ3DGO903LCMIzx4TofERERUaSF7RYxIrIFQDIAGIYRDWAfgCXhOh8RERFRpEVqaK83gB0i4vE+NURERETFUaQCqasAvBOhcxERERFFRNgDKcMwYgBcDuADL9vHGIaxxjCMNYcOHQp3cyjEUhekInVBqtXNICKrpabqQlTKhC1Hykl/AL+JyEFPG0XkZQAvA0BKSopEoD0UQvXj61vdBCIqCurzWkClUyQC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PxOZJ/
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 12,
"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",
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
2021-03-02 08:32:40 +01:00
"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",
"execution_count": 13,
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
2021-03-02 08:32:40 +01:00
"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",
"execution_count": 14,
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
2021-03-02 08:32:40 +01:00
"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",
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
2021-03-02 08:32:40 +01:00
"source": [
"Zobaczmy, jak to wygląda na wykresie. Narysujemy w tym celu granicę między klasą 1 a 0:"
]
},
{
"cell_type": "code",
"execution_count": 15,
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
2021-03-02 08:32:40 +01:00
"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",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" \n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3de5xcdX3/8fdnb9lkd8NmScgSQi4QSERKQtgi5dYCigg2iYCgVSuWlrY/fiSAEoyXn9ZKsdESolJbGhUvVNFcu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8yYMYczdgAAgJLKp9jcJMndf2Bmz0n6nqQJh3MSd99tZj+XdJGkZw93kAAAAOUonxmpv03+4u7PSTpb0txcO5nZGDNrTfw+XNLbJa0NOE4AAICyk0+QOs7MWiQp8WiYu5TfrNLRkh4zs2ck/VbxGqkfBR0oAABAucnn1t6n3f2HZna2pHdK+pKkr0l6W7adEo+SObXwIQIAAJSnfGakoomfl0j6mrvfJ6khvCEBAABUhnyC1Btm9p+SrpD0kJkNy3M/AACAqpZPILpC0iOSLnL33ZLaJN0U6qgAAAAqQD6PiDkgaXnK8ibx0GIAAABu0QEAAARFkAIAAAiIIAUAABAQQQoAACAgghQAAEBABCkAAICACFIAAAABEaQAAAACIkgBAFBK7tKKFfGf+awf7OPHYuGev8oRpAAAKKV775UuvVS64Ya+0OIeX7700vj2MI+/YEG4569yOR8RAwAAQjRnjjRvnrR4cXx50aJ
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 17,
"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",
"execution_count": 18,
"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",
"execution_count": 19,
"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",
"execution_count": 20,
"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",
"execution_count": 21,
"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",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" # Remove the CWD from sys.path while we load stuff.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJ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Jukp83s/9y9Mod4AQAACkY2d+1FzOxbko4kkigzW5npOI85FH/bLf7yXIIFAAAoJNnUSK2Lf+6vZjYwvi2rR8bEn9W3WtIeSU+4+7Ikn7nBzFaY2Yq9e/dmGzcAAEDeZZNINbv7bYq1QVhiZlOU5cySu0fcfZKkUZLON7NzknzmAXef6u5ThwwZcjKxAwAA5FU2xeYmSe7+OzNbJ+k3ksaczEnc/YCZPSnpSklrTzZIAACAQpTNjNQnE7+4+zpJF0u6KdNBZjbEzPrHf+8l6QpJGwLGCQAAUHCySaRON7O+khR/NMwvlN2s0nBJFWb2gqTlitVI/SlooAAAAIUmm6W9/+fuvzeziyW9U9J3JP1Q0gXpDoo/Sua83EMEAAAoTNnMSEXiP98t6Yfu/qik7uGFBAAAUByySaRqzOzHkq6T9JiZ9cjyOAAAgE4tm4ToOkmPS7rS3Q9IGijp1lCjAgAAKALZPCLmiKSHW73fKR5aDAAAwBIdAABAUCRSAAAAAZFIAQAABEQiBQAAEBCJFAAAQEAkUgAAAAGRSAEAAAREIgUAABAQiRQAAPnkLj3ySOxnNtvbe/xoNNzzd3IkUgAA5NOiRdI110i33NKStLjH3l9zTWx/mOPfeWe45+/kMj4iBgAAhGjWLGn
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" # Remove the CWD from sys.path while we load stuff.\n",
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" \n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deZxVdf0/8Nd7NmaHGQYYGFbZXEgQCBHNUksN/QJaamZ9tSyqnwlK4lK2fMuyzCQsW4z2rFQ2K02zGhCFQRhAFoFhHZgZhtmYfblz73n//rj3Ogt349x75tzl9Xw87mO459zzOe97ROft5/M+7yOqCiIiIiI6d0l2B0BEREQUq5hIEREREZnERIqIiIjIJCZSRERERCYxkSIiIiIyiYkUERERkUkpdgfQW0FBgY4fP97uMIiIiIj6KC0trVPVYf23R1UiNX78eGzfvt3uMIiIiIj6EJFyX9u5tEdERERkEhMpIiIiIpOYSBERERGZxESKiIiIyCQmUkREREQmMZEiIiIiMomJFBEREZFJTKSIiIiITGIiRURERGQSEykiIiIik5hIEREREZnERIqIiIjIJCZSRERERCYxkSIiIiIyiYkUERERkUlMpIiIiIhMYiJFREREZBITKSIiIiKTmEgRERERmcREioiIiMgkJlJEREREJjGRIiIiIjKJiRQRERGRSUykiIiIiExiIkVERERkEhMpIiIiIpOYSBERERGZxESKiIiIyCQmUkREREQmMZEiIiIiMomJFBEREZFJTKSIiIiITGIiRURERGQSEykiIiIik5hIEREREZnERIqIiIjIJCZSRERERCYxkSIiIiIyydJESkSGiMhqETkgIvtF5DIrz0dEREQ0kFIsHn8lgFdV9eMikgYg0+LzEREREQ0YyxIpEckFcCWAuwBAVR0AHFadj4iIiGigWbm0dx6AWgC/FZGdIrJKRLL6f0hEFovIdhHZXltba2E4RERERJFlZSKVAmAmgJ+r6iUA2gA83P9Dqvqsqs5W1dnDhg2zMBwiIiKiyLIykaoAUKGqWz3vV8OdWBERERHFBcsSKVWtBnBSRKZ6Nl0D4F2rzkdEREQ00Ky+a+9eAM957tg7CuAzFp+PiIiIaMBYmkip6i4As608BxEREZFd2NmciIiIyCQmUkREREQmMZEiIiIiMomJFBEREZFJTKSIiIiITGIiRURERGQSEykiIiIik5hIEREREZnERIqIiIjIJCZSRERERCYxkSIiIiIyiYkUERERkUlMpIiIiIhMYiJFREREZBITKSIiIiKTmEgRERERmcREioiIiMgkJlJEREREJjGRIiIiIjKJiRQRERGRSUykiIiIiExiIkVERERkEhMpIiIiIpOYSBERERGZxESKiIiIyCQmUkREREQmMZEiIiIiMomJFBEREZFJTKSIiIiITGIiRURERGQSEykiIiIik5hIEREREZnERIqIiIjIJCZSRERERCYxkSIiIiIyKcXKwUXkOIAWAC4ATlWdbeX5iIiIiAaSpYmUx1WqWjcA5yEiIiIaUFzaIyIiIjLJ6kRKAfxLREpFZLHF5yIiIiIaUFYv7V2uqlUiMhzA6yJyQFXf6P0BT4K1GADGjh1rcThEREREkWPpjJSqVnl+1gBYB2COj888q6qzVXX2sGHDrAyHiIiIKKIsS6REJEtEcrx/BnAtgL1WnY+IiIhooFm5tDcCwDoR8Z7nz6r6qoXnIyIiIhpQliVSqnoUwHSrxiciIiKyG9sfEBEREZnERIqIiIjIJCZSRERERCYxkSIiIiIyiYkUERERkUlMpIiIiIhMYiJFREREZBITKSIiIiKTmEgRERERmcREioiIiMgkJlJEREREJjGRIiIiIjKJiRQRERGRSUykiIiIiExiIkVERERkEhMpIiIiIpOYSBERERGZxESKiIiIyCQmUkREREQmMZEiIiIiMomJFBEREZFJTKSIiIiITGIiRURERGQSEykiIiIik5hIEREREZnERIqIiIjIJCZSRERERCYxkSIiIiIyiYkUERERkUlMpIiIiIhMYiJFREREZBITKSIiIiKTmEgRERERmcREioiIiMgkJlJEREREJqWE+kERGQ4g3fteVU9YEhERERFRjAg6IyUiC0TkEIBjADYCOA7gn6GeQESSRWSniPzDdJREREREUSiUpb3vAJgLoExVJwC4BsBb53COpQD2m4iNiIiIKK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IiIiIhiQ8BESlVdAGaJiAxQPEREREQxI5SGnDsBvCQiLwJo825U1bWWRUVEREQUA0JJpPIB1AO4utc2BcBEioiIiBJaKJ3NPzMQgRARERHFmqB37YnIFBH5j4js9by/WEQetT40IiIiougWSvuDXwF4BEA3AKjqbgCfsDIoIiIiolgQSiKVqapv99vmtCIYIiIiolgSSiJVJyIT4S4wh4h8HMApS6MiIiIiigGh3LV3D4BnAZwvIpUAjgH4VLCDRCQdwBsABnnOs1pVvxlGrERERERRJZS79o4C+LCIZAFIUtWWEMfuAnC1qraKSCqAN0Xkn6paEka8RERERFEjlLv2XCLyfQDt3iRKRHYEO07dWj1vUz0vDSdYIiIiomgSSo3UPs/n/iUi+Z5tIT0yxvOsvl0AagC8rqpbfXxmsYhsF5HttbW1ocZNREREZLtQEimnqj4IdxuETSIyCyHOLKmqS1VnABgNYI6ITPPxmWdVdbaqzh42bNi5xE5ERERkq1CKzQUAVPUFEdkH4C8Axp7LSVS1UUQ2ALgewN5zDZKIiIgoGoUyI/U57x9UdR+AKwAsCXaQiAwTkSGeP2cA+DCAAybjJCIiIoo6oSRS54lIDgB4Hg3zO4Q2qzQSQLGI7AawDe4aqX+YDZSIiIgo2oSytPd1VX1RRK4AcB2AJwH8HMClgQ7yPErmkvBDJCIiIopOocxIuTw/bwDwc1V9CUCadSERERE
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 24,
"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",
"execution_count": 25,
"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",
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlwAAAFkCAYAAAD13eXtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3de5hddX3v8c83Fy5NhqIkNUiIEYj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AA8uExtfDKZWnu3In1Uil5spW0j27DeOSGz0IAALJXvdtSq3Z+UfUOV63aOV1p9NFtGI/CIuECAKSv9tHW4GCURA0ORuvr10d3sqqPE3t7o/Xe3mg9adIV10e3JRmMR7E1mtxV1IVJ8wDQBuJq4fX3T58gXztxfsOG1vsoaL29YBiP3IlJ8wCATHlMLbw1a6Srr5Y2bZo+32jjxunbZ9NHQevtBcN45I5J8wAAAIExaR4AACBHJFwAAACBkXABACYrlaS1a6Of9bYfOtQ5dRAbxVzdXiq1Hmca15rVeBXl36UTNZpNX9SFtxQBILDqG4SLFkUleNyjn4sWRdtPOqlz3jDcsKH5tVTHopU407jWrMarKP8ubUpN3lLMPYGa6ULCBQCB1SZX1aSrdn1srHPqINaLvXZ9fLz1ONO41qzGqyj/Lm2KhAsAMDO1SVZ1qb3jVZuYVJekyVZV7R/z6pLHH/W4a0kjzqK0UaR+OlCzhIvPQgAA6iuVpHnzJtbHxyfXPeykOohx15JGnEVpo0j9dBg+CwEAmJlSSVqyZPK2JUsmJtJ3Uh3EuGtJI86
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 27,
2022-04-08 09:24:18 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
2021-03-02 08:32:40 +01:00
"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",
"execution_count": 28,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" if sys.path[0] == '':\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3gU5fYH8O8kEBJKaKFDqIYuAQLSlAiIICBIEQveHzYsV6SoKIjl2lAExQJe9ArYG02xIio2LICggNIFAgIh9ARI2T2/Pw7DluxuNtsm5ft5nnmSndmZeXezzB7e97xnDBEBERERERVelNUNICIiIiquGEgRERERBYiBFBEREVGAGEgRERERBYiBFBEREVGAGEgRERERBaiM1Q1wlpCQII0aNbK6GUREREQu1q5dmyEiNdzXF6lAqlGjRlizZo3VzSAiIiJyYRjGbk/rObRHREREFCAGUkREREQBYiBFREREFCAGUkREREQBYiBFREREFCAGUkREREQBYiBFREREFCAGUkREREQBYiBFREREFKCwBVKGYTQ3DGO903LCMIzx4TofERERUaSF7RYxIrIFQDIAGIYRDWAfgCXhOh8RERFRpEVqaK83gB0i4vE+NURERETFUaQCqasAvBOhcxERERFFRNgDKcMwYgBcDuADL9vHGIaxxjCMNYcOHQp3cyjEUhekInVBqtXNICKrpabqQlTKhC1Hykl/AL+JyEFPG0XkZQAvA0BKSopEoD0UQvXj61vdBCIqCur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WDl6paXtoPxOZJ/
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" \n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3de5xcdX3/8fdnb9lkd8NmScgSQi4QSERKQtgi5dYCigg2iYCgVSuWlrY/fiSAEoyXn9ZKsdESolJbGhUvVNFcuAgFUVHQGjGBiFwCREgEcr9vLnuZmc/vj5lhZzdzy5k9c9vX8/GYx+45Z8453znwYN98z2c+x9xdAAAAOHw1pR4AAABApSJIAQAABESQAgAACIggBQAAEBBBCgAAICCCFAAAQEB1pR5AqtGjR/ukSZNKPQwAAIB+Vq9evd3dxwxcX1ZBatKkSVq1alWphwEAANCPmW1It55bewAAAAERpAAAAAIiSAEAAAREkAIAAAiIIAUAABAQQQoAACAgghQAAEBABCkAAICACFIAAAABEaQAAAACIkgBAAAERJACAAAIiCAFAAAQEEEKAAAgIIIUAABAQAQpAACAgAhSAAAAARGkAAAAAiJIAQAABESQAgAACIggBQAAEBBBCgAAICCCFAAAQEAEKQAAgIAIUgAAAAERpAAAAAIiSAEAAAREkAIAA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AAKKV775UuvVS64Ya+0OIeX7700vj2MI+/YEG4569yOR8RAwAAQjRnjjRvnrR4cXx50aJ
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 30,
"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",
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"theta = [[-10.68923095]\n",
" [ 5.52649967]\n",
" [ 5.83316957]\n",
" [ -0.60707243]\n",
" [ -0.46353729]\n",
" [ -2.82974456]]\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.05, eps=10**-7, maxSteps=100000)\n",
"print(r'theta = {}'.format(theta))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" # Remove the CWD from sys.path while we load stuff.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQ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VBm5TGIiIiIYoWdzYmIiIhMYpAiIiIiMolBioiIiMgkBikiIiIikxikiIiIiExikCIiIiIyiUGKiIiIyCQGKSIiIiKTGKSIiIiITGKQIiIiIjKJQYqIiIjIJAYpIiIiIpMYpIiIiIhMYpAiIiIiMolBioiIiMgkBikiIiIikxikiIiIiExikCIiIiIyiUGKiIiIyCQGKSIiIiKTGKSIiIiITGKQIiIiIjKJQYqIiIjIJAYpIiIiIpMYpIiIiIhMYpAiIiIiMolBioiIiMgkBikiIiIikxikiIiIiExikCIiIiIyiUGKiIiIyCQGKSIiIiKTGKSIiIiITGKQIiIiIjKJQYqIiIjIJIeVOxeRQwAaALgBuFS1zMrjEREREfUkS4OU1zWqWt0DxyEiIiLqUby0R0RERGSS1UFKAbwuIuUico/FxyIiIiLqUVZf2pukqhUiMgDAGyLysaqu83+DN2DdAwBDhw61eDhERERE3cfSM1KqWuF9PQFgBYBLA7znGVUtU9WygoICK4dDRERE1K0sC1IikiEiWb6fAVwP4COrjkdERETU06y8tFcIYIWI+I7zf6r6moXHIyIiIupRlgUpVT0A4CKr9k9EREQUa2x/QERERGQSgxQRERGRSQxSRERERCYxSBERERGZxCBFREREZBKDFBEREZFJDFJEREREJjFIEREREZnEIEVERERkEoMUERERkUkMUkREREQmMUgRERERmcQgRURERGQSgxQRERGRSQxSRERERCYxSBERERGZxCBFREREZBKDFBEREZFJDFJEREREJjFIEREREZnEIEVERERkEoMUERERkUkMUkREREQmMUgRERERmcQgRURERGQSgxQRERGRSQxSRERERCYxSBERERGZxCBFREREZBKDFBEREZFJDFJEREREJjFIEREREZnEIEVERERkEoMUERERkUkMUkREREQmOSJ9o4gMAJDqm1fVTywZEREREVGCCHtGSkSmisheAAcBvA3gEIC/R3oAEbGLyBYR+ZvpURIRERHFoUgu7T0KYCKAPap6LoBrAbx3FseYCWCXibERERERxbVIglS7qtYAsImITVXXABgfyc5FpBjATQCejWKMRERERHEpkhqpOhHJBLAOwIsicgKAK8L9/wLAHABZJsdHREREF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LpbH5XTwyEiIiIKNGEvWtPREaJyFsi8pF3fpyI/Lv1QyMiIiKKb5G0P/g9gPkA2gFAVbcD+JKVgyIiIiJKBJEEqXRV3XjGMpcVgyEiIiJKJJEEqWoRGQGjwBwicgeASktHRURERJQAIrlr7z4AzwAYLSKfAjgI4KvhNhKRVADrAKR4j7NUVX8cxViJiIiI4kokd+0dAHCdiGQAsKlqQ4T7bgPwWVVtFJEkAO+KyN9V9f0oxktEREQUNyK5a88tIk8AaPaFKBHZHG47NTR6Z5O8k0YzWCIiIqJ4EkmN1A7v+14XkTzvsogeGeN9Vt9WACcAvKGqGwK85x4R2SQim6qqqiIdNxEREVHMRRKkXKo6B0YbhHdEpBQRnllSVbeqjgdQDOBSEbkgwHueUdUyVS0rKCg4m7ETERERxVQkxeYCAKr6FxHZAeDPAIaezUFUtU5E1gK4EcBHZztIIiIiongUyRmpb/l+UNUdACYDuD/cRiJSICI53p/TAFwH4GOT4yQiIiKKO5EEqeEikgUA3kfDPI/IzioNBLBGRLYD+ABGjdTfzA6UiIiIKN5EcmnvEVV9WUQmA7gBwFMAfgvgslAbeR8lc3H0QyQiIiKKT5GckXJ7X28C8FtVXQUg2bohERERESWGSILUpyLyOwB
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" # Remove the CWD from sys.path while we load stuff.\n",
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" \n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAFkCAYAAADrFNVeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeZwU9Zk/8M+3u+fsuZlhZhguAQFBAR0EFG9dNcQAXkGTbBJX1+xqIopRxMTNZrOKaxIJyWo2xiPR+IsXhyai0ZhBPEBkOOUYbhiYg7lneq6e7np+f1Q303P0YXXXdPfM5/161aunqrqqni5x+plvPfWUEhEQERER0ZdniXYARERERPGKiRQRERGRQUykiIiIiAxiIkVERERkEBMpIiIiIoOYSBEREREZZIt2AL5yc3Nl7Nix0Q6DiIiIqIfS0tJaEcnrvTymEqmxY8diy5Yt0Q6DiIiIqAel1LH+lvPSHhEREZFBTKSIiIiIDGIiRURERGQQEykiIiIig5hIERERERnERIqIiIjIICZSRERERAYxkSIiIiIyiIkUERERkUFMpIiIiIgMYiJFREREZBATKSIiIiKDmEgRERERGcREioiIiMggJlJEREREBjGRIiIiIjKIiRQRERGRQUykiIiIiAxiIkVERERkEBMpIiIiIoOYSBEREREZxESKiIiIyCAmUkREREQGMZEiIiIiMoiJFBEREZFBTKSIiIiIDGIiRURERGQQEykiIiIig5hIERERERnERIqIiIjIICZSRERERAYxkSIiIiIyiIkUERERkUFMpIiIiIgMYiJFREREZBATKSIiIiKDmEgRERERGcREioiIiMggUxMppVSWUuoNpdQ+pdRepdQFZh6PiIiIaCDZTN7/SgDvishNSqlEAKkmH4+IiIhowJiWSCmlMgBcAuC7ACAiTgBOs45HRERENNDMvLQ3DkANgBeUUtuUUs8qpey936SUulMptUUptaWmpsbEcIiIiIgiy8xEygbgPAC/FZFzAbQCeKj3m0TkGRGZKSIz8/LyTAyHiIiIKLLMTKROADghIp955t+AnlgRERERDQqmJVIiUgWgXCk1ybPoSgB7zDoeERER0UAz+669HwB42XPH3mEAt5l8PCIiIqIBY2oiJSLbAcw08xhERERE0cLO5kREREQGMZEiIiIiMoiJFBEREZFBTKSIiIiIDGIiRURERGQQEykiIiIig5hIERERERnERIqIiIjIICZSRERERAYxkSIiIiIyiIkUERERkUFMpIiIiIgMYiJFREREZBATKSIiIiKDmEgRERERGcREioiIiMggJlJEREREBjGRIiIiIjKIiRQRERGRQUykiIiIiAxiIkVERERkEBMpIiIiIoOYSBEREREZxESKiIiIyCAmUkREREQGMZEiIiIiMoiJFBEREZFBTKSIiIiIDGIiRURERGQQEykiIiIig5hIERERERnERIqIiIjIICZSRERERAYxkSIiIiIyiIkUERERkUE2M3eulDoKoAWAG4BLRGaaeTwiIiKigWRqIuVxuYjUDsBxiIiIiAYUL+0RERERGWR2IiUA3lNKlSql7jT5WEREREQDyuxLe3NFpEIpNRzA+0qpfSKywfcNngTrTgAYPXq0yeEQERERRY6pI1IiUuF5PQVgDYBZ/bznGRGZKSIz8/LyzAyHiIiIKKJMS6SUUnalVLr3ZwBXA/jCrOMRERERDTQzL+3lA1ijlPIe5/+JyLsmHo+IiIhoQJmWSInIYQDTzdo/ERERUbSx/QERERGRQUykiIiIiAxiIkVERERkEBMpIiIiIoOYSBEREREZxESKiIiIyCAmUkREREQGMZEiIiIiMoiJFBEREZFBTKSIiIiIDGIiRURERGQQEykiIiIig5hIERERERnERIqIiIjIICZSRERERAYxkSIiIiIyiIkUERERkUFMpIiIiIgMYiJFREREZBATKSIiIiKDmEgRERERGcREioiIiMggJlJEREREBjGRIiIiIjKIiRQRERGRQUykiIiIiAxiIkVERERkEBMpIiIiIoOYSBEREREZxESKiIiIyCAmUkREREQGMZEiIiIiMoiJFBEREZFBTKSIiIiIDGIiRURERGSQLdQ3KqWGA0j2zovIcVMiIiIiIooTQUeklFLzlVIHABwB8CGAowDeCfUASimrUmqbUuqvhqMkIiIiikGhXNr7GYA5APaLyBkArgTwyZc4xmIAew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p9fPey0Wk1LyQiIiIiOJDwERKRNwAipVSaoDiISIiIooboTTk3AbgTaXU6wBavQtFZLVpURERERHFgVASqRwAdQCu8FkmAJhIERER0ZAWSmfz2wYiECIiIqJ4E/SuPaXURKXUB0qpLzzz05RSPzY/NCIiIqLYFkr7g98DWAagCwBEZCeAW8wMioiIiCgehJJIpYrI5l7LXGYEQ0RERBRPQkmkapVS46EXmEMpdROASlOjIiIiIooDody1dzeAZwBMVkqdBHAEwLeCbaSUSgawAUCS5zhviMhPwoiViIiIKKaEctfeYQBXKaXsACwi0hLivjsBXCEiDqVUAoCPlVLviMimMOIlIiIiihmh3LXnVko9DqDNm0QppbYG2050Ds9sgmeScIIlIiIiiiWh1Ejt9rzvPaVUjmdZSI+M8TyrbzuAUwDeF5HP+nnPnUqpLUqpLTU1NaHGTURERBR1oSRSLhF5EHobhI+UUsUIcWRJRNwiMgPASACzlFJn9/OeZ0RkpojMzMvL+zKxExEREUVVKMXmCgBE5DWl1G4AfwYw+sscREQalVLrAVwL4IsvGyQRERFRLAplROoO7w8ishvARQDuCbaRUipPKZXl+TkFwFUA9hmMk4iIiCjmhJJIjVNKpQOA59Ewf0Boo0qFAEqUUjsBfA69RuqvRgMlIiIiijWhXNp7REReV0pdBOAaAL8A8FsAswN
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 34,
"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",
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmwAAAFmCAYAAADQ5sbeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3df5BcZ33v+c93jEZUWp1gCQFmYGKzms3GElvGmnWSi2qHEANi/rAHAxkTUzF7xXp9E64lpBskimygnGsgvlWS5brUTewJAXa1MARGwlkmVoT5qSWwjLQGJCgzg3MtfMfBjgShNcDM2P3dP8450lFP96hnuvv86verqqu7n3O65zn9Y863n+f7PI+5uwAAAJBdPWlXAAAAAMsjYAMAAMg4AjYAAICMI2ADAADIOAI2AACAjCNgAwAAyLjnpV2BNLzwhS/0q6++Ou1qAAAAXOLEiRP/4u4ba8u7MmC7+uqrNTU1lXY1AAAALmFmT9Qrp0sUAAAg4wjYAAAAMo6ADQAAIOMI2AAAADKOgA0AACDjCNgAAAAyjoANAAAg4wjYAAAAMo6ADQAAIOMyEbCZ2UfN7GkzO9Vgu5nZ/WY2Y2bfMbPrY9tuN7Pp8HJ7crUGAABIRiYCNkkfk7R9me1vlDQQXu6Q9F8kyczWS3q/pN+SdIOk95vZlR2tKYCVc5cOHw6umynPi04cV1FfKwAtyUTA5u5flXRumV1ulvQ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iq9XOEYbmrTZgz8pkxW1q6WtptF0bAkYABGxAfmVl6ggslZWW2Ta
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"fig = plot_data_for_classification(Xbn, Ybn, xlabel=r'$x_1$', ylabel=r'$x_2$')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"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",
"execution_count": 37,
"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",
"execution_count": 38,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" if sys.path[0] == '':\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmwAAAFmCAYAAADQ5sbeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOydd1gUVxfG3106CwgIFhDEiog1orHFEk0sUaNGo7HXGM2nRo0txoi9YIm9kaiJvaNGjRW7RqyAiChFBKVJWfqW8/1x6UUpWwa4v+e5z+zOzJ05Mzs78865554rIiJwOBwOh8PhcISLWNsGcDgcDofD4XA+DBdsHA6Hw+FwOAKHCzYOh8PhcDgcgcMFG4fD4XA4HI7A4YKNw+FwOBwOR+BwwcbhcDgcDocjcHS1bYA2sLKyIgcHB22bweFwOBwOh5OLBw8eRBORdd75FVKwOTg4wMvLS9tmcDgcDofD4eRCJBKFFDSfN4lyOBwOh8PhCBwu2DgcDofD4XAEDhdsHA6Hw+FwOAKHCzYOh8PhcDgcgcMFG4fD4XA4HI7A4YKNw+FwOBwOR+BwwcbhcDgcDocjcLhg43A4HA6HwxE4XLBxOBwOh8PhCBwu2DgcDofD4XAEjiAEm0gk+lMkEkWKRCKfQpaLRCLRBpFI9FIkEj0ViUSf5Fg2UiQSBWSUkZqzmsPhVFiIgBMn2LQo8zkcDqeUCEKwAdgNoPsHlvcAUC+jfA9gKwCIRCJLAAsAfAqgFYAFIpHIQq2Wcjic4lEexc3Jk0D//sC0adn2E7Hv/fuz5cWhPJ4jDoejUgQh2IjoOoD3H1jlawB/EeMuAHORSFQdQDcAF4noPRHFAriIDws/DoejaVQtboRA377A1KnA+vXZxzVtGvs+dSpbXhzK4znicDgqRVfbBhQRWwChOb6/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bDTPhp/ttRFhKEcNUxrYHTz0RjVbFTZ6NXFm8PUw/ffs+mOHcWveprV3dG7+HVLjDpGcyiDqFyYEAGXLwOrVgEXLwKmpizObfp0JuJUTFBsEPY82YNdj3fhdfzrD647p90cLO+6/KPb1IaASkxPxCGfQ3j5/iXqWtbFoEaDypVnLRMu2HLABRtH5cTFQbFhPc6eWo3NTom4UAcQicT4qv5X+L7F9+hRtwd0xDratrLo8OYw9VAWe4lW8OGy1C5MHj1iwu3wYUBfn4n6WbNYZwUVo1AqMP3f6dh8fzMUpMi3vKgetorWRKlpeC9RDkcdxMQgev4MrPqmGuq+d0Wf3onwdrbGb50WIPinYJz67hR61e9VtsQawDxoeUcjyNkc1revti3kaIIKPlyWNE2Knvt6QpouzQp4T5IlQZrO5iemJ5Z+J82bs6Hnnj9nIyds3gzUrs1SgoSElH77OdAR62DJ50tgrGdc4HKZUobPa3/+0e0c8j0EJRU8uoOSlDjkc6hUdnIKhgs2DqckxMfDe8EPGDuhOmpgLWZ3SEPNui1wZOARBM8Mg2snV9hVstO2lSUn71BLYjGPXapolMchxYqJRoVJvXosh1tAABsx4Y8/2LyJE4GwMJXtxtTAFGeHnoWpvikkeiyG1kDHADoiHaQr0uG0yQmjTo7C43ePC90GT7OhHbhg43CKgVKagLNLR+GL6VZoIt6OA85KjKo3EN4TveH5Py8MaDgAejp62jZTNWSKtpxwsVZxKOl4qeUIrQiTWrVYj9JXr4Bx45hwq1OHxbdFRqpkF+3t2yN8RjjWd1+POe3mYHPPzYibEwffSb4Y23wsjj47iubbm+PzPZ/jzIsz+URrZpqNgqhIaTY0DRdsHE4RSJHGYsfKb+H8qyW+ku/Bs2o6WOb0P4TOjsC2EYfRqEojbZuoeip4c5haaNaMlZJUrdYMzaqVrG6J6Nu
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 39,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" \n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlwAAAFmCAYAAAC4FUTmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3de3xU5Z0/8M93kplJMplwv8lFEBBFxAhpbb1AV6uLULEqLdvb7nZp3bVVbm6Brtttt62X2v2h2G67tbTd37ZuS38CXgr1uii4bVWwoCAiAQmXcAmXkMltZjLz/P44czKTyZlbcibPTM7n/XrlZTIzOfPNmHA+8zzP+T6ilAIRERER5Y9LdwFERERE/R0DFxEREVGeMXARERER5RkDFxEREVGeMXARERER5RkDFxEREVGeleouoCeGDh2qxo8fr7sMIkdTAA61t+NsOIyLKyrgLynRXVLRiSiFd1pa4BbBxRUVcIvoLomIemnHjh2nlVLDkm8vysA1fvx4bN++XXcZRI4Vjkbx2b178VZDAx666CKsHDdOd0lFa2tjI+a98w5aPR68Ul2N0V6v7pKIqBdEpM7qdk4pElFOOqJRfH7vXjzZ0ID/M3Eiw1YvzRo4EC9Mn46ToRD+YudO1AeDuksiojxg4CKirEWVwqJ9+/DbhgZ8/6KLsHzsWN0l9QsfHTAAz02fjuOhEG7ctQsNoZDukojIZgxcRJQVpRSW1Nbiv06exLfHj8c/cmTL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fhzTfT7U+P26S6F+zONy4W9GjsSzp0/jZCikuxyifoOBi6g
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 40,
"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",
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" # Remove the CWD from sys.path while we load stuff.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlwAAAFmCAYAAAC4FUTmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzde5xT5Z0/8M+TTDKX5ASYOxcRFeoFlMsMqC21tdqqs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kVj5EwxUDFxFRHoV9YbQ/1Y7a+bUocxi+uQcRGYSBi4gojzqf60TwRJBb+RANcwxcRER55FzjhKXeglFfHmX0UIjIQAxcRER5EugKoPPZTm0rnzL+uCUazvgTgIgoTzp+2wHpl2i8pdHooRCRwVjBSUSUJ8412lY+9hl2o4dCRHkmpUTIE0r5PAMXEVEeeA9pW/mc8cAZ3MqHqAT52nxwt7rh2u6C620X1BYVIz4/IuXrGbiIiPKgfW07AKD+xnqDR0JEgxV0BaG2qtpthwrXWy74PvVpT5oA2/k21C2sw8hLRwKbkx+DgYuIKMeklGh7vA0jLhmBygmVRg+HiLIQDoTheccD1zZt1kptVeHZ6wHC2vPlp5fD8VkHHBc5oDQpsM+wo8weF6duTH5cBi4iohxTW1R4P/TitH84zeihEFEaMizhPeCFukPFqT+fgtqiwrPbg3CPlq4sdRbYZ9hRe20tRlw8AvYZdljrrAP6LAYuIqIcc651QlgFt/IhKjC+Iz5t1qolErB2qAi5tEJ3k80EpVnBmG+OgeNCB5QLFVScXpGzGkwGLiKiHAoHta18aubWwDLKYvRwiIa
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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",
"execution_count": 42,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" \n",
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: UserWarning: The following kwargs were not used by contour: 'lw'\n",
" # Remove the CWD from sys.path while we load stuff.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEU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rLVWY8Hvz17FrdXVGCKoe/ZayS7+Tp86HirA2XfKgNTJrZUrFYo8LMxY/C5w4H1LS1pHiEhJJqMCFyc8wDnvAZANYA5jLFpkbdhjN3LGNvDGNvT2to69IMkJEs8evIkBAD/OXq03EMhaWBZZwH3cpTfXt7/jcPcXFaG8wwG/OTECfgFIU2jI4TEkhGBS8I57wLwAYCrolz3e855Lee8tpTOuCIkqpNuN9Y2NeGuykqMzcuTezgkDZr/2AzjLCNMNaak7qdgDD8bMwZHXS6so1kuQoac7IGLMVbKGCsIfp4H4OsADss7KkKy0y9PngQD8B+jRsk9FJIGjoMO2PfZUXF77N5b8SwuKcH5RiN+TrNchAw52QMXgEoAWxljBwDshljD9XeZx0RI1jnlduNPzc24q7ISI3U6uYdD0sDykgVQAmVLywZ0f8YYfjxmDI653TTLRcgQU8k9AM75AQDnyz0OQrLdr0+dAgCsptmtnCT4BTQ/34ziq4uhKdcM+DjXFhdjhsGAX548iW+Xl0NJPdoIGRKZMMNFCBmkJo8Hf2hqwh0VFRhFs1s5qfPdTnibvai4a2DLiRLGGH4wejSOulzYQCcgETJkKHARkgOeOHMGPs7xyMiRcg+FpIllnQWqQhWKFxYP+lg3lJZikl6PX548Sd3nCRkiFLgIyXJdPh+ebWzE0rIyTNDr5R4OSQO/1Y+2TW0oW1oGhWbwf7aVjOGRkSOx3+HA2x0dKRghIaQ/FLgIyXLPNjbCHghgFc1u5ayWl1sguARU3Dm45cRwt5aXo1qr7an9I4SkFwUuQrK
"text/plain": [
"<Figure size 691.2x388.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"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."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.8.3"
},
"livereveal": {
"start_slideshow_at": "selected",
2021-04-14 10:13:55 +02:00
"theme": "white"
2021-03-02 08:32:40 +01:00
}
},
"nbformat": 4,
"nbformat_minor": 4
}