1205 lines
450 KiB
Plaintext
1205 lines
450 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## Uczenie maszynowe UMZ 2017/2018\n",
|
|||
|
"# 3. Naiwny klasyfikator bayesowski, drzewa decyzyjne\n",
|
|||
|
"### Część 1"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## 3.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",
|
|||
|
"* Klasyfikatory probabilistyczne → klasyfikatory bayesowskie → naiwny klasyfikator bayesowski"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Twierdzenie Bayesa\n",
|
|||
|
"\n",
|
|||
|
"$$ P( y_k \\,|\\, x ) = \\frac{ P( x \\,|\\, y_k ) \\cdot P( y_k ) }{ \\sum_{i} P( x \\,|\\, y_i ) \\, P( y_i ) } $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Twierdzenie Bayesa\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": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Rola wyrażenia normalizacyjnego w twierdzeniu Bayesa\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 baywsowski 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",
|
|||
|
"execution_count": 10,
|
|||
|
"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": 11,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Wczytanie danych (gatunki kosaćców)\n",
|
|||
|
"\n",
|
|||
|
"data_iris_setosa = (\n",
|
|||
|
" pandas.read_csv('iris.csv', usecols=['pł.dł.', 'pł.sz.', 'Gatunek'])\n",
|
|||
|
" .apply(lambda x: [x[0], x[1], 1 if x[2] == 'Iris-setosa' else 0], axis=1))\n",
|
|||
|
"data_iris_setosa.columns = ['dł. płatka', 'szer. płatka', 'Iris setosa?']\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": 12,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"count: {0: 100, 1: 50}\n",
|
|||
|
"prior prob.: {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 'count: ', {c: count[c] for c in classes}\n",
|
|||
|
"print 'prior prob.:', {c: prior_prob[c] for c in classes}"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 13,
|
|||
|
"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": 14,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|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6/Mar7M7ERJt0i6U1K5svmjkpZV\n7rh9wcxM0uckvUXSbyW9zyfVe03GG+4BIGeeoD2M1FzbniRtfQpc+9O2kvzt0/i7hDpPymgvBAAA\nEFBhCu4BAAAwjuQLAAAgIJIvAED6PKY3X6kkXXRR9N6mWuVy/e2zOUeLldU0jfloGSRfAID0xfXm\nO/NMaeNGqa9vPNEql6P1jRuj3o7NnqPV+/81ivloHUkeiSzSwqsmAKAFxL2naWzMvbc3Wu/tjd7r\nNXm92XMU9F1QmWE+cqe8XzWRFZ52BIAWUb3rMjw8vm1wUBoail4TUL3TtX37+P7eXmnr1oktZZo5\nR6dhPnLFqyYAAPnzmN585bI0d+74eqmUPPFKeo5Ow3zkhldNAADyVb0LU6u2Hql656tWbQ1YGufo\nNMxHSyD5AgCkr/brr8HBKKEaHIzW166N7nBVv3Ls7Y3We3uj9aQJWNw5Oi3hYD5aR5LCsCItFNwD\nQAuI683X3z+1uL626H7duubPUdD+f5lhPnInCu4BALnxmN58q1ZJH/2otGHD1Pqk9eunbp/NOQra\n/y8zzEfuKLgHAAAIiIJ7AACAAiL5AgAACIjkCwAwUakkrV4d/ay3fd++9unLOF3M1e2lUvNxpnGt\noearKH+XdpekKr9IC087AkDGqk8idnVFbYDco59dXdH2I45onycV162b+Vqqc9FMnGlca6j5Ksrf\npUUp4dOOuSdTjS4kXwCQsdpEq5qA1a6PjrZPX8Z6sdeuj401H2ca1xpqvoryd2lRJF8AgNmrTbiq\nS+2dsNokpbokTbyqav9
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f805c1a8510>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 15,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[matrix([[ 1. , 4.906, 1.676]]), matrix([[ 1. , 1.464, 0.244]])]\n",
|
|||
|
"[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 X_mean \n",
|
|||
|
"print X_std"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 16,
|
|||
|
"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": 17,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"from scipy.stats import norm\n",
|
|||
|
"\n",
|
|||
|
"# Prawdopodobieństwo klasy dla pojedynczej cechy\n",
|
|||
|
"def prob(x, c, feature, mean, std):\n",
|
|||
|
" return norm(mean[c].item(0, feature), std[c].item(0, feature)).pdf(x)\n",
|
|||
|
"\n",
|
|||
|
"# Prawdopodobieństwo klasy\n",
|
|||
|
"def class_prob(x, c, mean, std):\n",
|
|||
|
" return prob(x[1], c, 1, mean, std) * prob(x[2], c, 2, mean, std)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 18,
|
|||
|
"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": 19,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/home/pawel/.local/lib/python2.7/site-packages/matplotlib/contour.py:967: UserWarning: The following kwargs were not used by contour: 'lw'\n",
|
|||
|
" s)\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzs3Xd8VGX2P/DPMymk0DsIoYOKJUJA\nYVEjoAtIExAs8Fvcr4urq4J1F3Xd3a+KrmVFV/QrNlZFFqUpqKigrAVdAY3Si0gXQkJNAmlzfn8c\nLlMyM5lkyp1JPm9f95XM3PbMZLxzeJ7znGtEBEREREQUOofdDSAiIiKqKRhYEREREYUJAysiIiKi\nMGFgRURERBQmDKyIiIiIwoSBFREREVGYMLAiIiIiChMGVkRERERhwsCKiIiIKEwS7W6Au6ZNm0r7\n9u3tbgYRERGRhzVr1uSJSLPKtoupwKp9+/ZYvXq13c0gIiIi8mCM2RnMdhwKJCIiIgoTBlZERERE\nYcLAioiIiChMGFgRERERhQkDKyIiIqIwYWBFREREFCYMrIiIiIjChIEVERERUZgwsCIiIiIKEwZW\nRERERGESscDKGNPNGJPjthwzxkyJ1PmIiIiI7BaxewWKyGYAmQBg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iQIaM/TGUEefzqAewGEq/eLiIiIKGYFSl6/CcAU6M2X18AVWB0D8FxlBzbGDAWQKyJrjDHZ\nAbabBGASAGRkZATXaiI
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f805c1dad10>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 20,
|
|||
|
"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)\n",
|
|||
|
"\n",
|
|||
|
"# 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": 21,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3XucXHV9//H3Z2/ZZHeTzZKYTQK5\nQCApUhLDVqkCLeAFsU1SQLBVf2JpafujJBAFjdWf1tbSYksMSi801lupUknCRVDESkVaIyYYESFA\nhEQg9/tukr3MzOf3x8yws5udmZOZc+a2r+fjMY/dc86cc75zkgd58z2f+RxzdwEAAKB4deUeAAAA\nQK0gWAEAAISEYAUAABASghUAAEBICFYAAAAhIVgBAACEhGAFAAAQEoIVAABASAhWAAAAIWko9wAy\nTZo0yWfNmlXuYQAAAAyxcePGve4+Od/7KipYzZo1Sxs2bCj3MAAAAIYws21B3setQAAAgJAQrAAA\nAEJCsAIAAAgJwQoAACAkBCsAAICQEKwAAABCQrACAAAICcEKAAAgJAQrAACAkBCsAAAAQkKwAgAA\nCAnBCgAAICQEKwAAgJAQrAAAAEJCsAIAAAgJwQoAACAkBCsAAICQEKwAAABCQrACAAAICcEKAAAg\nJAQrAACAkBCsAAAAQkKwAgAACAnBCgAAICQEKwAAgJAQrAAAAEJCsAIAAAgJwQoAACAkBCsAAICQ\nEKwAAABCQrACAAAICcEKAAAgJAQrAACAkBCsAAAAQkKwAgAACAnBCgAAICQEKwAAgJAQrAAAAEIS\nabAys3Yzu8fMNpvZs2b2m1GeDwAAoJwaIj7+KknfcfcrzKxJ0riIzwcAAFA2kQUrM5sg6QJ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
|||
|
"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": 39,
|
|||
|
"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": 40,
|
|||
|
"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": 41,
|
|||
|
"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": 42,
|
|||
|
"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": 43,
|
|||
|
"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": 44,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3XucVXW9//H3Z2ZAYLjf5B4qghkJ\nAqmYWWpX6iBaabfz6251KlHKW6dfdX7VsbTkYNrF6HI6WVmgZGWZdUZEZRAGUEBwQHCAGW4DIsNt\nLnt/fn/svZ0ZmNl7M3utWXvPvJ6Px37MrLX2+q7PWpnz8bs+67PM3QUAAIDcFUUdAAAAQFdBYgUA\nABAQEisAAICAkFgBAAAEhMQKAAAgICRWAAAAASGxAgAACAiJFQAAQEBIrAAAAAJSEnUALQ0dOtTH\njx8fdRgAAACtVFRU1Lr7sEzfy6vEavz48Vq1alXUYQAAALRiZlXZfI9bgQAAAAEhsQIAAAgIiRU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UnS9Zl2MrNhZjYw+XtvSW+TtKmDcQIAAOS9bBKrM82snyQlX2XzC2U36zRSUpmZPSdppRI1Vn/u\ncKQAAAB5Lptbgf/X3f9gZpdIequkOyX9SNKF6XZKvvrm/NxDBAAAKAzZzFjFkj/fLek+d/+LpJ7h\nhQQAAFCYskmsqs3sJ5KulfSImZ2W5X4AAADdSjYJ0jWSHpX0Dnc/KGmwpJtCjQoAAKAAZfNKm6OS\nHmyxvEu8hBkAAOAk3NI
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f80526a7ed0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 45,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xl8VOX1P/DPySQhOyQECIRVVpUK\nQqqI1lZtXdAvIK3a1vanrS1tv1ZQKiqtXb7dbK2VYrWLpXtta2XTVqu1bUQUghBAdsIaSELIRvZl\nMnPP74+ZMQvJzDBzb+4sn/frNa8w98597pkrmuPznHuuqCqIiIiIKHwJdgdAREREFCuYWBERERGZ\nhIkVERERkUmYWBERERGZhIkVERERkUmYWBERERGZhIkVERERkUmYWBERERGZhIkVERERkUkS7Q6g\nu9zcXB0/frzdYRARERH1UFxcXKOqwwJ9LqISq/Hjx2P79u12h0FERETUg4iUBvM5LgUSERERmYSJ\nFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERER\nmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgR\nERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJ\nmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFRER\nEZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJmFgRERERmYSJFREREZFJLE2sRGSIiKwR\nkYMickBErrDyfERERER2SrR4/FUAXlXVj4lIMoA0i89HREREZBvLEisRGQzgagB3A4CqOgE4rTof\nERERkd2sXAqcAKAawG9FZKeIrBaR9N4fEpHFIrJdRLZXV1dbGA4RERGRtaxMrBIBzALwc1W9FEAL\ngEd6f0hVn1XVAlUtGDZsmIXhEBEREVnLysSqDECZqm71vl8DT6JFREREFJMsS6xUtRLAKRGZ6t10\nHYD9Vp2PiIiIyG5W3xV4H4DnvHcEHgPwGYvPR0RERGQbSxMrVd0FoMDKcxARERFFCnZeJyIiIjIJ\nEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIi\nIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCx\nIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIi\nkzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysiIiIikzCxIiIiIjIJEysi\nIiIikzCx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rNIpIE4C0R+aeqFoURLxER\nEVHECuauQLeI/ABAqy+pEpEdgY5Tj2bv2yTvS8MJloiIiCiSBVNjtc/7uX+JSI53mwQzuPdZg7sA\nVAF4XVW39vGZxSKyXUS2V1dXBxs3ERERUcQJJrFyqepDAFYD2CQisxHkzJOqulV1JoDRAC4Tkel9\nfOZZVS1Q1YJhw4adT+xEREREESWY4nUBAFV9XkT2AfgzgLHncxJVrReRQgA3Ath73lESERERRYFg\nZqw+5/uDqu4F8AEASwI
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f80526a7ad0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 46,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Wczytanie danych (gatunki kosaćców)\n",
|
|||
|
"\n",
|
|||
|
"data_iris_versicolor = (\n",
|
|||
|
" pandas.read_csv('iris.csv', usecols=['pł.dł.', 'pł.sz.', 'Gatunek'])\n",
|
|||
|
" .apply(lambda x: [x[0], x[1], 1 if x[2] == 'Iris-versicolor' else 0], axis=1))\n",
|
|||
|
"data_iris_versicolor.columns = ['dł. płatka', 'szer. płatka', 'Iris versicolor?']\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_versicolor.values[:, 2]).reshape(m, 1)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 47,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"count: {0: 100, 1: 50}\n",
|
|||
|
"prior prob.: {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 'count: ', {c: count[c] for c in classes}\n",
|
|||
|
"print 'prior prob.:', {c: prior_prob[c] for c in classes}"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 48,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl8AAAFnCAYAAACYfixAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+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sLEkbJf1IUqm8+6OS5pavuH3e\nzEzSDZIukvSKpPf7qHqv0WgvBAA58gR9+aTseyo2cP+/hpbk91/v7yXEOTJCb0cAAICAClNwDwAA\ngGEkXwCAdHlMe5ihIem666KlA6qVSuPvn8o5GuyuTt2Yj4ZC8gUASFdce5jLLpNWr5a6uoYTrVIp\n2l69Omo/VO85mqEFzWQwH40lSVV+kV487QgABRe3VMCBA+6dndF2Z2e09MTo7XrPUeDlCDLBfBSC\n8n7aMSsU3ANAA6hcdVmzZnhfT4/U2xs9qVa50vXII8PHOzul/v6Rq5rXc45Ww3zkjqcdAQD58pj2\nMKWSNG3a8PbQUPLEK+k5Wg3zkSuedgQA5KdyFaZadT1S5cpXteoasDTO0WqYj4ZB8gUASFf17a+e\nniih6umJtpcuja5wVW45dnZG252d0XbSBCzuHK2WcDAfjSVJYViRXhTcA0DBxbWH6e4eW1xfXXS/\nbFn95yhwC5pMMB+FIAruAQC58Jj2MJdcIn30o9LKlWPrk1asGLt/KucocAuaTDAfhUDBPQAAQEAU\n3AMAABQQyRcAAEBAJF8AgGHl/ooDe1/Wus3rdN3d12nd5nUa2Pty1HfxwIHm6cs4UczV+9OItShj\nFOEciCSpyi/Si6cdASBDy5b5xrny9o+1+cxPz3R9Qj7z0zO9/WNtvnGu3E87rXmeVFy2LP6zpBFr\nUcYowjmanBI+7Zh7MjX
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f805a13dd50>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 49,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[matrix([[ 1. , 3.508, 1.135]]), matrix([[ 1. , 4.26 , 1.326]])]\n",
|
|||
|
"[matrix([[ 0. , 2.08373127, 0.91459007]]), matrix([[ 0. , 0.46518813, 0.19576517]])]\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 X_mean\n",
|
|||
|
"print X_std"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 50,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/home/pawel/.local/lib/python2.7/site-packages/matplotlib/contour.py:1180: UserWarning: No contour levels were found within the data range.\n",
|
|||
|
" warnings.warn(\"No contour levels were found\"\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzs3Xd4lFXax/HvmfRGgBAglAiKgBQJ\nRWlKEVwBuyBYcBd1RXFVsC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IAb7zhvI4YUbVxeNEb8U4bR3Tw3TaK1BaeJFaHjTHhwI/Ah8aY/UCad8MS\nERERqXk8GQq8EkgH7gXmAb8Dl3kzKBEREZGayJPEqo+11m2tzbXWvmetfQUY7u3ARERERGoaTxKr\nvxpjLsp/Y4x5EKcXS0REREQK8aTG6grgq2MJ1SCgLUqsROR0mTmzqiPwupnDfb+NIrVFuYmVtTbp\n2FxW84HlwDBrrfV6ZCIiAA0aVHUEXtcg1PfbKFJblJpYGWNSAQuYY6+BwJnAMGOMtdbW8eQBxhg/\nIB7Yba1V0buInJypU53XUaOqMgqvmpowFYBRcaOqNA4ROXWlJlbW2ohKesZYnJnaPUrERESKUGIl\nIjVIucXrxpgFnuwr5dpmwKXAOycfmoiIiEjNUtZQYDAQBjQwxtTDGRIEp+epqYf3nwQ8BFRW75eI\niIhItVVW8frtwDicxZe
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f8051c24090>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 51,
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3X2cHXV5///3dfb+LgnJ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qSp5nHvd\n/UsFjBUAAGBEy+epwFclXWhmdZIi7t6S5767JF3g7q1mViHpCTP7T3d/soDxAgAAjFj5PBUYM7Ov\nS2pPhSozezbXdp7QmpytSE5eyGABAABGsnxqrF5Mfu6/zGxicpnls/PkuwbXS9or6Zfu/tQgn7nK\nzNaa2dp9+/blO24AAIARJ59gFXX3GyXdKelxM1uoPK88uXvM3edLmiHpLDM7dZDP3OHui9x90eTJ\nk49m7AAAACNKPsXrJknuvsrMXpT0E0kzj+Yg7n7IzB6V9F5JLxz1KAEAAEpAPlesPpX6xd1fkPQ2\nSdfk2sjMJpvZhOTvNZLeLWnjEMcJAAAw4uUTrE4wswZJSr7K5t+U31WnaZIeNbPnJT2jRI3Vz4c8\nUgAAgBEun1uBX3T3e8zsPEkXSvq/kr4j6exsGyVffXNm4UMEAAAoDflcsYolf14k6Q53/4WkyvCG\nBAAAUJryCVY7zOy7kq6Q9JCZVeW5HQAAwJiST0D6kKRHJP2Jux+SNFHSDaGOCgAAoATl80qbdkmr\n0+Z3iZcwAwAADMAtPQA
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f8051b18a10>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 52,
|
|||
|
"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": 53,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"theta = [[-22.4168027 ]\n",
|
|||
|
" [ 12.03703048]\n",
|
|||
|
" [ 11.13804517]\n",
|
|||
|
" [ -1.75684025]\n",
|
|||
|
" [ 1.01230657]\n",
|
|||
|
" [ -7.61403096]]\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": 54,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3XmcFOWdP/DPt++5h2EGZuQQEASF\nyBnEiBo1bgwawCMha7LZuGbdjSbikYi46y9m3RXXRAkadWPMxpBLohwaY4zG9VbA4UYOuQWGa4CZ\nnrPP7++P6mZ6jj6Yrprunvm8X6969XRVV9XTJTIfnudbT4mqgoiIiIjSZ8t0A4iIiIh6CwYrIiIi\nIpMwWBERERGZhMGKiIiIyCQMVkREREQmYbAiIiIiMgmDFREREZFJGKyIiIiITMJgRURERGQSR6Yb\nEKu8vFyHDRuW6WYQERERtbNmzZpaVa1I9rmsClbDhg1DdXV1pptBRERE1I6I7EvlcxwKJCIiIjIJ\ngxURERGRSRisiIiIiEzCYEVERERkEgYrIiIiIpMwWBERERGZhMGKiIiIyCQMVkREREQmYbAiIiIi\nMgmDFREREZFJGKyIiIiITMJgRURERGQSBisiIiIikzBYEREREZmEwYqIiIjIJAxWRERERCZhsCIi\nIiIyCYMVERERkUkYrIiIiIhMwmBFREREZBIGKyIiIiKTMFgRERERmYTBioiIiMgkDFZEREREJmGw\nIiIiIjIJgxURERGRSRisiIiIiEzCYEVERERkEgYrIiIiIpMwWBERERGZhMGKiIiIyCQMVkREREQm\nYbAiIiIiMgmDFREREZFJGKyIiIiITMJgRURERGQSBisiIiIikzBYEREREZnE0mAlIqUi8oKIbBOR\nrSJygZXnIyIiIsokh8XHXwTgVVW9XkRcAPItPh8RERFRxlgWrESkBMDFAL4FAKrqB+C36nxERERE\nmWblUOBwAMcA/EpE1onIMyJS0PFDInKziFSLSPWxY8csbA4RERGRtawMVg4AkwA8paoTATQBuKfj\nh1T1aVWdoqpTKioqLGwOERERkbWsDFYHABxQ1VWR9y/ACFpEREREvZJlwUpVDwPYLyKjI6suB7DF\nqvMRERERZZrVdwV+D8DvIncE7gZwo8XnIyIiIsoYS4OVqq4HMMXKcxARERFlC868TkRERGQSBisi\nIiIikzBYEREREZmEwYqIiIjIJAxWRERERCZhsCIiIiIyCYMVERERkUkYrIiIiIhMwmBFREREZBIG\nKyIiIiKTMFgRERERmYTBioiIiMgkDFZEREREJmGwIiIiIjIJgxURERGRSRisiIiIiEzCYEVERERk\nEgYrIiIiIpMwWBERERGZhMGKiIiIyCQMVkREREQmYbAiIiIiMgmDFREREZFJGKyIiIiITMJgRURE\nRGQSBisiIiIikzBYEREREZmEwYqIiIjIJAxWRERERCZhsCIiIiIyCYMVERERkUkYrIiIiIhMwmBF\nREREZBIGKyIiIiKTMFgRERERmYTBioiIiMgkDisPLiJ7ATQACAEIquoUK89HRERElEmWBquIS1W1\ntgfOQ0RERJRRHAokIiIiMonVwUoBvCYia0Tk5q4+ICI3i0i1iFQfO3bM4uYQERERWcfqYDVdVScB\n+BKAW0Xk4o4fUNWnVXWKqk6pqKiwuDlERERE1rE0WKnqwcjrUQDLAUy18nxEREREmWRZsBKRAhEp\niv4M4O8AbLbqfERERESZZuVdgQMBLBeR6Hl+r6qvWng+IiIiooyyLFip6m4A4606PhEREVG24XQL\nRERERCZhsCIiIiIyCYMVERERkUkYrIiIiIhMwmBFREREZBIGKyIiIiKTMFgRERERmYTBioiIiMgk\nDFZEREREJmGwIiIiIjIJgxURERGRSRisiIiIiEzCYEVERERkEgYrIiIiIpMwWBERERGZhMGKiIiI\nyCQMVkREREQmYbAiIiIiMgmDFREREZFJGKyIiIiITMJgRURERGQSBisiIiIikzBYEREREZmEwYqI\niIjIJAxWRERERCZhsCIiIiIyCYMVERERkUkYrIiIiIhMwmBFREREZBIGKyIiIiKTMFgRERERmYTB\nioiIiMgkDFZEREREJmGwIiIiIjKJI9UPisgAAJ7oe1X91JIWEREREeWopD1WIjJTRHYA2APgbQB7\nAfwl1ROIiF1E1onIy91uJREREVEOSGUo8AEA0wB8oqrDAVwOYOVpnGMugK3daBsRERFRTkklWAVU\n9TgAm4jYVPVNAFNSObiIDAZwFYBn0mgjERERUU5IpcaqTkQKAbwD4HcichRAU4rH/ymAuwEUdbN9\nRERERDkjlR6rWQCaAdwB4FUAuwBcnWwnEbkawFFVXZPkczeLSLWIVB87diyF5hARERFlp1SC1cWq\nGlbVoKr+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iIcksq0y38AsB8AAEAUNWNAL5mZaOIiIiIclEqwSpfVVd3WBe0ojFE\nREREuSyVYFUrImfBKFiHiFwP4JClrSIiIiLKQancFXgrgKcBjBGRgwD2APhGsp1ExAPgHQDuyHle\nUNUfptFWIiIioqyWyl2BuwF8QUQKANhUtSHFY/sAXKaqjSLiBPCeiPxFVVem0V4iIiKirJXKXYEh\nEXkIQHM0VInI2mT7qaEx8tYZWTSdxhIRERFls1RqrD6OfO41ESmLrJNUDh551uB6AEcBvK6qq7r4\nzM0iUi0i1ceOHUu13URERERZJ5VgFVTVuwE8A+BdEZmMFHueVDWkqhMADAYwVUTGdfGZp1V1iqpO\nqaioOJ22ExEREWWVVIrXBQBUdYmIfAzg9wCGns5JVLVORN4EcCWAzafdSiIiIqIckEqP1bejP6jq\nZgAXAbgt2U4iUiEipZG
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f8051b20250>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 55,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlYAAAFnCAYAAABkaweKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzs3Xl4W+Wd9vHvI8mSLK9x7DjORhIS\nEkggawktFAYoUxpaCEuhQzt9u8wwLcywdcrSGd62L9OGblDK0E4pM9PSlZYlUEqhhQJlDWSFEBIS\nshDixI4T2/Km9TzvH8eKncSWlEjyEt+f6zqXonN0znkkTHznOT/9jrHWIiIiIiK58wz2AERERESO\nFgpWIiIiInmiYCUiIiKSJwpWIiIiInmiYCUiIiKSJwpWIiIiInmiYCUiIiKSJwpWIiIiInmiYCUi\nIiKSJ77BHkBv1dXVdvLkyYM9DBEREZEDrFy5sslaW5PpdUMqWE2ePJkVK1YM9jBEREREDmCM2Z7N\n63QpUERERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxE\nRERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRP\nFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRE\nRCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTB\nSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE8kTBSkRERCRPFKxERERE\n8qSgwcoYU2mMecAYs8EY85Yx5v2FPJ+IiIjIYPIV+Ph3Ak9Yay8xxviBUIHPJyIiIjJoChasjDEV\nwOnAZwCstTEgVqjziYiIiAy2Ql4KnALsAf7XGLPaGHOvMabk4BcZY64wxqwwxqzYs2dPAYcjIiIi\nUliFDFY+YD7wI2vtPKADuOngF1lr77HWLrTWLqypqSngcEREREQKq5DB6j3gPWvt8u7nD+AGLRER\nEZGjUsGClbV2N7DDGDOje9XZwPpCnU9ERERksBX6W4H/Avyy+xuBW4DPFvh8IiIiIoOmoMHKWrsG\nWFjIc4iIiIgMFeq8LiIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJg\nJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIi\neaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYi\nIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpIn\nClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieaJgJSIiIpInClYiIiIieeIr5MGN\nMduANiAJJKy1Cwt5PhEREZHBVNBg1e1Ma23TAJxHREREZFDpUqCIiIhInhQ6WFngT8aYlcaYK/p6\ngTHmCmPMCmPMij179hR4OCIiIiKFU+hgdZq1dj7wEeAqY8zpB7/AWnuPtXahtXZhTU1NgYcjIiIi\nUjgFDVbW2p3dj43Aw8DJhTyfiIiIyGAqWLAyxpQYY8pSfwb+FlhXqPOJiIiIDLZCfiuwFnjYGJM6\nz6+stU8U8HwiIiIig6pgwcpauwWYU6jji4iIiAw1arcgIiIikicKViIiIiJ5omAlIiIikicKViIi\nIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicK\nViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIi\nkicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAl\nIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5omAlIiIikicKViIiIiJ5\n4sv2hcaYMUAw9dxa+25BRiQiIiIyTGWcsTLGnG+M2QRsBZ4DtgF/zPYExhivMWa1MeaxIx6liIiI\nyDCQzaXAW4FTgLettVOAs4FXDuMc1wBvHcHYRERERIaVbIJV3Fq7F/AYYzzW2meAhdkc3BgzATgP\nuDeHMYqIiIgMC9nUWLUYY0qBvwK/NMY0Ah1ZHv/7wA1A2RGOT0RERGT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fah\ngo1KREREZBjKJlgFgb3AWb3WWUDBSkRERKSXbDqvf3YgBiIiIiIy3GX8VqAx5jhjzNPGmHXdz08y\nxvx74YcmIiIiMrxk027hJ8DNQBzAWvs68IlCDkpERERkOMomWIWsta8etC5RiMGIiIiIDGfZBKsm\nY8yxuAXrGGMuAXYVdFQiIiIiw1A23wq8CrgHmGmM2QlsBT6VaSdjTBD4KxDoPs8D1tqv5jBWERER\nkSEtm28FbgE+ZIwpATzW2rYsjx0FzrLWthtjioAXjDF/tNa+ksN4RURERIasbL4VmDTG3AZ0pkKV\nMWZVpv2sq737aVH3YnMZrIiIiMhQlk2N1Zvdr/uTMaaqe53J5uDd9xpcAzQCf7bWLu/jNVcYY1YY\nY1bs2bMn23GLiIiIDDnZBKuEtfYG4F7geWPMArKcebLWJq21c4EJwMnGmNl9vOYea+1Ca+3Cmpqa\nwxm7iIiIyJCSTfG6AbD
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f8051b4b0d0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 56,
|
|||
|
"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": 57,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnAAAAFpCAYAAAAcIhVtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3X+QXGd95/vPd4xGVFqdYMla4gwI\nm2h2CVa2jDVxkkWVIcSAmD/sQbAZE1MxiXJ92Q1ryVKCRHG3QrEhEN9bkuUbahMzIcBeFTRrRkIJ\nihVhfs4SWEZeAxKUmcEUxh4HvBKE1gRmBvp7/zjnSEc93TPdM93nV79fVV3T/ZzT3c/pH3O+/Tzf\n53nM3QUAAID86Eu7AgAAAGgPARwAAEDOEMABAADkDAEcAABAzhDAAQAA5AwBHAAAQM4QwAEAAOQM\nARwAAEDOEMABAADk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14F\nOmKlKTh6ecqUXrCaOfSyMpVMB9cknnxiUgOHBrT3ob269/P3au9DezVwaECTT0w2vxNdyUhRp3Lp\nMsvde+6yfft2B1pSq7nv2RMs975nT+PbExNX3q6/38REuseAtYm/l9El/l6vdN/4/Vq5Tye18vlt\nwQ9//EMv/1nZ9XYtuZT/rOzV+WrjO/LdQAZU56s+fmbcD54+6ONnxpt/XjNC0pS3EMukHkylcSGA\nQ8taOQFFQVz9ybBZOfJjLQHQWgK/TupAPd575r1eemepYQBXemfJx8+Mr/zcawgggV7SagCXehcq\nkGmtTMHBKM/iWu0cep6
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f8062365510>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 58,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"count: {0: 69, 1: 30}\n",
|
|||
|
"prior prob.: {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 'count: ', {c: count[c] for c in classes}\n",
|
|||
|
"print 'prior prob.:', {c: prior_prob[c] for c in classes}"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 59,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[matrix([[ 1. , 0.03949835, 0.02825019]]), matrix([[ 1. , 0.09929617, 0.06206227]])]\n",
|
|||
|
"[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 X_mean\n",
|
|||
|
"print X_std"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 60,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnAAAAFpCAYAAAAcIhVtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzsnXdYFFcXxt9dOgsICBYQxIqINaKx\nxRJNLFGjRqOx1xj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h+1bfo1f9XtAR6qjbypLDu8+UgzaOQq3k\n03spXag8fsyE3NGjgL4+E/lz5rDBDwpGJpdh5r8zsfXBVshIVmB9ST1wla1LU9XwUagcjiqIjUXM\nwllY800N1P/gin59k+DjbI3fuixCyE8hOPPdGfRp2Ee7xBvAPGz5Z0vI3X3Wv7+6LeSogko+vZc4\nXYzeB3pDnCHODqBPliRDnME+T8pIKv9OWrZkU+W9eMFmdti6Fahbl6UgCQ0t//ZzoSPUwbLPl8FY\nz7jQ9RK5BJ/X/fyj2znidwRyKnz2CTnJccT3SLns5JQMLuA4nLKQkACfRT9g/KSaqIX1mNspHbXr\nt8KxwccQMjscrl1cYVfF7uPb0VTyTw0lFPLYp8pGRZwCrZSoVKg0aMByyAUGshkd/viDfTZ5MhCu\nuCm+TQ1McX74eZjqm0Kkx2JwDXQMoCPQQYYsA05bnDDm9Bg8ef+kyG3wtB6aARdwHE4pkIsTcX75\nGHwx0wrNhDtxyFmOMQ0Gw2eyD7z+541BjQdBT0dP3WYqhiwRlxsu3ioPZZ3vtQKhFqFSpw4bsfr6\nNTBhAhNy9eqx+LioKIX
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f80526c9fd0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 61,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|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gDUe/\nKIkFQ4bgGpcLDzc0IMgrIom00R7ARORtEblWRGaKyAwR+bbumoiKTZPXi582NeEvR47EeI5+URJK\nKTx42WV4v7MTm3hBE5E22gMYEfXdupMn4RfB6nHjdJdCReCuYcNwRVUVHm5oYHd8Ik0YwIiKXIvf\njx83NmLx8OGYUlWluxwqAmVKYfW4cdjX3o7nL1zQXQ5RSWIAIypyP25sRFswiFUc/aIMfH7ECIx1\nOrvXDhJRfjGAERWxrmAQ60+exG2DBqHW7dZdDhURh82GFWPH4tVLl/BGa6vucohKDgMYURH777Nn\nccbvx9c4+kVZ+MqoURhQVob/e+KE7lKISg4DGFGREhGsPXEC17hcuGXQIN3lUBFyl5fjntGjsam5\nGQ1dXbrLISopDGBEReqlixdxoKMDK8aNg1JKdzlUpO4fMwYA8MTJk5orISotDGBERWr9yZMYYbfj\nz4cP110KFbFxFRX4zPDh+GlTE9oCAd3lEJUMBjCiInS4owPbL1zAvWPGwGnjnzH1zbIxY3ApGMR/\nnTmjuxSiksF/uYmK0A9PnYJdKfztqFG6S6F+4LqaGtS53fjhqVNszEqUJwxgREWmLRDAf54+jc8M\nG4aRTqfucqgfUErh/jFjcLCjA//T0qK7HKKSwABGVGSeOnsWrcEg7gsvniaywmeHDcOQ8nL86NQp\n3aUQlQQGMKIiIiL48alTuMblwkdqanSXQ/1IRVkZlowahWfPncMpr1d3OUT9HgMYURF5vbUV+9rb\n8XejR7P1BFnub0ePRhDAhqYm3aUQ9XsMYERF5N+bmlBdVoYvjBihuxTqhyZVVuK2QYPw06YmBLkY\nnyinGMCIisSlQAAbz57
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f8059fbccd0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 62,
|
|||
|
"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": 63,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmAAAAFpCAYAAAA7jJSFAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzs3XmcU/W9P/7XJzPJLMkJMDuLFBV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4\n/EhEDGBERDknpYTjcQdGXjwSlZ+rNHo4RFQAGMCIiHLMud0Jz0EPGm/k7BcRaRjAiIhyzPG4A6ZK\nE+oXceshItIwgBER5VDIG0LHkx2oW1CHcrvhva+JqEAwgBER5VDXH7sQOB7g1kNE1AcDGBFRDjnW\nOmBuMGPUV0YZPRQiKiAMYEREOeLv9qPrmS5t66Fy/rgloij+RCAiypHO33VC+iSabmoyeihEVGBY\nEUpElCOOtdrWQ7aZNqOHQkQ5JqVE0B1M+3gGMCKiHPB8pG09dOr9p3LrIaIS5G33wtXmgnOHE863\nnFBbVYy4cETaz2cAIyLKgY51HQCAhusbDB4JEQ1VwBmA2qZqt50qnG844f3Uqz1oAqznWFG/qB4j\nLx4JbEnvnAxgRERZJqVE+2PtGHHRCFRNqDJ6OESUgZA/BPfbbji3a7NaapsK9z43ENIer/hcBeyf\nt8N+vh1KswLbTBvKbTFx6vr0XocBjIgoy9RWFZ4PPDjln08xeihElIQMSXgOeqDuVHHyzZNQW1W4\n97gR6tXSlrneDNtMG+q
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f808fe1d8d0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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": 64,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmAAAAFpCAYAAAA7jJSFAAAABHNCSVQICAgIfAhkiAAAAAlw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PbL06fhCATw6LhxcneFpEHzi83QVGpQdHVRQre/q7ISk/Ly8J9ffknFWQnJELIHMC6y\nB79UBz/oLwQhA3TO48Fvz53DtysqMNXQf3ccyW6+Dh863upA2TfKwJSJTS2rFQr8bOxYHHQ4sKGl\nJc09JIQkQvYABgCMMSVjbB+AFgD/4JzvinCbexljexhje1pbW4e+k4RkiUdPnYIA4L/GjJG7KyQN\nLOst4F6O8m+Xx79xmFvLynCBwYCfnDwJvyCkqXeEkERlRADjnAc459UARgKYyxibHuE2v+ec13DO\na0ppRxchEZ1yu7GuqQl3VVZiXF6e3N0hadD8x2YYZxthqjYldT8FY/jZ2LE45nJhPY2CESK7jAhg\nEs55F4BtAK6Ruy+EZKNfnjoFBuA/R4+WuyskDRyHHLDvs6Pi29Frf8WypKQEFxqN+DmNghEiO9kD\nGGOslDFWEPw8D8BVAI7I2ytCss9ptxt/am7GXZWVGKXTyd0dkgaWlyyAEihbVjag+zPG8OOxY3Hc\n7aZRMEJkJnsAA1AJYBtj7ACAWohrwP4uc58IyTq/Pn0aALCGRr9ykuAX0Px8M4qvLYamXDPgdq4v\nLsZMgwG/PHUKAdoRSYhsZA9gnPMDnPMLOeczOOfTOec/k7tPhGSbJo8Hf2hqwh0VFRhNo185qfPd\nTnibvai4a2DTjxLGGH4wZgyOuVzYSBuaCJGN7AGMEDJ4T5w9Cx/neGTUKLm7QtLEst4CVaEKxYuK\nB93WTaWlmKzX45enTlF
|
|||
|
"text/plain": [
|
|||
|
"<matplotlib.figure.Figure at 0x7f805269b2d0>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"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ę średnią i odchyleniem standardowym"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"celltoolbar": "Slideshow",
|
|||
|
"kernelspec": {
|
|||
|
"display_name": "Python 3",
|
|||
|
"language": "python",
|
|||
|
"name": "python3"
|
|||
|
},
|
|||
|
"language_info": {
|
|||
|
"codemirror_mode": {
|
|||
|
"name": "ipython",
|
|||
|
"version": 2
|
|||
|
},
|
|||
|
"file_extension": ".py",
|
|||
|
"mimetype": "text/x-python",
|
|||
|
"name": "python",
|
|||
|
"nbconvert_exporter": "python",
|
|||
|
"pygments_lexer": "ipython2",
|
|||
|
"version": "2.7.15rc1"
|
|||
|
},
|
|||
|
"livereveal": {
|
|||
|
"start_slideshow_at": "selected",
|
|||
|
"theme": "amu"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 2
|
|||
|
}
|