1741 lines
375 KiB
Plaintext
1741 lines
375 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## Uczenie maszynowe UMZ 2019/2020\n",
|
|||
|
"### 7 kwietnia 2020\n",
|
|||
|
"# 5. Regresja wielomianowa. Problem nadmiernego dopasowania"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## 5.1. Regresja wielomianowa"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Wprowadzenie: wybór cech"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Niech naszym zadaniem będzie przewidzieć cenę działki o kształcie prostokąta.\n",
|
|||
|
"\n",
|
|||
|
"Jakie cechy wybrać?"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Możemy wybrać dwie cechy:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
" * $x_1$ – szerokość działki, $x_2$ – długość działki:\n",
|
|||
|
"$$ h_{\\theta}(\\vec{x}) = \\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"...albo jedną:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
" * $x_1$ – powierzchnia działki:\n",
|
|||
|
"$$ h_{\\theta}(\\vec{x}) = \\theta_0 + \\theta_1 x_1 $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Można też zauważyć, że cecha „powierzchnia działki” powstaje przez pomnożenie dwóch innych cech: długości działki i jej szerokości."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"**Wniosek:** możemy tworzyć nowe cechy na podstawie innych poprzez wykonywanie na nich różnych operacji matematycznych."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Regresja wielomianowa"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"W regresji wielomianowej będziemy korzystać z cech, które utworzymy jako potęgi cech wyjściowych."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 1,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Przydatne importy\n",
|
|||
|
"\n",
|
|||
|
"import ipywidgets as widgets\n",
|
|||
|
"import matplotlib.pyplot as plt\n",
|
|||
|
"import numpy as np\n",
|
|||
|
"import pandas\n",
|
|||
|
"\n",
|
|||
|
"%matplotlib inline"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Przydatne funkcje\n",
|
|||
|
"\n",
|
|||
|
"def cost(theta, X, y):\n",
|
|||
|
" \"\"\"Wersja macierzowa funkcji kosztu\"\"\"\n",
|
|||
|
" m = len(y)\n",
|
|||
|
" J = 1.0 / (2.0 * m) * ((X * theta - y).T * (X * theta - y))\n",
|
|||
|
" return J.item()\n",
|
|||
|
"\n",
|
|||
|
"def gradient(theta, X, y):\n",
|
|||
|
" \"\"\"Wersja macierzowa gradientu funkcji kosztu\"\"\"\n",
|
|||
|
" return 1.0 / len(y) * (X.T * (X * theta - y)) \n",
|
|||
|
"\n",
|
|||
|
"def gradient_descent(fJ, fdJ, theta, X, y, alpha=0.1, eps=10**-5):\n",
|
|||
|
" \"\"\"Algorytm gradientu prostego (wersja macierzowa)\"\"\"\n",
|
|||
|
" current_cost = fJ(theta, X, y)\n",
|
|||
|
" logs = [[current_cost, theta]]\n",
|
|||
|
" while True:\n",
|
|||
|
" theta = theta - alpha * fdJ(theta, X, y)\n",
|
|||
|
" current_cost, prev_cost = fJ(theta, X, y), current_cost\n",
|
|||
|
" if abs(prev_cost - current_cost) > 10**15:\n",
|
|||
|
" print('Algorithm does not converge!')\n",
|
|||
|
" break\n",
|
|||
|
" if abs(prev_cost - current_cost) <= eps:\n",
|
|||
|
" break\n",
|
|||
|
" logs.append([current_cost, theta]) \n",
|
|||
|
" return theta, logs\n",
|
|||
|
"\n",
|
|||
|
"def plot_data(X, y, xlabel, ylabel):\n",
|
|||
|
" \"\"\"Wykres danych (wersja macierzowa)\"\"\"\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",
|
|||
|
" ax.scatter([X[:, 1]], [y], c='r', s=50, label='Dane')\n",
|
|||
|
" \n",
|
|||
|
" ax.set_xlabel(xlabel)\n",
|
|||
|
" ax.set_ylabel(ylabel)\n",
|
|||
|
" ax.margins(.05, .05)\n",
|
|||
|
" plt.ylim(y.min() - 1, y.max() + 1)\n",
|
|||
|
" plt.xlim(np.min(X[:, 1]) - 1, np.max(X[:, 1]) + 1)\n",
|
|||
|
" return fig\n",
|
|||
|
"\n",
|
|||
|
"def plot_fun(fig, fun, X):\n",
|
|||
|
" \"\"\"Wykres funkcji `fun`\"\"\"\n",
|
|||
|
" ax = fig.axes[0]\n",
|
|||
|
" x0 = np.min(X[:, 1]) - 1.0\n",
|
|||
|
" x1 = np.max(X[:, 1]) + 1.0\n",
|
|||
|
" Arg = np.arange(x0, x1, 0.1)\n",
|
|||
|
" Val = fun(Arg)\n",
|
|||
|
" return ax.plot(Arg, Val, linewidth='2')"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Wczytanie danych (mieszkania) przy pomocy biblioteki pandas\n",
|
|||
|
"\n",
|
|||
|
"alldata = pandas.read_csv('data_flats.tsv', header=0, sep='\\t',\n",
|
|||
|
" usecols=['price', 'rooms', 'sqrMetres'])\n",
|
|||
|
"data = np.matrix(alldata[['sqrMetres', 'price']])\n",
|
|||
|
"\n",
|
|||
|
"m, n_plus_1 = data.shape\n",
|
|||
|
"n = n_plus_1 - 1\n",
|
|||
|
"Xn = data[:, 0:n]\n",
|
|||
|
"Xn /= np.amax(Xn, axis=0)\n",
|
|||
|
"Xn2 = np.power(Xn, 2) \n",
|
|||
|
"Xn2 /= np.amax(Xn2, axis=0)\n",
|
|||
|
"Xn3 = np.power(Xn, 3) \n",
|
|||
|
"Xn3 /= np.amax(Xn3, axis=0)\n",
|
|||
|
"\n",
|
|||
|
"X = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n + 1)\n",
|
|||
|
"X2 = np.matrix(np.concatenate((np.ones((m, 1)), Xn, Xn2), axis=1)).reshape(m, 2 * n + 1)\n",
|
|||
|
"X3 = np.matrix(np.concatenate((np.ones((m, 1)), Xn, Xn2, Xn3), axis=1)).reshape(m, 3 * n + 1)\n",
|
|||
|
"y = np.matrix(data[:, -1]).reshape(m, 1)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Postać ogólna regresji wielomianowej:\n",
|
|||
|
"\n",
|
|||
|
"$$ h_{\\theta}(x) = \\sum_{i=0}^{n} \\theta_i x^i $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Funkcja regresji wielomianowej\n",
|
|||
|
"\n",
|
|||
|
"def h_poly(Theta, x):\n",
|
|||
|
" \"\"\"Funkcja wielomianowa\"\"\"\n",
|
|||
|
" return sum(theta * np.power(x, i) for i, theta in enumerate(Theta.tolist()))\n",
|
|||
|
"\n",
|
|||
|
"def polynomial_regression(theta):\n",
|
|||
|
" \"\"\"Funkcja regresji wielomianowej\"\"\"\n",
|
|||
|
" return lambda x: h_poly(theta, x)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Najprostszym przypadkiem regresji wielomianowej jest funkcja kwadratowa:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Funkcja kwadratowa:\n",
|
|||
|
"\n",
|
|||
|
"$$ h_{\\theta}(x) = \\theta_0 + \\theta_1 x + \\theta_2 x^2 $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"[<matplotlib.lines.Line2D at 0x7f32d565c2d0>]"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoAAAAFkCAYAAACw8IoqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/A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z+1wREQkgl5Y5Cz/qvhDoo0SwFbWOT2Zs47ogbXw7MKNbocjIiIRUvj9fj77tojkBA/nHdXL7XBEDqAE0AWX+YtBXszbRHlVtcvRiIhIJLyYtwmfhVOHdqNzerLb4YgcQAmgC47q05EhPdpTXFrBu19vczscEREJs2qf5cXFNb3/RKKNEkAXGGO4fKyKQUREYtXc/CIK95TRp3MaYwd0cTsckYMoAXTJxOE9yUxJZMmG3awo3ON2OCIiEkaB4o9Jo3vj8aj4Q6KPEkCXpCUncv5I56HgZxaoGEREJFbs8Jbx0aodJHgMF45U8YdEJyWALvqpvxjk9S+3sLes0uVoREQkHF5espkqn+XHh2WT3T7V7XBEglIC6KJB2RkcO7AL+yureXXJZrfDERGRFvL5LC8scoo/tPOHRDMlgC4L7A/89IINWKv9gUVE2rL563axsXgfOR3bcUJultvhiISkBNBlPx7SjW7tU1hbVMr8dbvcDkdERFrgeX/xx4WjepGg4g+JYkoAXZaU4OHi0c4ywTNqCSMi0mYVl1bwnxXb8Rhn20+RaKYEMApccnQfEjyG91dsZ/veMrfDERGRZnh16WYqqn386JAsenZs53Y4IvVSAhgFundI5dQh3aj22R+WD0REpO2wtub7t4o/pC1QAhglAsUgzy/aSGW1z+VoRESkKfI27GZtUSlZmSmcdGi22+GINEg
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 691.2x388.8 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"fig = plot_data(X2, y, xlabel='x', ylabel='y')\n",
|
|||
|
"theta_start = np.matrix([0, 0, 0]).reshape(3, 1)\n",
|
|||
|
"theta, logs = gradient_descent(cost, gradient, theta_start, X2, y)\n",
|
|||
|
"plot_fun(fig, polynomial_regression(theta), X)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Innym szczególnym przypadkiem regresji wielomianowej jest funkjca sześcienna:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Funkcja sześcienna:\n",
|
|||
|
"\n",
|
|||
|
"$$ h_{\\theta}(x) = \\theta_0 + \\theta_1 x + \\theta_2 x^2 + \\theta_3 x^3 $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 6,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[[ 397521.34262209]\n",
|
|||
|
" [-841360.50134775]\n",
|
|||
|
" [2253766.75764159]\n",
|
|||
|
" [-244049.33104935]]\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAoAAAAFkCAYAAACw8IoqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeXxU1f3/8deZrGRh3wOyBhBEQFFQcd8QFffijhZrtepXavut2vZX7bf1q1/aurQqXXDButG6UkWpFjeUfVF2E7YAIezLZF/m/P64MyaETDJJJrmzvJ+PRx4Tzr1z74dEkw/n3M/nGGstIiIiIhI/PG4HICIiIiKtSwmgiIiISJxRAigiIiISZ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7dRVF7FqQM6MbSnGj+LO5QARqkrT3ASwH99s5PySp/L0YiISCgqq3y8+NUWAG47XbN/4h4lgFFqWM+2DO6Wyf6icj77do/b4YiISAg+XFPAjoMl9O+SzlmDurodjsQxJYBRyhjz3SygloFFRKLDc/7Gz98/TY2fxV1KAKPYZSOzMAb+s243B4vL3Q5HRETqsWzrAVbkHaR9WtJ33RxE3KIEMIp1b5fKuIGdKa/y8d432hlERCSSPTd/EwA3jDlGjZ/FdUoAo1xgGfhtNYUWEYlY2/YX8+HqQOPnvm6HI6IEMNpdOKw7ackJLNt6gC17i9wOR0RE6vDiV07j50uP70k3NX6WCKAEMMqlJSdy0XE9AHhLs4AiIhHHW1rBrCXbAPi+Gj9LhFACGAOql4G1NZyISKSZtWQbhWWVnNK/E8dltXM7HBFACWBMGNu/Ez3apbJtfwlLtx5wOxwREfGrrPL
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 691.2x388.8 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"fig = plot_data(X3, y, xlabel='x', ylabel='y')\n",
|
|||
|
"theta_start = np.matrix([0, 0, 0, 0]).reshape(4, 1)\n",
|
|||
|
"theta, _ = gradient_descent(cost, gradient, theta_start, X3, y)\n",
|
|||
|
"plot_fun(fig, polynomial_regression(theta), X)\n",
|
|||
|
"\n",
|
|||
|
"print(theta)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Regresję wielomianową można potraktować jako szczególny przypadek regresji liniowej wielu zmiennych:\n",
|
|||
|
"\n",
|
|||
|
"$$ h_{\\theta}(x) = \\theta_0 + \\theta_1 x + \\theta_2 x^2 + \\theta_3 x^3 $$\n",
|
|||
|
"$$ x_1 = x, \\quad x_2 = x^2, \\quad x_3 = x^3, \\quad \\vec{x} = \\left[ \\begin{array}{ccc} x_0 \\\\ x_1 \\\\ x_2 \\end{array} \\right] $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"(W tym przypadku za kolejne cechy przyjmujemy kolejne potęgi zmiennej $x$)."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Uwaga praktyczna: przyda się normalizacja cech, szczególnie skalowanie!"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Do tworzenia cech „pochodnych” możemy używać nie tylko potęgowania, ale też innych operacji matematycznych, np.:\n",
|
|||
|
"\n",
|
|||
|
"$$ h_{\\theta}(x) = \\theta_0 + \\theta_1 x + \\theta_2 \\sqrt{x} $$\n",
|
|||
|
"$$ x_1 = x, \\quad x_2 = \\sqrt{x}, \\quad \\vec{x} = \\left[ \\begin{array}{ccc} x_0 \\\\ x_1 \\end{array} \\right] $$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Jakie zatem cechy wybrać? Najlepiej dopasować je do konkretnego problemu."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Wielomianowa regresja logistyczna\n",
|
|||
|
"\n",
|
|||
|
"Podobne modyfikacje cech możemy również stosować dla regresji logistycznej."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 7,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def powerme(x1,x2,n):\n",
|
|||
|
" \"\"\"Funkcja, która generuje n potęg dla zmiennych x1 i x2 oraz ich iloczynów\"\"\"\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)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 8,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"matrix([[ 1. , 0.36596696, -0.11214686],\n",
|
|||
|
" [ 0. , 0.4945305 , 0.47110656],\n",
|
|||
|
" [ 0. , 0.70290604, -0.92257983],\n",
|
|||
|
" [ 0. , 0.46658862, -0.62269739],\n",
|
|||
|
" [ 0. , 0.87939462, -0.11408015],\n",
|
|||
|
" [ 0. , -0.331185 , 0.84447667],\n",
|
|||
|
" [ 0. , -0.54351701, 0.8851383 ],\n",
|
|||
|
" [ 0. , 0.91979241, 0.41607012],\n",
|
|||
|
" [ 0. , 0.28011742, 0.61431157],\n",
|
|||
|
" [ 0. , 0.94754363, -0.78307311]])"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 8,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# Wczytanie danych\n",
|
|||
|
"import pandas\n",
|
|||
|
"import numpy as np\n",
|
|||
|
"\n",
|
|||
|
"alldata = pandas.read_csv('polynomial_logistic.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",
|
|||
|
"Xpl = powerme(data[:, 1], data[:, 2], n)\n",
|
|||
|
"Ypl = np.matrix(data[:, 0]).reshape(m, 1)\n",
|
|||
|
"\n",
|
|||
|
"data[:10]"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 9,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def plot_data_for_classification(X, Y, xlabel, ylabel):\n",
|
|||
|
" \"\"\"Wykres danych (wersja macierzowa)\"\"\"\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": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Przyjmijmy, że mamy następujące dane i chcemy przeprowadzić klasyfikację dwuklasową dla następujących klas:\n",
|
|||
|
" * czerwone krzyżyki\n",
|
|||
|
" * zielone kółka"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 10,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmwAAAFmCAYAAADQ5sbeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3df3Ac93nf8c8DiaCnIBKLlBLLlBhJJZpEUmZoCVHSmhM6tmXLcCtCjBLIURo6YUaTXzUjyqnocRpn1GQspzNiqNRtqiCJnYS1YCsARY+ZsrQkO6Op7QhSZIu0RwYsNzYDNVZEOz1CKUjxnv6xu8TicAccgLvd7+6+XzM3uPvuHvBd7N3ec999vs+auwsAAADh6sm7AwAAAFgaARsAAEDgCNgAAAACR8AGAAAQOAI2AACAwBGwAQAABO7ivDuQh0svvdSvuuqqvLsBAACwwNNPP/0P7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gz1oYDu43PwAgI2FNdqPmSBRumR2pXUpqI+FIAMUYcNxdT44ZguESIx0ob2rbY2FfWhAGSIHDYUExeSR6esNUdmpblvALAEcthQLsPDUVCWHslIRjzGxzkdhfatJUeG0/IAMkLAhmIiERV5W23uGwCsAjlsALAaRbwuI4DCImADgNVITsunc9ySoG3HjsxOy9fmaho7Oaapl6c0sGlAI9eNqH99fyZ/G0B2mHTQSRUs8AcgP09+40kNHRpS3euaPTervnV96rEeHb3zqLZv2Z539wCsApMOskAhTQAZqc3
|
|||
|
"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=r'$x_1$', ylabel=r'$x_2$')"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Propozycja hipotezy:\n",
|
|||
|
"\n",
|
|||
|
"$$ h_\\theta(x) = g(\\theta^T x) = g(\\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\theta_3 x_3 + \\theta_4 x_4 + \\theta_5 x_5) \\; , $$\n",
|
|||
|
"\n",
|
|||
|
"gdzie $g$ – funkcja logistyczna, $x_3 = x_1^2$, $x_4 = x_2^2$, $x_5 = x_1 x_2$."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 11,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def safeSigmoid(x, eps=0):\n",
|
|||
|
" \"\"\"Funkcja sigmoidalna zmodyfikowana w taki sposób, \n",
|
|||
|
" żeby wartości zawsz były odległe od asymptot o co najmniej eps\n",
|
|||
|
" \"\"\"\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",
|
|||
|
"def h(theta, X, eps=0.0):\n",
|
|||
|
" \"\"\"Funkcja hipotezy\"\"\"\n",
|
|||
|
" return safeSigmoid(X*theta, eps)\n",
|
|||
|
"\n",
|
|||
|
"def J(h,theta,X,y, lamb=0):\n",
|
|||
|
" \"\"\"Funkcja kosztu\"\"\"\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",
|
|||
|
"def dJ(h,theta,X,y,lamb=0):\n",
|
|||
|
" \"\"\"Pochodna funkcji kosztu\"\"\"\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",
|
|||
|
"def classifyBi(theta, X):\n",
|
|||
|
" \"\"\"Funkcja decyzji\"\"\"\n",
|
|||
|
" prob = h(theta, X)\n",
|
|||
|
" return prob"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 12,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def GD(h, fJ, fdJ, theta, X, y, alpha=0.01, eps=10**-3, maxSteps=10000):\n",
|
|||
|
" \"\"\"Metoda gradientu prostego dla regresji logistycznej\"\"\"\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": 13,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"theta = [[ 1.59558981]\n",
|
|||
|
" [ 0.12602307]\n",
|
|||
|
" [ 0.65718518]\n",
|
|||
|
" [-5.26367581]\n",
|
|||
|
" [ 1.96832544]\n",
|
|||
|
" [-6.97946065]]\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=10000)\n",
|
|||
|
"print(r'theta = {}'.format(theta))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 14,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def plot_decision_boundary(fig, theta, X):\n",
|
|||
|
" \"\"\"Wykres granicy klas\"\"\"\n",
|
|||
|
" ax = fig.axes[0]\n",
|
|||
|
" xx, yy = np.meshgrid(np.arange(-1.0, 1.0, 0.02),\n",
|
|||
|
" np.arange(-1.0, 1.0, 0.02))\n",
|
|||
|
" l = len(xx.ravel())\n",
|
|||
|
" C = powerme(xx.reshape(l, 1), yy.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], lw=3);"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 15,
|
|||
|
"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": "iVBORw0KGgoAAAANSUhEUgAAAmwAAAFmCAYAAADQ5sbeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAA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3gcrgJMOjMpoBaLl8eVwPAtRyVcqWSMTHRmNuSPmIjoiGlHhUQDsI2vREfbtTNaIfMxM74PVISJBd+nWrZsYgs0mMnq0CGD/6um6kdhsIunpZeP2tr086ell/w6uf5/0dN/FTeZWxdeZpcgiqatTZcKCCZK6OlUsRZYAB04UBMz2PlgBADLFQ+6iefKkxcUwCRuTEu+C8EVMfuT6/HFe+Dwi0l4Qvg96S9hYw6ZnYtIGur4iLjVxTqyFo6qSStbI8PVJ5H9B+DpjDZsRORvluj8ZvW0PNmwRQr4iVaiRYX8oIv/j++AZTNjIuKryJkvkznWktjLNRNkfiogCiH3YyJjc3xxdW4QAHGmjivM2exmwb09K8rzGLftDEVEAsYaNjIkLyZOvVLdGprK1b+WwllpRcPIUTp0sdFxO4fSpYpQUlTgupSgpLoXYzi6NpRyPFRYRhvDIcETUcF4iULN2DURdUOvMJTQstEpxEVHgeKthY8JGxhSEhaikQxWY+CIiKDhxCof3HMGhPUdxeM9RHNl7FHkHjuP44RM4fugEjh86ieOHTqAw/7Rfw61ZuwYuiK2DmMZ1Ua9xXcQ0sn+NbVYfjVrGonGrhohtVh9h4Tz5QqQVJmwumLA
|
|||
|
"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=r'$x_1$', ylabel=r'$x_2$')\n",
|
|||
|
"plot_decision_boundary(fig, theta, Xpl)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 16,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Wczytanie danych\n",
|
|||
|
"\n",
|
|||
|
"alldata = pandas.read_csv('polynomial_logistic.tsv', sep='\\t')\n",
|
|||
|
"data = np.matrix(alldata)\n",
|
|||
|
"\n",
|
|||
|
"m, n_plus_1 = data.shape\n",
|
|||
|
"Xn = data[:, 1:]\n",
|
|||
|
"\n",
|
|||
|
"n = 10\n",
|
|||
|
"Xpl = powerme(data[:, 1], data[:, 2], n)\n",
|
|||
|
"Ypl = np.matrix(data[:, 0]).reshape(m, 1)\n",
|
|||
|
"\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=10000)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 17,
|
|||
|
"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": "iVBORw0KGgoAAAANSUhEUgAAAmwAAAFmCAYAAADQ5sbeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3hT5RcH8O/bSSllD6FsKShDGRVREByAWESGaFXcKIo/ZargAgcIggLFrcWNUkeLqBVEVKQIQhkqZbWsUgq0UEZaSkdyf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QuuwiBxjY16vxho2PfLVxIYzoMhHpGSmIGZhDExiQkFJAcKDwxGgApA8Ihm9mvfSOjyiirE+WtdYw+ZNlDqbuFgb6Fa03VtwOJ58gKHIgJiFMTAUG1BQUgAAKCgpgKHYvD2/OF/jCIkq4G310baN5J3Z7sOYsOmZrxWIWofjrUlbQED5U79EOpeQlgCTmOzeZhITErYkeDgiIid5Y320r70PXgAmbHrmiyNSXCePvFz60fQzI2tlFZQUICMvw8MRETnJUX209X1Gj8mPL74PVhEnHeiZLxaIOhqO99bfh/xOVL0ohAeH203awoPD0aZuGw2iInLCkCHmWmHbOmjr+0yfPvpMfnzxfbCKOOnAG/hKgWhF6+T54YuPdKKSk3wMRQZEzomEodhQ7kdFhEQge2I2aoTU8ETkRP7DV94HncBJB97K2wpEK+LK4XgWopKrVLJGJiI0AskjkhEREoHw4HAA5pG1iBDzdiZrRC7mS++DF0JE/O7SrVs38Qo
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 691.2x388.8 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"# Przykład dla większej liczby cech\n",
|
|||
|
"fig = plot_data_for_classification(Xpl, Ypl, xlabel=r'$x_1$', ylabel=r'$x_2$')\n",
|
|||
|
"plot_decision_boundary(fig, theta, Xpl)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## 5.2. Problem nadmiernego dopasowania"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Obciążenie a wariancja"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 18,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Dane do prostego przykładu\n",
|
|||
|
"\n",
|
|||
|
"data = np.matrix([\n",
|
|||
|
" [0.0, 0.0],\n",
|
|||
|
" [0.5, 1.8],\n",
|
|||
|
" [1.0, 4.8],\n",
|
|||
|
" [1.6, 7.2],\n",
|
|||
|
" [2.6, 8.8],\n",
|
|||
|
" [3.0, 9.0],\n",
|
|||
|
" ])\n",
|
|||
|
"\n",
|
|||
|
"m, n_plus_1 = data.shape\n",
|
|||
|
"n = n_plus_1 - 1\n",
|
|||
|
"Xn1 = data[:, 0:n]\n",
|
|||
|
"Xn1 /= np.amax(Xn1, axis=0)\n",
|
|||
|
"Xn2 = np.power(Xn1, 2) \n",
|
|||
|
"Xn2 /= np.amax(Xn2, axis=0)\n",
|
|||
|
"Xn3 = np.power(Xn1, 3) \n",
|
|||
|
"Xn3 /= np.amax(Xn3, axis=0)\n",
|
|||
|
"Xn4 = np.power(Xn1, 4) \n",
|
|||
|
"Xn4 /= np.amax(Xn4, axis=0)\n",
|
|||
|
"Xn5 = np.power(Xn1, 5) \n",
|
|||
|
"Xn5 /= np.amax(Xn5, axis=0)\n",
|
|||
|
"\n",
|
|||
|
"X1 = np.matrix(np.concatenate((np.ones((m, 1)), Xn1), axis=1)).reshape(m, n + 1)\n",
|
|||
|
"X2 = np.matrix(np.concatenate((np.ones((m, 1)), Xn1, Xn2), axis=1)).reshape(m, 2 * n + 1)\n",
|
|||
|
"X5 = np.matrix(np.concatenate((np.ones((m, 1)), Xn1, Xn2, Xn3, Xn4, Xn5), axis=1)).reshape(m, 5 * n + 1)\n",
|
|||
|
"y = np.matrix(data[:, -1]).reshape(m, 1)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 19,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmAAAAFoCAYAAADw0EcgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAUqUlEQVR4nO3df6zd933X8dfbcbuu915YSrw1azva4at2o3/QYapuvUJVu4rMTA2MTjeTtqWTp2iI0g5PsDLQKk0ICkKDgcYgpKUblPZWWcXCMIzSrhoWLIqTBdrUVPdSWGucLe6GupvLoMvuhz/O9ey518l14vv+2vc8HpJ17j3n3HPe+fp7b57+/ro1xggAAH0OTT0AAMC8EWAAAM0EGABAMwEGANBMgAEANBNgAADN9i3Aqup9VfVEVX3qsvteVFUfrar1ndtb9+v9AQBuVPu5Bez9Se644r53JfnYGGM5ycd2PgcAmCu1nxdiraqXJ/n5Mcardz7/TJI3jDEer6rbk3xijPHKfRsAAOAG1H0M2NeMMR5Pkp3br25+fwCAyR2eeoCrqap7ktyTJAsLC3/8Va961cQTAQD8fg8//PAXxhhHrvXrugPs16vq9st2QT5xtSeOMe5Ncm+SHDt2bJw5c6ZrRgCAPamqX302X9e9C/KBJHfvfHx3kp9rfn8AgMnt52UoPpjkPyd5ZVWdq6oTSd6T5M1VtZ7kzTufAwDMlX3bBTnG+K6rPPSm/XpPAICbgSvhAwA0E2AAAM0EGABAMwEGANBMgAEANBNgAADNBBgAQDMBBgDQTIABADQTYAAAzQQYAEAzAQYA0EyAAQA0E2AAAM0EGABAMwEGANBMgAEANBNgAADNBBgAQLPDUw8AwAG0uZmsrSXr68nyc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|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"fig = plot_data(X1, y, xlabel='x', ylabel='y')"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 20,
|
|||
|
"metadata": {
|
|||
|
"scrolled": true,
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"[<matplotlib.lines.Line2D at 0x7f32473f2ad0>]"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 20,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmAAAAFoCAYAAADw0EcgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dd3yW9b3/8fcnG5IQVtgbwh6JUkdFrVhH0YqKmPjoLqe257QHFOuqVq2jFheipz39cbrOOe0hEUVFxVFHq2gdaMIeCTuEEVZIApn39/fHHZVigCTkvq57vJ6PB4+Qe11vL6/cvHONz23OOQEAAMA7cX4HAAAAiDUUMAAAAI9RwAAAADxGAQMAAPAYBQwAAMBjFDAAAACPhayAmdkfzGyPma066rauZvZXMytu+tolVMsHAAAIV6HcA/YnSZcec9ttkt5wzmVJeqPpewAAgJhioRzEamaDJL3onBvb9P16SV9xzu00s96S/uacGxGyAAAAAGHI63PAejrndkpS09ceHi8fAADAdwl+BzgeM7te0vWSlJqaevrIkSN9TgQAaI2t+w7rUE29UpPi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Q9ShgAGLX0qXSlClSICBVV0upqdLs2dKSJdKkSZ5EWLWjQg+9+unIifHq0SnFk+UC8BeHIAHEpsrKYPmqrAyWLyn49dPbq6pCHuFwXYNm5heqvtHpW2cN1EWje4Z8mQDCAwUMQGwqKAju+WpOIBC8P8Tue3GtNpVXK6tHmu64bFTIlwcgfFDAAMSm4uLP93wdq7paKik
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 691.2x388.8 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"fig = plot_data(X1, y, xlabel='x', ylabel='y')\n",
|
|||
|
"theta_start = np.matrix([0, 0]).reshape(2, 1)\n",
|
|||
|
"theta, _ = gradient_descent(cost, gradient, theta_start, X1, y, eps=0.00001)\n",
|
|||
|
"plot_fun(fig, polynomial_regression(theta), X1)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Ten model ma duże **obciążenie** (**błąd systematyczny**, _bias_) – zachodzi **niedostateczne dopasowanie** (_underfitting_)."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 21,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"[<matplotlib.lines.Line2D at 0x7f3247374bd0>]"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 21,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png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Glz/xtR76NE9+piSxHwoYAISBQMDqn5/madJzuaqortcZx3TQWzcer6x0to4IRZ1bt9Ar143UjSf1lJX0949X6+qn56i4otp1NIQIChgAhLiK6jpd/+J83f/xaknSrWf00aNXDFVKAjuwh7LYGJ9uPaOvnvvJcLVLidfs/O0a/+AX+iKvxHU0hAAKGACEsPziSp0/ebY+Xr5VqYmxevqaYbrxpGzWe4WRE3una9rNJ2h0z7baVlmrq5+eq//7cJXq/QHX0eAQBQwAQtTHy7fq/MmztaakSr07pOjdm8bopL7tXcfCEWjfMlEvTBqhX53aW0bSw9PzdfkTX2tz2W7X0eAIBQwAQkwgYPXAx6v1X8/nqrKmXuMHdtR/fn68Mtslu46GoxDjM7rl1F566dqRap+aoHnrSjX+wS/02cqtrqPBAQoYAISQ8uo6XfdCrh78NE/GSL87s48m/3iIklnvFTFG9Wyr9285QWN7p6t0V51++myu/jJ1OVdJRhkKGACEiPziCp3/8Gx9sqJYLRNj9czEYfr5uGwOeI5AbVMS9MzEYbrtrL6K8Rk98UWBbn19ESUsivArFQCEgA+XbdGvpyxUVa1ffTum6l9XDVX3tkw5RjKfz+hnY3tqUJfWmvTcPL25YJMk6W8XD1IMF1lEPCcjYMaYXxljlhljlhpjXjbGJLrIAQCuBQJWf/9ola5/Yb6qav06+9hOevPnoylfUWRUz7Z6ZuIwJcXH6M0FmxgJixKeFzBjTIakmyXlWGsHSIqRdJnXOQDAtTp/QDf+e4Ee+ixfPiP94ay+evjywUqKZ3I
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 691.2x388.8 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"fig = plot_data(X2, y, xlabel='x', ylabel='y')\n",
|
|||
|
"theta_start = np.matrix([0, 0, 0]).reshape(3, 1)\n",
|
|||
|
"theta, _ = gradient_descent(cost, gradient, theta_start, X2, y, eps=0.000001)\n",
|
|||
|
"plot_fun(fig, polynomial_regression(theta), X1)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Ten model jest odpowiednio dopasowany."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 22,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"[<matplotlib.lines.Line2D at 0x7f32bab3cad0>]"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 22,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmAAAAFoCAYAAADw0EcgAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dd3zV1eH/8fe5mWQRICHsmQACskSGoFVxUkerVrQOVNS2vzqodljbandrh35rh1XBuhXrHtSFoqKyQYaMhB0SSELInjf3/P64UREZCSSfc8fr+XjwSHLvTe7bj5fLO+dzzvkYa60AAADgHZ/rAAAAANGGAgYAAOAxChgAAIDHKGAAAAAeo4ABAAB4jAIGAADgsXYrYMaYh4wxRcaYNfvc1tkY85YxJrf5Y6f2en4AAIBQ1Z4jYA9LOmu/226TNM9amyNpXvPXAAAAUcW050asxph+kl611g5v/nqDpJOttYXGmO6S5ltrB7dbAAAAgBDk9RywLG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DMwbphlNzXEcCEEacbMRqrb1T0p0unhsAjta7G4r00+dXS5JunzpE15800HEiAOGGzWkAoBXKaxp123OrJEm3nD6I8gXgiFDAAKAVfv3qp9pdUa8xfdL1/VOyXccBEKYoYADQQm9/ulvPLc9XQqxPf/nWSDZZBXDEKGAA0AJlNQ26/YXgvK8fnTlYAzJTHCcCEM4oYADQAr965VMVVdbr+H6ddPWk/q7jAAhzFDAAOIw31u7SCyt2KjHOpz9fxKl
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 691.2x388.8 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"fig = plot_data(X5, y, xlabel='x', ylabel='y')\n",
|
|||
|
"theta_start = np.matrix([0, 0, 0, 0, 0, 0]).reshape(6, 1)\n",
|
|||
|
"theta, _ = gradient_descent(cost, gradient, theta_start, X5, y, alpha=0.5, eps=10**-7)\n",
|
|||
|
"plot_fun(fig, polynomial_regression(theta), X1)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Ten model ma dużą **wariancję** (_variance_) – zachodzi **nadmierne dopasowanie** (_overfitting_)."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"(Zwróć uwagę na dziwny kształt krzywej w lewej części wykresu – to m.in. efekt nadmiernego dopasowania)."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Nadmierne dopasowanie występuje, gdy model ma zbyt dużo stopni swobody w stosunku do ilości danych wejściowych.\n",
|
|||
|
"\n",
|
|||
|
"Jest to zjawisko niepożądane.\n",
|
|||
|
"\n",
|
|||
|
"Możemy obrazowo powiedzieć, że nadmierne dopasowanie występuje, gdy model zaczyna modelować szum/zakłócenia w danych zamiast ich „głównego nurtu”. "
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"Zobacz też: https://pl.wikipedia.org/wiki/Nadmierne_dopasowanie"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"<img style=\"margin:auto\" width=\"90%\" src=\"fit.png\"/>"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Obciążenie (błąd systematyczny, _bias_)\n",
|
|||
|
"\n",
|
|||
|
"* Wynika z błędnych założeń co do algorytmu uczącego się.\n",
|
|||
|
"* Duże obciążenie powoduje niedostateczne dopasowanie."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Wariancja (_variance_)\n",
|
|||
|
"\n",
|
|||
|
"* Wynika z nadwrażliwości na niewielkie fluktuacje w zbiorze uczącym.\n",
|
|||
|
"* Wysoka wariancja może spowodować nadmierne dopasowanie (modelując szum zamiast sygnału)."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"<img style=\"margin:auto\" width=\"60%\" src=\"bias2.png\"/>"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"<img style=\"margin:auto\" width=\"60%\" src=\"curves.jpg\"/>"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## 5.3. Regularyzacja"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 58,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def SGD(h, fJ, fdJ, theta, X, Y, \n",
|
|||
|
" alpha=0.001, maxEpochs=1.0, batchSize=100, \n",
|
|||
|
" adaGrad=False, logError=False, validate=0.0, valStep=100, lamb=0, trainsetsize=1.0):\n",
|
|||
|
" \"\"\"Stochastic Gradient Descent - stochastyczna wersja metody gradientu prostego\n",
|
|||
|
" (więcej na ten temat na wykładzie 11)\n",
|
|||
|
" \"\"\"\n",
|
|||
|
" errorsX, errorsY = [], []\n",
|
|||
|
" errorsVX, errorsVY = [], []\n",
|
|||
|
" \n",
|
|||
|
" XT, YT = X, Y\n",
|
|||
|
" \n",
|
|||
|
" m_end=int(trainsetsize*len(X))\n",
|
|||
|
" \n",
|
|||
|
" if validate > 0:\n",
|
|||
|
" mv = int(X.shape[0] * validate)\n",
|
|||
|
" XV, YV = X[:mv], Y[:mv] \n",
|
|||
|
" XT, YT = X[mv:m_end], Y[mv:m_end] \n",
|
|||
|
" m, n = XT.shape\n",
|
|||
|
"\n",
|
|||
|
" start, end = 0, batchSize\n",
|
|||
|
" maxSteps = (m * float(maxEpochs)) / batchSize\n",
|
|||
|
" \n",
|
|||
|
" if adaGrad:\n",
|
|||
|
" hgrad = np.matrix(np.zeros(n)).reshape(n,1)\n",
|
|||
|
" \n",
|
|||
|
" for i in range(int(maxSteps)):\n",
|
|||
|
" XBatch, YBatch = XT[start:end,:], YT[start:end,:]\n",
|
|||
|
"\n",
|
|||
|
" grad = fdJ(h, theta, XBatch, YBatch, lamb=lamb)\n",
|
|||
|
" if adaGrad:\n",
|
|||
|
" hgrad += np.multiply(grad, grad)\n",
|
|||
|
" Gt = 1.0 / (10**-7 + np.sqrt(hgrad))\n",
|
|||
|
" theta = theta - np.multiply(alpha * Gt, grad)\n",
|
|||
|
" else:\n",
|
|||
|
" theta = theta - alpha * grad\n",
|
|||
|
" \n",
|
|||
|
" if logError:\n",
|
|||
|
" errorsX.append(float(i*batchSize)/m)\n",
|
|||
|
" errorsY.append(fJ(h, theta, XBatch, YBatch).item())\n",
|
|||
|
" if validate > 0 and i % valStep == 0:\n",
|
|||
|
" errorsVX.append(float(i*batchSize)/m)\n",
|
|||
|
" errorsVY.append(fJ(h, theta, XV, YV).item())\n",
|
|||
|
" \n",
|
|||
|
" if start + batchSize < m:\n",
|
|||
|
" start += batchSize\n",
|
|||
|
" else:\n",
|
|||
|
" start = 0\n",
|
|||
|
" end = min(start + batchSize, m)\n",
|
|||
|
" return theta, (errorsX, errorsY, errorsVX, errorsVY)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 59,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"# Przygotowanie danych do przykładu regularyzacji\n",
|
|||
|
"\n",
|
|||
|
"n = 6\n",
|
|||
|
"\n",
|
|||
|
"data = np.matrix(np.loadtxt(\"ex2data2.txt\", delimiter=\",\"))\n",
|
|||
|
"np.random.shuffle(data)\n",
|
|||
|
"\n",
|
|||
|
"X = powerme(data[:,0], data[:,1], n)\n",
|
|||
|
"Y = data[:,2]"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 60,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def draw_regularization_example(X, Y, lamb=0, alpha=1, adaGrad=True, maxEpochs=2500, validate=0.25):\n",
|
|||
|
" \"\"\"Rusuje przykład regularyzacji\"\"\"\n",
|
|||
|
" plt.figure(figsize=(16,8))\n",
|
|||
|
" plt.subplot(121)\n",
|
|||
|
" plt.scatter(X[:, 2].tolist(), X[:, 1].tolist(),\n",
|
|||
|
" c=Y.tolist(),\n",
|
|||
|
" s=100, cmap=plt.cm.get_cmap('prism'));\n",
|
|||
|
"\n",
|
|||
|
" theta = np.matrix(np.zeros(X.shape[1])).reshape(X.shape[1],1)\n",
|
|||
|
" thetaBest, err = SGD(h, J, dJ, theta, X, Y, alpha=alpha, adaGrad=adaGrad, maxEpochs=maxEpochs, batchSize=100, \n",
|
|||
|
" logError=True, validate=validate, valStep=1, lamb=lamb)\n",
|
|||
|
"\n",
|
|||
|
" xx, yy = np.meshgrid(np.arange(-1.5, 1.5, 0.02),\n",
|
|||
|
" np.arange(-1.5, 1.5, 0.02))\n",
|
|||
|
" l = len(xx.ravel())\n",
|
|||
|
" C = powerme(xx.reshape(l, 1),yy.reshape(l, 1), n)\n",
|
|||
|
" z = classifyBi(thetaBest, C).reshape(int(np.sqrt(l)), int(np.sqrt(l)))\n",
|
|||
|
"\n",
|
|||
|
" plt.contour(xx, yy, z, levels=[0.5], lw=3);\n",
|
|||
|
" plt.ylim(-1,1.2);\n",
|
|||
|
" plt.xlim(-1,1.2);\n",
|
|||
|
" plt.legend();\n",
|
|||
|
" plt.subplot(122)\n",
|
|||
|
" plt.plot(err[0],err[1], lw=3, label=\"Training error\")\n",
|
|||
|
" if validate > 0:\n",
|
|||
|
" plt.plot(err[2],err[3], lw=3, label=\"Validation error\");\n",
|
|||
|
" plt.legend()\n",
|
|||
|
" plt.ylim(0.2,0.8);"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 61,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stderr",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"/home/pawel/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:19: UserWarning: The following kwargs were not used by contour: 'lw'\n",
|
|||
|
"No handles with labels found to put in legend.\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7AAAAHWCAYAAABOj2WsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3iT1RfA8e9tm6aDAmXvIRtKgVKKCLIpgoCKgPCTpUAVZQiIIoogrjJEZIigDEGGyBJUQGSKbGSDbJBSZENL93h/f7yMjqRJ27Rpyvk8Tx/aNzf3noRCcnLvPVdpmoYQQgghhBBCCJHTOdk7ACGEEEIII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mSJcmuLVmyhG7dull1/xIlSrBs2bIMj58ygf3tt9/Inz9/hvtLj/j4+DR/NichISFT40oCK4SwLR8fWLsW8uYFT89H15XSf65bV19m/biciyuEEEKIXKtTp0788ssvxMTEAHDhwgVCQ0Np1KjRw3NZ/fz8qFmzJj///HOq+1+4cAEfHx8AoqKi6Nq1K76+vrz00ktERUU9bNe/f3/8/f2pUaMGo0ePBmDKlCmEhobSrFkzmjVrBkC5cuW4ceMGAJMmTcLHxwcfHx8mT578cLxq1arRr18/atSoQWBgYLJxHrh+/Tovvvgi9erVo169evz1118AjBkzhqCgIAIDA+nZsyfz5s2jc+fOtG/fnsDAQDRNY/jw4fj4+FCzZk1+vF+0c8uWLTRr1oz//e9/1KxZM1PP+WO2vlEIkS0aN4bQUFi0CH74Ae7dg0q
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 1152x576 with 2 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"draw_regularization_example(X, Y)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Regularyzacja\n",
|
|||
|
"\n",
|
|||
|
"Regularyzacja jest metodą zapobiegania zjawisku nadmiernego dopasowania (*overfitting*) poprzez odpowiednie zmodyfikowanie funkcji kosztu.\n",
|
|||
|
"\n",
|
|||
|
"Do funkcji kosztu dodawane jest specjalne wyrażenie (**wyrazenie regularyzacyjne** – zaznaczone na czerwono w poniższych wzorach), będące „karą” za ekstremalne wartości parametrów $\\theta$.\n",
|
|||
|
"\n",
|
|||
|
"W ten sposób preferowane są wektory $\\theta$ z mniejszymi wartosciami parametrów – mają automatycznie niższy koszt.\n",
|
|||
|
"\n",
|
|||
|
"Jak silną regularyzację chcemy zastosować? Możemy o tym zadecydować, dobierajac odpowiednio **parametr regularyzacji** $\\lambda$."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Regularyzacja dla regresji liniowej – funkcja kosztu\n",
|
|||
|
"\n",
|
|||
|
"$$\n",
|
|||
|
"J(\\theta) \\, = \\, \\dfrac{1}{2m} \\left( \\displaystyle\\sum_{i=1}^{m} h_\\theta(x^{(i)}) - y^{(i)} \\color{red}{ + \\lambda \\displaystyle\\sum_{j=1}^{n} \\theta^2_j } \\right)\n",
|
|||
|
"$$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "fragment"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"* $\\lambda$ – parametr regularyzacji\n",
|
|||
|
"* jeżeli $\\lambda$ jest zbyt mały, skutkuje to nadmiernym dopasowaniem\n",
|
|||
|
"* jeżeli $\\lambda$ jest zbyt duży, skutkuje to niedostatecznym dopasowaniem"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Regularyzacja dla regresji liniowej – gradient\n",
|
|||
|
"\n",
|
|||
|
"$$\\small\n",
|
|||
|
"\\begin{array}{llll}\n",
|
|||
|
"\\dfrac{\\partial J(\\theta)}{\\partial \\theta_0} &=& \\dfrac{1}{m}\\displaystyle\\sum_{i=1}^m \\left( h_{\\theta}(x^{(i)})-y^{(i)} \\right) x^{(i)}_0 & \\textrm{dla $j = 0$ }\\\\\n",
|
|||
|
"\\dfrac{\\partial J(\\theta)}{\\partial \\theta_j} &=& \\dfrac{1}{m}\\displaystyle\\sum_{i=1}^m \\left( h_{\\theta}(x^{(i)})-y^{(i)} \\right) x^{(i)}_j \\color{red}{+ \\dfrac{\\lambda}{m}\\theta_j} & \\textrm{dla $j = 1, 2, \\ldots, n $} \\\\\n",
|
|||
|
"\\end{array} \n",
|
|||
|
"$$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Regularyzacja dla regresji logistycznej – funkcja kosztu\n",
|
|||
|
"\n",
|
|||
|
"$$\n",
|
|||
|
"\\begin{array}{rtl}\n",
|
|||
|
"J(\\theta) & = & -\\dfrac{1}{m} \\left( \\displaystyle\\sum_{i=1}^{m} y^{(i)} \\log h_\\theta(x^{(i)}) + \\left( 1-y^{(i)} \\right) \\log \\left( 1-h_\\theta(x^{(i)}) \\right) \\right) \\\\\n",
|
|||
|
"& & \\color{red}{ + \\dfrac{\\lambda}{2m} \\displaystyle\\sum_{j=1}^{n} \\theta^2_j } \\\\\n",
|
|||
|
"\\end{array}\n",
|
|||
|
"$$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Regularyzacja dla regresji logistycznej – gradient\n",
|
|||
|
"\n",
|
|||
|
"$$\\small\n",
|
|||
|
"\\begin{array}{llll}\n",
|
|||
|
"\\dfrac{\\partial J(\\theta)}{\\partial \\theta_0} &=& \\dfrac{1}{m}\\displaystyle\\sum_{i=1}^m \\left( h_{\\theta}(x^{(i)})-y^{(i)} \\right) x^{(i)}_0 & \\textrm{dla $j = 0$ }\\\\\n",
|
|||
|
"\\dfrac{\\partial J(\\theta)}{\\partial \\theta_j} &=& \\dfrac{1}{m}\\displaystyle\\sum_{i=1}^m \\left( h_{\\theta}(x^{(i)})-y^{(i)} \\right) x^{(i)}_j \\color{red}{+ \\dfrac{\\lambda}{m}\\theta_j} & \\textrm{dla $j = 1, 2, \\ldots, n $} \\\\\n",
|
|||
|
"\\end{array} \n",
|
|||
|
"$$"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Implementacja metody regularyzacji"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 62,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def J_(h,theta,X,y,lamb=0):\n",
|
|||
|
" \"\"\"Funkcja kosztu z regularyzacją\"\"\"\n",
|
|||
|
" m = float(len(y))\n",
|
|||
|
" f = h(theta, X, eps=10**-7)\n",
|
|||
|
" j = 1.0/m \\\n",
|
|||
|
" * -np.sum(np.multiply(y, np.log(f)) + \n",
|
|||
|
" np.multiply(1 - y, np.log(1 - f)), axis=0) \\\n",
|
|||
|
" + lamb/(2*m) * np.sum(np.power(theta[1:] ,2))\n",
|
|||
|
" return j\n",
|
|||
|
"\n",
|
|||
|
"def dJ_(h,theta,X,y,lamb=0):\n",
|
|||
|
" \"\"\"Gradient funkcji kosztu z regularyzacją\"\"\"\n",
|
|||
|
" m = float(y.shape[0])\n",
|
|||
|
" g = 1.0/y.shape[0]*(X.T*(h(theta,X)-y))\n",
|
|||
|
" g[1:] += lamb/m * theta[1:]\n",
|
|||
|
" return g"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 63,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"slider_lambda = widgets.FloatSlider(min=0.0, max=0.5, step=0.005, value=0.01, description=r'$\\lambda$', width=300)\n",
|
|||
|
"\n",
|
|||
|
"def slide_regularization_example_2(lamb):\n",
|
|||
|
" draw_regularization_example(X, Y, lamb=lamb)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 64,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"application/vnd.jupyter.widget-view+json": {
|
|||
|
"model_id": "e0246912b4c349958ea4f5391b3ae44f",
|
|||
|
"version_major": 2,
|
|||
|
"version_minor": 0
|
|||
|
},
|
|||
|
"text/plain": [
|
|||
|
"interactive(children=(FloatSlider(value=0.01, description='$\\\\lambda$', max=0.5, step=0.005), Button(descripti…"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {},
|
|||
|
"output_type": "display_data"
|
|||
|
},
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"<function __main__.slide_regularization_example_2(lamb)>"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 64,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"widgets.interact_manual(slide_regularization_example_2, lamb=slider_lambda)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 65,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def cost_lambda_fun(lamb):\n",
|
|||
|
" \"\"\"Koszt w zależności od parametru regularyzacji lambda\"\"\"\n",
|
|||
|
" theta = np.matrix(np.zeros(X.shape[1])).reshape(X.shape[1],1)\n",
|
|||
|
" thetaBest, err = SGD(h, J, dJ, theta, X, Y, alpha=1, adaGrad=True, maxEpochs=2500, batchSize=100, \n",
|
|||
|
" logError=True, validate=0.25, valStep=1, lamb=lamb)\n",
|
|||
|
" return err[1][-1], err[3][-1]\n",
|
|||
|
"\n",
|
|||
|
"def plot_cost_lambda():\n",
|
|||
|
" \"\"\"Wykres kosztu w zależności od parametru regularyzacji lambda\"\"\"\n",
|
|||
|
" plt.figure(figsize=(16,8))\n",
|
|||
|
" ax = plt.subplot(111)\n",
|
|||
|
" Lambda = np.arange(0.0, 1.0, 0.01)\n",
|
|||
|
" Costs = [cost_lambda_fun(lamb) for lamb in Lambda]\n",
|
|||
|
" CostTrain = [cost[0] for cost in Costs]\n",
|
|||
|
" CostCV = [cost[1] for cost in Costs]\n",
|
|||
|
" plt.plot(Lambda, CostTrain, lw=3, label='training error')\n",
|
|||
|
" plt.plot(Lambda, CostCV, lw=3, label='validation error')\n",
|
|||
|
" ax.set_xlabel(r'$\\lambda$')\n",
|
|||
|
" ax.set_ylabel(u'cost')\n",
|
|||
|
" plt.legend()\n",
|
|||
|
" plt.ylim(0.2,0.8)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 66,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7AAAAHmCAYAAABK9WIBAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tY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c9vrNN9+sSCSihoYG/a//9b+0cePGtI/hwQcf1PPPP6/169fr5z//uRYuXKhgMDhqn+rqaj3++OO6++671dDQoI0bNyYuAnU2Dz/8sB577DE1NDToBz/4gb75zW+m/WeYCjOZKefZpLGx0W7dunWmhwEAAABghu3evVurVq2a6WHMqOHhYfl8PuXm5mrz5s361Kc+lbiolBeM92dojNlmrW0cb38OIQYAAAAAjzp27JjuvPNOxWIx5efn69FHH53pIWUUAQsAAAAAHrVixQq99dZbMz2MrOEcWAAAAACAJxCwAAAAAABPIGABAAAAAJ5AwAIAAAAAPIGABQAAAIAsKS4uliQ1Nzfrwx/+8Lj7XHfddTrbrUO/8Y1vKBQKJbZvueUWdXd3p2+gsxQBCwAAAABZtmjRIj399NPTfv/YgH3uuedUVlaWjqGdVSQSmXD7TKLR6Dl/NwELAAAAANPw2c9+Vt/5zncS21/60pf0ta99Tf39/brhhhu0fv16rV27Vj/72c9Oe++RI0e0Zs0aSdLg4KDuuusuNTQ06CMf+YgGBwcT+33qU59SY2OjVq9erQcffFCS9PDDD6u5uVnXX3+9rr/+eknS0qVL1d7eLkn6+te/rjVr1mjNmjX6xje+kfi+VatW6d5779Xq1at10003jfqeuLa2Nn3oQx/S5Zdfrssvv1yvvfZa4me77777dNNNN+kTn/iEHn/8cd1xxx36wAc+oJtuuknWWj3wwANas2aN1q5dq3/913+VJL344ou6/vrr9dGPflRr164953/m3AcWAAAAgPd9qTSDn90z7tN33XWX/vzP/1x/8id/Ikl66qmn9Itf/EKFhYV65plnVFJSovb2dm3cuFG33nqrjDHjfs4//uM/qqioSDt27NCOHTu0fv36xGt/+7d/q4qKCkWjUd1www3asWOH7r//fn3961/XCy+8oKqqqlGftW3bNj322GP6zW9+I2utrrjiCl177bUqLy/X/v379eSTT+rRRx/VnXfeqZ/85Cf62Mc+Nur9f/Znf6bPfOYzuvrqq3Xs2DG9733v0+7duxOf/eqrr8rv9+vxxx/X5s2btWPHDlVUVOgnP/mJtm/frrffflvt7e26/PLLdc0110iS3njjDe3cuVP19fXT++efgoAFAAAAgGm49NJL1draqubmZrW1tam8vFyLFy/WyMiIvvCFL+jll19WTk6OTpw4oZaWFi1YsGDcz3n55Zd1//33S5IaGhrU0NCQeO2pp57SI488okgkopMnT2rXrl2jXh/r1Vdf1Qc/+EEFAgFJ0u23365XXnlFt956q+rr67Vu3TpJ0mWXXaYjR46c9v5NmzZp165die3e3l719fVJkm699Vb5/f7EazfeeKMqKioS33v33XfL5/Np/vz5uvbaa7VlyxaVlJRow4YNaYlXiYAFAAAAgGn78Ic/rKefflqnTp3SXXfdJUl64okn1NbWpm3btikvL09Lly7V0NDQhJ8z3uzs4cOH9dWvflVbtmxReXm57rnnnrN+jrX2jK8VFBQk1n0+37iHEMdiMW3evHlUqMbFo3i87Ym+d+z7zgUBCwAAAMD7znCYb6bddddduvfee9Xe3q6XXnpJktTT06N58+YpLy9PL7zwgo4ePTrhZ1xzzTV64okndP3112vnzp3asWOHJGf2MxAIqLS0VC0tLfr5z3+u6667TpI
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 1152x576 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plot_cost_lambda()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "slide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## 5.4. Krzywa uczenia się"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"* Krzywa uczenia pozwala sprawdzić, czy uczenie przebiega poprawnie.\n",
|
|||
|
"* Krzywa uczenia to wykres zależności między wielkością zbioru treningowego a wartością funkcji kosztu.\n",
|
|||
|
"* Wraz ze wzrostem wielkości zbioru treningowego wartość funkcji kosztu na zbiorze treningowym rośnie.\n",
|
|||
|
"* Wraz ze wzrostem wielkości zbioru treningowego wartość funkcji kosztu na zbiorze walidacyjnym maleje."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 67,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "notes"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def cost_trainsetsize_fun(m):\n",
|
|||
|
" \"\"\"Koszt w zależności od wielkości zbioru uczącego\"\"\"\n",
|
|||
|
" theta = np.matrix(np.zeros(X.shape[1])).reshape(X.shape[1],1)\n",
|
|||
|
" thetaBest, err = SGD(h, J, dJ, theta, X, Y, alpha=1, adaGrad=True, maxEpochs=2500, batchSize=100, \n",
|
|||
|
" logError=True, validate=0.25, valStep=1, lamb=0.01, trainsetsize=m)\n",
|
|||
|
" return err[1][-1], err[3][-1]\n",
|
|||
|
"\n",
|
|||
|
"def plot_learning_curve():\n",
|
|||
|
" \"\"\"Wykres krzywej uczenia się\"\"\"\n",
|
|||
|
" plt.figure(figsize=(16,8))\n",
|
|||
|
" ax = plt.subplot(111)\n",
|
|||
|
" M = np.arange(0.3, 1.0, 0.05)\n",
|
|||
|
" Costs = [cost_trainsetsize_fun(m) for m in M]\n",
|
|||
|
" CostTrain = [cost[0] for cost in Costs]\n",
|
|||
|
" CostCV = [cost[1] for cost in Costs]\n",
|
|||
|
" plt.plot(M, CostTrain, lw=3, label='training error')\n",
|
|||
|
" plt.plot(M, CostCV, lw=3, label='validation error')\n",
|
|||
|
" ax.set_xlabel(u'trainset size')\n",
|
|||
|
" ax.set_ylabel(u'cost')\n",
|
|||
|
" plt.legend()"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"### Krzywa uczenia a obciążenie i wariancja\n",
|
|||
|
"\n",
|
|||
|
"Wykreślenie krzywej uczenia pomaga diagnozować nadmierne i niedostateczne dopasowanie:\n",
|
|||
|
"\n",
|
|||
|
"<img width=\"100%\" src=\"learning-curves.png\"/>\n",
|
|||
|
"\n",
|
|||
|
"Źródło: http://www.ritchieng.com/machinelearning-learning-curve"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 68,
|
|||
|
"metadata": {
|
|||
|
"slideshow": {
|
|||
|
"slide_type": "subslide"
|
|||
|
}
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7AAAAHgCAYAAACcrIEcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeXyU5b3///eVyb4ACQTZSQBRBIKs4lJWF7RH675VLV2kte3xtP3Vanu+rT329PdtrcdWT2s96lHOsVZrXVpP64psoqAsKgdBMOxhTSAJgewz1/ePe5KZLJOEMDP3LK/n4zGPXPcyM58EE/POtRlrrQAAAAAAiHUpbhcAAAAAAEBPEGABAAAAAHGBAAsAAAAAiAsEWAAAAABAXCDAAgAAAADiAgEWAAAAABAXUt0u4GQNGDDAFhUVuV0GAAAAACAC1q9fX2GtLezsWtwF2KKiIq1bt87tMgAAAAAAEWCM2R3qGkOIAQAAAABxgQALAAAAAIgLBFgAAAAAQFyIuzmwAAAAACBJTU1NKisrU319vduloBcyMzM1bNgwpaWl9fg5BFgAAAAAcamsrEx5eXkqKiqSMcbtcnASrLU6cuSIysrKVFxc3OPnMYQYAAAAQFyqr69X//79Ca9xyBij/v37n3TvOQEWAAAAQNwivMav3vzbEWABAAAAoBeqqqr0yCOP9Oq5l112maqqqrq85yc/+YmWLFnSq9dPVARYAAAAAOiFrgKs1+vt8rmvvvqq+vXr1+U99913ny688MJe13ey2tfc3Nzco+f19L5wIMACAAAAQC/cc8892r59u84++2zdddddWr58uebOnaubb75ZEydOlCRdeeWVmjp1qsaPH6/HHnus9blFRUWqqKjQrl27NG7cON1+++0aP368Lr74YtXV1UmSFi5cqBdeeKH1/nvvvVdTpkzRxIkT9emnn0qSysvLddFFF2nKlCn6+te/rpEjR6qioqJDrW+++abOPfdcTZkyRdddd52OHz/e+rr33XefLrjgAv35z3/WnDlz9KMf/UizZ8/WQw89pN27d2v+/PkqKSnR/PnztWfPntbavve972nu3Lm6++67I/dFbodViAEAAADEvaJ7/h6x1971i893ev4Xv/iFNm3apI8++kiStHz5cn3wwQfatGlT68q6Tz75pAoKClRXV6fp06frmmuuUf/+/du8zmeffaZnn31Wjz/+uK6//nq9+OKLuuWWWzq834ABA7RhwwY98sgjeuCBB/TEE0/oX/7lXzRv3jz98Ic/1Ouvv94mJLeoqKjQv/7rv2rJkiXKycnRL3/5Sz344IP6yU9+IsnZzmbVqlWSpEcffVRVVVVasWKFJOnyyy/Xbbfdpi996Ut68skndeedd+ovf/mLJGnbtm1asmSJPB5Pb76svUKABQAAAIAwmTFjRpttYR5++GG9/PLLkqS9e/fqs88+6xBgi4uLdfbZZ0uSpk6dql27dnX62ldffXXrPS+99JIkadWqVa2vv2DBAuXn53d43po1a7R582adf/75kqTGxkade+65rddvuOGGNvcHH69evbr1vW699Vb94Ac/aL123XXXRTW8SgRYAAAAAAibnJyc1vby5cu1ZMkSrV69WtnZ2ZozZ06n28ZkZGS0tj0eT+sQ4lD3eTye1nmn1tpua7LW6qKLLtKzzz7bbc2dHQcLXjm4q/sihQALAAAAIO6FGuYbSXl5eaqpqQl5vbq6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1aQnFnx129b/vnnQo3hxAnrqx8aehUqWytJCvtWCZNuMbtqgAAAIDEFWKYb6TdeOONuv3221VRUaEVK1ZIkqqrqzVw4EClpaVp2bJ
|
|||
|
"text/plain": [
|
|||
|
"<Figure size 1152x576 with 1 Axes>"
|
|||
|
]
|
|||
|
},
|
|||
|
"metadata": {
|
|||
|
"needs_background": "light"
|
|||
|
},
|
|||
|
"output_type": "display_data"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plot_learning_curve()"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"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",
|
|||
|
"theme": "amu"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 4
|
|||
|
}
|