uczenie-maszynowe/wyk/03_Regresja_liniowa_2.ipynb

1499 lines
524 KiB
Plaintext
Raw Normal View History

2022-10-14 11:34:46 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Uczenie maszynowe\n",
"# 3. Regresja liniowa część 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 3.1. Regresja liniowa wielu zmiennych"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Do przewidywania wartości $y$ możemy użyć więcej niż jednej cechy $x$:"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Przykład ceny mieszkań"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 2,
2022-10-27 16:10:38 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
2022-10-14 11:34:46 +02:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-10-18 13:52:38 +02:00
" y:price x1:isNew x2:rooms x3:floor x4:location x5:sqrMetres\n",
"1 476118.0 False 3 1 Centrum 78\n",
"2 459531.0 False 3 2 Sołacz 62\n",
"3 411557.0 False 3 0 Sołacz 15\n",
"4 496416.0 False 4 0 Sołacz 14\n",
"5 406032.0 False 3 0 Sołacz 15\n",
"... ... ... ... ... ... ...\n",
"1335 349000.0 False 4 0 Szczepankowo 29\n",
"1336 399000.0 False 5 0 Szczepankowo 68\n",
"1337 234000.0 True 2 7 Wilda 50\n",
"1338 210000.0 True 2 1 Wilda 65\n",
"1339 279000.0 True 2 2 Łazarz 36\n",
"\n",
"[1339 rows x 6 columns]\n"
2022-10-14 11:34:46 +02:00
]
}
],
"source": [
2022-10-18 13:52:38 +02:00
"import numpy as np\n",
"import pandas as pd\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"data = pd.read_csv(\"data_flats_train.tsv\", sep=\"\\t\")\n",
2022-10-28 09:00:37 +02:00
"data.rename(\n",
" columns={\n",
" col: f\"x{i}:{col}\" if i > 0 else f\"y:{col}\"\n",
" for i, col in enumerate(data.columns)\n",
" },\n",
" inplace=True,\n",
")\n",
2022-10-18 13:52:38 +02:00
"data.index = np.arange(1, len(data) + 1)\n",
2022-10-28 09:00:37 +02:00
"print(data)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"$$ x^{(2)} = ({\\rm \"False\"}, 3, 2, {\\rm \"Sołacz\"}, 62), \\quad x_3^{(2)} = 2 $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Hipoteza"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"W naszym przypadku (wybraliśmy 5 cech):\n",
"\n",
"$$ h_\\theta(x) = \\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\theta_3 x_3 + \\theta_4 x_4 + \\theta_5 x_5 $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"W ogólności ($n$ cech):\n",
"\n",
"$$ h_\\theta(x) = \\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\ldots + \\theta_n x_n $$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Jeżeli zdefiniujemy $x_0 = 1$, będziemy mogli powyższy wzór zapisać w bardziej kompaktowy sposób:\n",
"\n",
"$$\n",
"\\begin{array}{rcl}\n",
"h_\\theta(x)\n",
" & = & \\theta_0 x_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\ldots + \\theta_n x_n \\\\\n",
" & = & \\displaystyle\\sum_{i=0}^{n} \\theta_i x_i \\\\\n",
" & = & \\theta^T \\, x \\\\\n",
" & = & x^T \\, \\theta \\\\\n",
"\\end{array}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Metoda gradientu prostego notacja macierzowa"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Metoda gradientu prostego przyjmie bardzo elegancką formę, jeżeli do jej zapisu użyjemy wektorów i macierzy."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"$$\n",
"X=\\left[\\begin{array}{cc}\n",
"1 & \\left( \\vec x^{(1)} \\right)^T \\\\\n",
"1 & \\left( \\vec x^{(2)} \\right)^T \\\\\n",
"\\vdots & \\vdots\\\\\n",
"1 & \\left( \\vec x^{(m)} \\right)^T \\\\\n",
"\\end{array}\\right] \n",
"= \\left[\\begin{array}{cccc}\n",
"1 & x_1^{(1)} & \\cdots & x_n^{(1)} \\\\\n",
"1 & x_1^{(2)} & \\cdots & x_n^{(2)} \\\\\n",
"\\vdots & \\vdots & \\ddots & \\vdots\\\\\n",
"1 & x_1^{(m)} & \\cdots & x_n^{(m)} \\\\\n",
"\\end{array}\\right]\n",
"\\quad\n",
"\\vec{y} = \n",
"\\left[\\begin{array}{c}\n",
"y^{(1)}\\\\\n",
"y^{(2)}\\\\\n",
"\\vdots\\\\\n",
"y^{(m)}\\\\\n",
"\\end{array}\\right]\n",
"\\quad\n",
"\\theta = \\left[\\begin{array}{c}\n",
"\\theta_0\\\\\n",
"\\theta_1\\\\\n",
"\\vdots\\\\\n",
"\\theta_n\\\\\n",
"\\end{array}\\right]\n",
"$$"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 3,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# Wersje macierzowe funkcji rysowania wykresów punktowych oraz krzywej regresyjnej\n",
"\n",
2022-10-18 13:52:38 +02:00
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-18 13:52:38 +02:00
"def h(theta, x):\n",
" return x * theta\n",
2022-10-14 11:34:46 +02:00
"\n",
"\n",
2022-10-27 16:10:38 +02:00
"def regdots(x, y, xlabel=\"\", ylabel=\"\"):\n",
2022-10-14 11:34:46 +02:00
" fig = plt.figure(figsize=(16 * 0.6, 9 * 0.6))\n",
" ax = fig.add_subplot(111)\n",
" fig.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)\n",
2022-10-27 16:10:38 +02:00
" ax.scatter([x], [y], c=\"r\", s=50, label=\"Dane\")\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-18 13:52:38 +02:00
" ax.set_xlabel(xlabel)\n",
" ax.set_ylabel(ylabel)\n",
2022-10-14 11:34:46 +02:00
" ax.margins(0.05, 0.05)\n",
" plt.ylim(y.min() - 1, y.max() + 1)\n",
2022-10-27 16:10:38 +02:00
" plt.xlim(np.min(x) - 1, np.max(x) + 1)\n",
2022-10-14 11:34:46 +02:00
" return fig\n",
"\n",
"\n",
2022-10-27 16:10:38 +02:00
"def regline(fig, fun, theta, x, y, cost_fun):\n",
2022-10-14 11:34:46 +02:00
" ax = fig.axes[0]\n",
2022-10-27 16:10:38 +02:00
" x_min = np.min(x)\n",
" x_max = np.max(x)\n",
2022-10-18 13:52:38 +02:00
" x_range = [x_min, x_max]\n",
" x_matrix = np.matrix([1, x_min, 1, x_max]).reshape(2, 2)\n",
2022-10-27 16:10:38 +02:00
" cost = cost_fun(theta, x, y)\n",
2022-10-14 11:34:46 +02:00
" ax.plot(\n",
2022-10-18 13:52:38 +02:00
" x_range,\n",
" fun(theta, x_matrix),\n",
2022-10-14 11:34:46 +02:00
" linewidth=\"2\",\n",
" label=(\n",
2022-10-27 16:10:38 +02:00
" r\"$y={theta0:.1f}{op}{theta1:.1f}x, \\; J(\\theta)={cost:.3f}$\".format(\n",
" theta0=theta[0],\n",
" theta1=(theta[1] if theta[1] >= 0 else -theta[1]),\n",
" op=\"+\" if theta[1] >= 0 else \"-\",\n",
" cost=cost,\n",
2022-10-14 11:34:46 +02:00
" )\n",
" ),\n",
2022-10-27 16:10:38 +02:00
" )\n",
"\n",
2022-10-28 09:00:37 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"def legend(fig):\n",
" ax = fig.axes[0]\n",
" handles, labels = ax.get_legend_handles_labels()\n",
" # try-except block is a fix for a bug in Poly3DCollection\n",
" try:\n",
" fig.legend(handles, labels, fontsize=\"15\", loc=\"lower right\")\n",
" except AttributeError:\n",
2022-10-28 09:00:37 +02:00
" pass\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 4,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-10-27 16:10:38 +02:00
"X[:5]=matrix([[ 1., 3., 1., 78.],\n",
2022-10-18 13:52:38 +02:00
" [ 1., 3., 2., 62.],\n",
" [ 1., 3., 0., 15.],\n",
" [ 1., 4., 0., 14.],\n",
" [ 1., 3., 0., 15.]])\n",
2022-10-27 16:10:38 +02:00
"X.shape=(1339, 4)\n",
2022-10-18 13:52:38 +02:00
"y[:5]=matrix([[476118.],\n",
" [459531.],\n",
" [411557.],\n",
" [496416.],\n",
" [406032.]])\n",
"y.shape=(1339, 1)\n"
2022-10-14 11:34:46 +02:00
]
}
],
"source": [
2022-10-18 13:52:38 +02:00
"# Wczytwanie danych z pliku regresja liniowa wielu zmiennych notacja macierzowa\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-18 13:52:38 +02:00
"import pandas as pd\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-18 13:52:38 +02:00
"data = pd.read_csv(\n",
2022-10-28 09:00:37 +02:00
" \"data_flats_train.tsv\",\n",
" delimiter=\"\\t\",\n",
" usecols=[\"price\", \"rooms\", \"floor\", \"sqrMetres\"],\n",
2022-10-14 11:34:46 +02:00
")\n",
"m, n_plus_1 = data.values.shape\n",
"n = n_plus_1 - 1\n",
2022-10-27 16:10:38 +02:00
"Xn = data.values[:, 1:].reshape(m, n)\n",
2022-10-14 11:34:46 +02:00
"\n",
"# Dodaj kolumnę jedynek do macierzy\n",
2022-10-27 16:10:38 +02:00
"X = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
2022-10-18 13:52:38 +02:00
"y = np.matrix(data.values[:, 0]).reshape(m, 1)\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"print(f\"{X[:5]=}\")\n",
"print(f\"{X.shape=}\")\n",
2022-10-18 13:52:38 +02:00
"print(f\"{y[:5]=}\")\n",
"print(f\"{y.shape=}\")\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Funkcja kosztu notacja macierzowa"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"$$J(\\theta)=\\dfrac{1}{2|\\vec y|}\\left(X\\theta-\\vec{y}\\right)^T\\left(X\\theta-\\vec{y}\\right)$$ \n"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 5,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\Large J(\\theta) = 85104141370.9717$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Math, Latex\n",
"\n",
"\n",
2022-10-18 13:52:38 +02:00
"def J(theta, X, y):\n",
2022-10-14 11:34:46 +02:00
" \"\"\"Wersja macierzowa funkcji kosztu\"\"\"\n",
" m = len(y)\n",
2022-10-18 13:52:38 +02:00
" cost = 1.0 / (2.0 * m) * ((X * theta - y).T * (X * theta - y))\n",
" return cost.item()\n",
2022-10-14 11:34:46 +02:00
"\n",
"\n",
2022-10-18 13:52:38 +02:00
"theta = np.matrix([10, 90, -1, 2.5]).reshape(4, 1)\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"cost = J(theta, X, y)\n",
2022-10-14 11:34:46 +02:00
"display(Math(r\"\\Large J(\\theta) = %.4f\" % cost))\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Gradient notacja macierzowa"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"$$\\nabla J(\\theta) = \\frac{1}{|\\vec y|} X^T\\left(X\\theta-\\vec y\\right)$$"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 6,
2022-10-18 13:52:38 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"# Wyświetlanie macierzy w LaTeX-u\n",
"\n",
"\n",
"def latex_matrix(matrix):\n",
" ltx = r\"\\left[\\begin{array}\"\n",
" m, n = matrix.shape\n",
" ltx += \"{\" + (\"r\" * n) + \"}\"\n",
" for i in range(m):\n",
" ltx += r\" & \".join([(\"%.4f\" % j.item()) for j in matrix[i]]) + r\" \\\\ \"\n",
" ltx += r\"\\end{array}\\right]\"\n",
" return ltx\n"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 7,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\large \\theta = \\left[\\begin{array}{r}10.0000 \\\\ 90.0000 \\\\ -1.0000 \\\\ 2.5000 \\\\ \\end{array}\\right]\\quad\\large \\nabla J(\\theta) = \\left[\\begin{array}{r}-373492.7442 \\\\ -1075656.5086 \\\\ -989554.4921 \\\\ -23806475.6561 \\\\ \\end{array}\\right]$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Math, Latex\n",
"\n",
"\n",
2022-10-18 13:52:38 +02:00
"def dJ(theta, X, y):\n",
2022-10-14 11:34:46 +02:00
" \"\"\"Wersja macierzowa gradientu funckji kosztu\"\"\"\n",
" return 1.0 / len(y) * (X.T * (X * theta - y))\n",
"\n",
"\n",
2022-10-18 13:52:38 +02:00
"theta = np.matrix([10, 90, -1, 2.5]).reshape(4, 1)\n",
2022-10-14 11:34:46 +02:00
"\n",
"display(\n",
" Math(\n",
" r\"\\large \\theta = \"\n",
2022-10-18 13:52:38 +02:00
" + latex_matrix(theta)\n",
2022-10-14 11:34:46 +02:00
" + r\"\\quad\"\n",
" + r\"\\large \\nabla J(\\theta) = \"\n",
2022-10-27 16:10:38 +02:00
" + latex_matrix(dJ(theta, X, y))\n",
2022-10-14 11:34:46 +02:00
" )\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Algorytm gradientu prostego notacja macierzowa"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"$$ \\theta := \\theta - \\alpha \\, \\nabla J(\\theta) $$"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 8,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
2022-10-28 14:31:38 +02:00
"slide_type": "subslide"
2022-10-14 11:34:46 +02:00
}
},
2022-10-27 16:10:38 +02:00
"outputs": [],
"source": [
"def gradient_descent(fJ, fdJ, theta, X, y, alpha, eps):\n",
" \"\"\"Implementacja algorytmu gradientu prostego za pomocą numpy i macierzy\"\"\"\n",
" current_cost = fJ(theta, X, y)\n",
" history = [[current_cost, theta]]\n",
" while True:\n",
" theta = theta - alpha * fdJ(theta, X, y) # implementacja wzoru\n",
" current_cost, prev_cost = fJ(theta, X, y), current_cost\n",
" if abs(prev_cost - current_cost) <= eps:\n",
" break\n",
" if current_cost > prev_cost:\n",
" print(\"Długość kroku (alpha) jest zbyt duża!\")\n",
" break\n",
" history.append([current_cost, theta])\n",
" return theta, history\n"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 9,
2022-10-27 16:10:38 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
2022-10-14 11:34:46 +02:00
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\large\\textrm{Wynik:}\\quad \\theta = \\left[\\begin{array}{r}17446.2135 \\\\ 86476.7960 \\\\ -1374.8950 \\\\ 2165.0689 \\\\ \\end{array}\\right] \\quad J(\\theta) = 10324864803.1591 \\quad \\textrm{po 374575 iteracjach}$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-18 13:52:38 +02:00
"theta_start = np.zeros((n + 1, 1))\n",
2022-10-14 11:34:46 +02:00
"\n",
"# Zmieniamy wartości alpha (rozmiar kroku) oraz eps (kryterium stopu)\n",
2022-10-27 16:10:38 +02:00
"theta_best, history = gradient_descent(J, dJ, theta_start, X, y, alpha=0.0001, eps=0.1)\n",
2022-10-14 11:34:46 +02:00
"\n",
"display(\n",
" Math(\n",
" r\"\\large\\textrm{Wynik:}\\quad \\theta = \"\n",
2022-10-18 13:52:38 +02:00
" + latex_matrix(theta_best)\n",
2022-10-14 11:34:46 +02:00
" + (r\" \\quad J(\\theta) = %.4f\" % history[-1][0])\n",
" + r\" \\quad \\textrm{po %d iteracjach}\" % len(history)\n",
" )\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 3.2. Metoda gradientu prostego w praktyce"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Kryterium stopu"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Algorytm gradientu prostego polega na wykonywaniu określonych kroków w pętli. Pytanie brzmi: kiedy należy zatrzymać wykonywanie tej pętli?\n",
"\n",
"W każdej kolejnej iteracji wartość funkcji kosztu maleje o coraz mniejszą wartość.\n",
"Parametr `eps` określa, jaka wartość graniczna tej różnicy jest dla nas wystarczająca:\n",
"\n",
" * Im mniejsza wartość `eps`, tym dokładniejszy wynik, ale dłuższy czas działania algorytmu.\n",
" * Im większa wartość `eps`, tym krótszy czas działania algorytmu, ale mniej dokładny wynik."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Na wykresie zobaczymy porównanie regresji dla różnych wartości `eps`"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 10,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
2022-10-18 13:52:38 +02:00
{
2022-10-27 16:10:38 +02:00
"name": "stdout",
"output_type": "stream",
"text": [
"eps= 0.1, cost=10324864803.159, steps=374575\n",
"eps= 1.0, cost=10324942127.799, steps=176746\n",
"eps= 10.0, cost=10325220747.014, steps= 60389\n",
"eps= 100.0, cost=10325742602.406, steps= 46184\n",
"eps= 1000.0, cost=10330453738.393, steps= 34059\n",
"eps=10000.0, cost=10377076139.727, steps= 22123\n"
2022-10-18 13:52:38 +02:00
]
2022-10-14 11:34:46 +02:00
}
],
"source": [
2022-10-27 16:10:38 +02:00
"theta_start = np.zeros((n + 1, 1))\n",
2022-10-18 13:52:38 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"epss = [10.0**n for n in range(-1, 5)]\n",
"costs = []\n",
"lengths = []\n",
"for eps in epss:\n",
2022-10-28 09:00:37 +02:00
" theta_best, history = gradient_descent(\n",
" J, dJ, theta_start, X, y, alpha=0.0001, eps=eps\n",
" )\n",
2022-10-27 16:10:38 +02:00
" cost = history[-1][0]\n",
" steps = len(history)\n",
" print(f\"{eps=:7}, {cost=:15.3f}, {steps=:6}\")\n",
" costs.append(cost)\n",
2022-10-28 09:00:37 +02:00
" lengths.append(steps)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 11,
2022-10-27 16:10:38 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
"def eps_cost_steps_plot(eps, costs, steps):\n",
" \"\"\"Wykres kosztu i liczby kroków w zależności od eps\"\"\"\n",
" fig, ax1 = plt.subplots()\n",
" ax2 = ax1.twinx()\n",
" ax1.plot(eps, steps, \"--s\", color=\"green\")\n",
" ax2.plot(eps, costs, \":o\", color=\"orange\")\n",
" ax1.set_xscale(\"log\")\n",
" ax1.set_xlabel(\"eps\")\n",
" ax1.set_ylabel(\"liczba kroków\", color=\"green\")\n",
" ax2.set_ylabel(\"koszt\", color=\"orange\")\n",
2022-10-28 09:00:37 +02:00
" plt.show()\n"
2022-10-27 16:10:38 +02:00
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 12,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2022-10-28 14:31:38 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAApoAAAHECAYAAACZYIrlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAACEGElEQVR4nO3deVxU5f4H8M+wDIsw7LIIKEqiuKBiIuYuSsZ172Zqaa5pWimlXirT7q3w2mqldu+v0qzMLTVvroQiLrihuKEoigIKuLLvM8/vj4mBERQGGc4An/frNa+Y5zxz5jNHcr4+5zzPkQkhBIiIiIiI6piR1AGIiIiIqHFioUlEREREesFCk4iIiIj0goUmEREREekFC00iIiIi0gsWmkRERESkFyw0iYiIiEgvWGgS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S8gIiIyBKX5QOL/AQVpUichHUheaPr4+CAuLg7Hjh3DrFmzMGnSJMTHxwMAZsyYgeDgYHTq1AkTJkzA2rVrsXXrVly9elXi1DUTFhaGrKwszaPsc0mpvVN7jG6vvrY0/FC4xGmIiIhqKD0SOD4D2NsL4OopDYbkhaZcLoe3tzf8/f0RHh4OPz8/LF++vMq+AQEBAIDExEQAgIuLS6WZ32XPXVxcHttHoVDAwsICjo6OMDY2rrJPxX0UFxcjMzPzkX2qYmZmpplRr1AoYG1t/bhDUW/e6fMOAGBz/Gbcy78ncRoiIqIakBkDDj2AFsN4N6AGRPJC82EqlQpFRUVVbouLiwMAuLq
2022-10-14 11:34:46 +02:00
"text/plain": [
2022-10-27 16:10:38 +02:00
"<Figure size 640x480 with 2 Axes>"
2022-10-14 11:34:46 +02:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-27 16:10:38 +02:00
"eps_cost_steps_plot(epss, costs, lengths)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Długość kroku ($\\alpha$)"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 13,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
2022-10-27 16:10:38 +02:00
"import ipywidgets as widgets\n",
"\n",
2022-10-14 11:34:46 +02:00
"# Jak zmienia się koszt w kolejnych krokach w zależności od alfa\n",
"\n",
"\n",
2022-10-28 09:00:37 +02:00
"def costchangeplot(history, return_fig=False):\n",
2022-10-14 11:34:46 +02:00
" fig = plt.figure(figsize=(16 * 0.6, 9 * 0.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.set_xlabel(\"krok\")\n",
" ax.set_ylabel(r\"$J(\\theta)$\")\n",
"\n",
" X = np.arange(0, 500, 1)\n",
" Y = [history[step][0] for step in X]\n",
" ax.plot(X, Y, linewidth=\"2\", label=(r\"$J(\\theta)$\"))\n",
2022-10-28 09:00:37 +02:00
" if return_fig:\n",
" return fig\n",
2022-10-14 11:34:46 +02:00
"\n",
"\n",
"def slide7(alpha):\n",
2022-10-28 09:00:37 +02:00
" theta_best, history = gradient_descent(\n",
" J, dJ, theta_start, X, y, alpha=0.0001, eps=0.1\n",
" )\n",
" fig = costchangeplot(history, return_fig=True)\n",
2022-10-14 11:34:46 +02:00
" legend(fig)\n",
"\n",
"\n",
"sliderAlpha1 = widgets.FloatSlider(\n",
" min=0.01, max=0.03, step=0.001, value=0.02, description=r\"$\\alpha$\", width=300\n",
")\n"
]
},
{
"cell_type": "code",
2022-10-28 14:31:38 +02:00
"execution_count": 14,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
2022-10-28 14:31:38 +02:00
"model_id": "ff4fdbda4b1f4f2b8be74d4416294e0a",
2022-10-14 11:34:46 +02:00
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.02, description='$\\\\alpha$', max=0.03, min=0.01, step=0.001), Button…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.slide7(alpha)>"
]
},
2022-10-28 14:31:38 +02:00
"execution_count": 14,
2022-10-14 11:34:46 +02:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"widgets.interact_manual(slide7, alpha=sliderAlpha1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## 3.3. Normalizacja danych"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Normalizacja danych to proces, który polega na dostosowaniu danych wejściowych w taki sposób, żeby ułatwić działanie algorytmowi gradientu prostego.\n",
"\n",
"Wyjaśnię to na przykladzie."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Użyjemy danych z „Gratka flats challenge 2017”.\n",
"\n",
2022-10-28 14:31:38 +02:00
"Rozważmy model $h(x) = \\theta_0 + \\theta_1 x_1 + \\theta_2 x_2 + \\theta_3 x_3$, w którym cena mieszkania prognozowana jest na podstawie liczby pokoi $x_1$, piętra $x_2$ i metrażu $x_3$:"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 14,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
2022-10-27 16:10:38 +02:00
"name": "stdout",
"output_type": "stream",
"text": [
" price rooms floor sqrMetres\n",
"0 476118.0 3 1 78\n",
"1 459531.0 3 2 62\n",
"2 411557.0 3 0 15\n",
"3 496416.0 4 0 14\n",
"4 406032.0 3 0 15\n",
"... ... ... ... ...\n",
"1334 349000.0 4 0 29\n",
"1335 399000.0 5 0 68\n",
"1336 234000.0 2 7 50\n",
"1337 210000.0 2 1 65\n",
"1338 279000.0 2 2 36\n",
"\n",
"[1339 rows x 4 columns]\n"
]
2022-10-14 11:34:46 +02:00
}
],
"source": [
2022-10-27 16:10:38 +02:00
"# Dane, które wczytaliśmy na początku wykładu\n",
2022-10-28 09:00:37 +02:00
"print(data)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 15,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
2022-10-27 16:10:38 +02:00
"def show_mins_and_maxs(X):\n",
" \"\"\"Funkcja, która pokazuje wartości minimalne i maksymalne w macierzy X\"\"\"\n",
" mins = np.amin(X, axis=0).tolist()[0] # wartości minimalne\n",
" maxs = np.amax(X, axis=0).tolist()[0] # wartości maksymalne\n",
2022-10-14 11:34:46 +02:00
" for i, (xmin, xmax) in enumerate(zip(mins, maxs)):\n",
" display(Math(r\"${:.2F} \\leq x_{} \\leq {:.2F}$\".format(xmin, i, xmax)))\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Cechy w danych treningowych przyjmują wartości z zakresu:"
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 16,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 1.00 \\leq x_0 \\leq 1.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle 2.00 \\leq x_1 \\leq 7.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
2022-10-27 16:10:38 +02:00
"$\\displaystyle 0.00 \\leq x_2 \\leq 16.00$"
2022-10-14 11:34:46 +02:00
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
2022-10-27 16:10:38 +02:00
},
{
"data": {
"text/latex": [
"$\\displaystyle 12.00 \\leq x_3 \\leq 196.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
2022-10-14 11:34:46 +02:00
}
],
"source": [
2022-10-28 14:31:38 +02:00
"show_mins_and_maxs(X)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Jak widzimy, $x_2$ przyjmuje wartości dużo większe niż $x_1$.\n",
"Powoduje to, że wykres funkcji kosztu jest bardzo „spłaszczony” wzdłuż jednej z osi:"
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 17,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"outputs": [],
"source": [
2022-10-27 16:10:38 +02:00
"def contour_plot(X, y):\n",
" theta0_vals = np.linspace(-1e7, 1e7, 100)\n",
" theta1_vals = np.linspace(-1e7, 1e7, 100)\n",
2022-10-14 11:34:46 +02:00
"\n",
" J_vals = np.zeros(shape=(theta0_vals.size, theta1_vals.size))\n",
" for t1, element in enumerate(theta0_vals):\n",
" for t2, element2 in enumerate(theta1_vals):\n",
" thetaT = np.matrix([1.0, element, element2]).reshape(3, 1)\n",
2022-10-27 16:10:38 +02:00
" J_vals[t1, t2] = J(thetaT, X, y)\n",
2022-10-14 11:34:46 +02:00
"\n",
" plt.figure()\n",
2022-10-27 16:10:38 +02:00
" plt.contour(theta0_vals, theta1_vals, J_vals.T, levels=20)\n",
" plt.xlabel(r\"$\\theta_0$\")\n",
" plt.ylabel(r\"$\\theta_1$\")\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 18,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
2022-10-28 09:00:37 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl4AAAHDCAYAAAD1MRSGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAACqlUlEQVR4nO29eZxcVZ33/7lrdSchAQwkBCOyKIKyRDARxgUljwmgI8rDEGWGZRRGNCgGROIjYFgMCDK48IijbM7AoPgTXMAIRvM4agY0yoBsA0wUQTosMemk03X33x/3nnO/59xT1d1Jd93q7u/79cqrq84991ZVd7r6XZ/vWawsyzIwDMMwDMMwY45d9xNgGIZhGIaZLLB4MQzDMAzDdAgWL4ZhGIZhmA7B4sUwDMMwDNMhWLwYhmEYhmE6BIsXwzAMw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yR8VulLg4jRAdeIHlTqR/Oml5Gq5WJZ+oSf59SZ/NkwlXYMM2r5R3nZEALt1pi+L1xjxrfX/H6bPmA7PduFZHjzbhWu5xX0Xru3KY67twi/KO9XjxW3Lg2PZ/KY/QaESWDdplhZJoFn0zGXcsNKHtidpXOmTZDGiVLSFEBNEYtGmlYKlAAK1loIdMnmjKmnVdpEWepZeItbLvdWEUJSZHcuDR1JDLgF3J6r81UOWZcVs3FBKlz45ozrrl6Z/RAJTPdkzlI9TVRYTbYZhighpGiEaYobhWFHO9PWJ3BnEzvLRHOhMAZDFa4z46Qu/hDfgj+o1LVi5pBXlGq8QObcQNEX
2022-10-14 11:34:46 +02:00
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-28 09:00:37 +02:00
"contour_plot(\n",
" X[:, [0, 2, 3]], y\n",
") # Wybieramy cechy [0, 2, 3], bo więcej nie da się zobaczyć na płaskim na wykresie\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
2022-10-27 16:10:38 +02:00
"Jeżeli funkcja kosztu ma kształt taki, jak na powyższym wykresie, to łatwo sobie wyobrazić, że znalezienie minimum lokalnego przy użyciu metody gradientu prostego musi stanowć nie lada wyzwanie: algorytm szybko znajdzie „rynnę”, ale „zjazd” wzdłuż „rynny” w poszukiwaniu minimum będzie odbywał się bardzo powoli."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Liczba kroków: 374575\n",
"Koszt: 10324864803.159063\n"
]
}
],
"source": [
"theta_start = np.zeros((n + 1, 1))\n",
"theta_best, history = gradient_descent(J, dJ, theta_start, X, y, alpha=0.0001, eps=0.1)\n",
"print(f\"Liczba kroków: {len(history)}\")\n",
2022-10-28 09:00:37 +02:00
"print(f\"Koszt: {history[-1][0]}\")\n"
2022-10-27 16:10:38 +02:00
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
2022-10-28 14:31:38 +02:00
"scrolled": true,
2022-10-27 16:10:38 +02:00
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2022-10-28 09:00:37 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAz4AAAH+CAYAAABdvNtFAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAzkUlEQVR4nO3dfZRVdb0/8M+ZGRhQmUF5FEEkH1PEwIy45s9SfLrk0nvLay66at6baWha1lVWV83lJbS6rh50UVmBrauS1kVv5kMootcCQxRFKcQyIRG5pTCAOODM/v2BczhnmEEG2GdvDq/XWmc1c84+e3/2mW8s3+f73Z9dSJIkCQAAgCpWk3UBAAAAaRN8AACAqif4AAAAVU/wAQAAqp7gAwAAVD3BBwAAqHqCDwAAUPUEHwAAoOoJPgAAQNUTfAAAgKpXtcHn8ccfj9NPPz0GDRoUhUIh7rnnni69/+23347zzz8/jjzyyKirq4szzzyzw+1mz54do0aNivr6+jjooINi2rRpO1w7AACwc1Vt8Fm3bl0cddRRccstt2zX+1taWqJnz57xhS98IcaOHdvhNi+//HKMGzcuPvaxj8WCBQvi8ssvj3/913+Nhx56aEdKBwAAdrJCkiRJ1kWkrVAoxIwZM8pmbZqbm+OrX/1q3HnnnbFq1aoYPnx43HjjjfHRj350i/eff/75sWrVqi1mja688sr41a9+Fc8//3zxuU996lOxatWqePDBB1M6GwAAoKuqdsbnvVxyySUxZ86cmD59ejz33HNx1llnxamnnhpLlizZ5n3MmTNni9mgU045JebMmbOzywUAAHbAbhl8li5dGlOnTo277747jjvuuDjwwAPjy1/+cnzkIx+JqVOnbvN+VqxYEQMGDCh7bsCAAdHU1BTr16/f2WUDAADbqS7rAr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4pKAQmhsAAECeCD4psNQNAADyRfBJQVlzA7kHAAAyJ/ikoKydtbVuAACQOcEnBaVL3cz4AABA9gSfFBRKko/cAwAA2RN8UlLTln0sdQMAgMwJPilpm/Wx1A0AALIn+KSkOOFjsRsAAGRO8ElJW0vr1taMCwEAAASf1Lw75WO+BwAAsif4pKStuYH7+AAAQPYEn5QU3p3ykXsAACB7gk9KijM+FrsBAEDmBJ+UaGcNAAD5IfikZPP9SyUfAADImuCTkkKxuUG2dQAAAIJPatqWusk9AACQPcEnJW3NDVpN+QAAQOYEn5QUZ3zkHgAAyJzgkxIzPgAAkB+CT2rM+AAAQF4IPilp6+oGAABkT/BJiaVuAACQH4JPSgqWugEAQG7kKvgccMABUSgUtnhMmDAh69K6zIwPAADkR13WBZSaN29etLS0FH9//vnn46STToqzzjorw6q2jxuYAgBAfuQq+PTr16/s9xtuuCEOPPDAOP744zOqaPu1NTdIzPgAAEDmchV8Sm3YsCH+67/+K770pS8VZ0/aa25ujubm5uLvTU1NlSrvPW0OPtnWAQAA5Owan1L33HNPrFq1Ks4///xOt5k8eXI0NjYWH0OGDKlcge+h2Nwg4zoAAIAcB58f//jHcdppp8WgQYM63WbixImxevXq4mPZsmUVrHDrNDcAAID8yOVSt1deeSUefvjh+O///u+tbldfXx/19fUVqqpris0N5B4AAMhcLmd8pk6dGv37949x48ZlXcp2K5jxAQCA3Mhd8GltbY2pU6fGeeedF3V1uZyQ2ibFdgxyDwAAZC53wefhhx+OpUuXxgUXXJB1KTuk5t0pHzM+AACQvdxNqZx88slVce+bYjvrbMsAAAAihzM+1aLYzlryAQCAzAk+KdHcAAAA8kPwSUmxnXXGdQAAAIJPatpuYFoN1ysBAMCuTvB
2022-10-27 16:10:38 +02:00
"text/plain": [
"<Figure size 960x540 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-28 09:00:37 +02:00
"costchangeplot(history)\n"
2022-10-27 16:10:38 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
2022-10-14 11:34:46 +02:00
"\n",
"Jak temu zaradzić?\n",
"\n",
"Spróbujemy przekształcić dane tak, żeby funkcja kosztu miała „ładny”, regularny kształt."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Skalowanie"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Będziemy dążyć do tego, żeby każda z cech przyjmowała wartości w podobnym zakresie.\n",
"\n",
"W tym celu przeskalujemy wartości każdej z cech, dzieląc je przez wartość maksymalną:\n",
"\n",
"$$ \\hat{x_i}^{(j)} := \\frac{x_i^{(j)}}{\\max_j x_i^{(j)}} $$"
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 21,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 1.00 \\leq x_0 \\leq 1.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle 0.29 \\leq x_1 \\leq 1.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
2022-10-27 16:10:38 +02:00
"$\\displaystyle 0.00 \\leq x_2 \\leq 1.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle 0.06 \\leq x_3 \\leq 1.00$"
2022-10-14 11:34:46 +02:00
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-27 16:10:38 +02:00
"X_scaled = X / np.amax(X, axis=0)\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"show_mins_and_maxs(X_scaled)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 22,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2022-10-28 09:00:37 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl4AAAHDCAYAAAD1MRSGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOyddXhUd/r2P3F3g4QkhIQkxN0VtyIFShVoqW6Nbrvd7cq72/3tbne3u11a6galBUop7hp3d4MkBEhC3GWSmfP+MSGUKjITac/nunLRnpHznfkmM/d55H5UBEEQEBERERERERERUTqq470AEREREREREZFfCqLwEhEREREREREZI0ThJSIiIiIiIiIyRojCS0RERERERERkjBCFl4iIiIiIiIjIGCEKLxERERERERGRMUIUXiIiIiIiIiIiY4QovERERERERERExghReImIiIiIiIiIjBGi8BIRERERERERGSNE4aVEEhMTueuuu7C2tkZFRYUDBw7c0uP/8pe/oKKi8p0fPT095SxYRERERERERKmIwkuJ9Pb24u3tzTvvvHNbj3/ppZdoaGi44cfNzY01a9YoeKUiIiIiIiIiY4EovJTIokWL+Nvf/sbKlSu/9/bBwUFeeuklbGxs0NPTIzg4mPj4+NHb9fX1mTJlyujP1atXKS0tZePGjWP0CkREREREREQUiSi8xpFnnnmGtLQ0vvzySwoLC1mzZg0LFy6kqqrqe+//8ccf4+zsTGRk5BivVEREREREREQRiMJrnKirq2Pr1q3s2bOHyMhIHB0deemll4iIiGDr1q3fuf/AwAA7duwQo10iIiIiIiKTGPXxXsAvlaKiIqRSKc7OzjccHxwcxMzM7Dv3379/P93d3axfv36sligiIiIiIiKiYEThNU709PSgpqZGTk4OampqN9ymr6//nft//PHHLF26FCsrq7FaooiIiIiIiIiCE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hPCeoe5mGh+d+rLT1HTW8euiwco6CxlqFfC/nlb7+j7+2b4xQiv3/72txw7doyioqLRY/fffz9tbW03FNcHBQWxZcsWQF6vZWdnxzPPPHPTxfW3KrwEQaD+cjslBXUUFdRRXnKFS7UtN0QKrqGlrYG9gwX2DhbYTjfDZpoZ1tNMmGpjgq7e2FzNTjb6egdpae6mpamL5pGflqYumho7aWrs5GpjJ4MDPy7OtLQ1sJlmio2dKTa2ptjYmmFrb4atvTkGhjpj9EomN1KpjIrKBtIzL5CWcYHzF27sFJ46xYjw0JmEh87E02MaamoTu+FaEAQqapo4mVjKqZRy2juvR52n25iyKNqdhVGzsDBV7pXzj9HdN8DRtDL2JRRSXX89CuY23YrVMd7MD3RBW0tD6esYkko5W3ieHUl5FNQ2jB73n2HDQ9F+RLvPQE0J9TSCIJBbV8+nKdmcK6++fl47azZGBhI90+GWovc/RUtvL9ty8vgiL58eiQQAe2NjnggOZIW7G5pqd37hOzA8xJdlRbyXl8nVXrmYVaQAK2lrZHNhMmcuVwGgpqLCakcvnvEIZ5q+0W0/b3V3M2+Xx3PiSsno895t58tTrtFM0bm95y3vusSHF46T3SZfq46aJmvtornXLgpdde1bfr6iznKSL6bxK+9HROH1Q3zTFd7X15c33niD2NhYTE1NsbOz45VXXuHKlSts374dkNtJeHh48PTTT/PII49w7tw5nnvuOY4ePcqCBQsAuZ3E+vXr+eCDDwgKCmLz5s189dVXlJeX3/SMxJ8SXoIgUH+pjZzMagrzLlKcX0dba8937mduaYjzrKk4u1ozY6YV9jMssJpirNAPChH5fnR19nO1sYPG+g4aR9K39VfaabjSztXGDmTSH/4TMTLRxdbeHFt7c+wdzLGbbo6dgwU
2022-10-14 11:34:46 +02:00
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-27 16:10:38 +02:00
"contour_plot(X_scaled[:, [0, 2, 3]], y)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Teraz możemy użyć większej długości kroku $\\alpha$, dzięki czemu algorytm szybciej znajdzie rozwiązanie."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Liczba kroków: 82456\n",
"Koszt: 10324856880.491594\n"
]
}
],
"source": [
"theta_start = np.zeros((n + 1, 1))\n",
2022-10-28 09:00:37 +02:00
"theta_best, history = gradient_descent(\n",
" J, dJ, theta_start, X_scaled, y, alpha=0.01, eps=0.1\n",
")\n",
2022-10-27 16:10:38 +02:00
"print(f\"Liczba kroków: {len(history)}\")\n",
2022-10-28 09:00:37 +02:00
"print(f\"Koszt: {history[-1][0]}\")\n"
2022-10-27 16:10:38 +02:00
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2022-10-28 09:00:37 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAz4AAAH+CAYAAABdvNtFAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAABG50lEQVR4nO3deXhU9d3//9fJTDLZN7KQkEDYd5Bd6i4oWqRaW2+ltGJpa7W4tbZ39b6/Vv15U7S1fr1b/VqXCva+FW6XG627KAJlNayyyL6FJYQtmayTycz5/ZFkmIEACWTmzEyej+s6V86c85mT9yRHrrz8LMcwTdMUAAAAAESxGKsLAAAAAIBgI/gAAAAAiHoEHwAAAABRj+ADAAAAIOoRfAAAAABEPYIPAAAAgKhH8AEAAAAQ9Qg+AAAAAKIewQcAAABA1CP4AAAAAIh6URt8Fi9erEmTJik/P1+GYejdd99t0/vr6up0xx13aPDgwbLb7brppptabLdw4UINHz5cDodDvXr10uzZsy+4dgAAAADtK2qDT3V1tYYO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AAAAIDgIfhYqDfBBwAAAAgJgo+FijolKs7W+CvYdphn+QAAAADBQvCxkN0Wox7ZjSu77TlaLVeDx+KKAAAAgOhE8LFY386Nw90avKZ2H622uBoAAAAgOhF8LNYnYJ4Pw90AAACAYCD4WKx3jt+S1qUscAAAAAAEA8HHYs1D3SRWdgMAAACCheBjscKMRMXHNq/sRvABAAAAgoHgY7GYGEO9cxp7ffYer1Gdm5XdAAAAgPZG8AkDvXMb5/m
2022-10-27 16:10:38 +02:00
"text/plain": [
"<Figure size 960x540 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-28 09:00:37 +02:00
"costchangeplot(history)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Normalizacja średniej"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Będziemy dążyć do tego, żeby dodatkowo średnia wartość każdej z cech była w okolicach $0$.\n",
"\n",
"W tym celu oprócz przeskalowania odejmiemy wartość średniej od wartości każdej z cech:\n",
"\n",
"$$ \\hat{x_i}^{(j)} := \\frac{x_i^{(j)} - \\mu_i}{\\max_j x_i^{(j)}} $$"
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 25,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 0.00 \\leq x_0 \\leq 0.00$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle -0.10 \\leq x_1 \\leq 0.62$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
2022-10-27 16:10:38 +02:00
"$\\displaystyle -0.17 \\leq x_2 \\leq 0.83$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle -0.24 \\leq x_3 \\leq 0.70$"
2022-10-14 11:34:46 +02:00
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-27 16:10:38 +02:00
"X_normalized = (X - np.mean(X, axis=0)) / np.amax(X, axis=0)\n",
2022-10-14 11:34:46 +02:00
"\n",
2022-10-27 16:10:38 +02:00
"show_mins_and_maxs(X_normalized)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "code",
2022-10-27 16:10:38 +02:00
"execution_count": 26,
2022-10-14 11:34:46 +02:00
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2022-10-28 09:00:37 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl4AAAHDCAYAAAD1MRSGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/av/WaAAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOyddXhT5/uH7yR1dzdatFhxd4a7uzNgg2FjG9tgY0O2AWMwYAx3d3d3KBSn0JZS6u5t9Pz+yODHlJM0LWPf3NeVi4s2z3veJM05z3nk80gEQRAwYsSIESNGjBgxUuxI3/YGjBgxYsSIESNG/lcwOl5GjBgxYsSIESMlhNHxMmLEiBEjRowYKSGMjpcRI0aMGDFixEgJYXS8jBgxYsSIESNGSgij42XEiBEjRowYMVJCGB0vI0aMGDFixIiREsLoeBkxYsSIESNGjJQQRsfLiBEjRowYMWKkhDA6XkaMGDFixIgRIyWE0fEqRs6fP0/Hjh3x8vJCIpGwd+9eney//vprJBLJnx7W1tbFs2EjRowYMWLESLFidLyKkby8PKpWrcqSJUv0sv/4449JSEj43SM4OJiePXsaeKdGjBgxYsSIkZLA6HgVI23btmXmzJl07dr1L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1tmVOtJzKJ+D8VQRD44voR7qUn4GhuyfLGPbEx/XNU8E2EJSXw0amDaASBvhWqMKFmfZ3XeIlCrWbsvoOci4rGwsSEFd27UM/fT+/1XmfD5Vt8tHk/BUoVDUr7s/n9Pvg6ORhk7dcRBIGt58Po/cMmHsQkYWtpzneD2/Hj8I642r/5rq84UGs0nLgWzuCvNzN6znb
2022-10-14 11:34:46 +02:00
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-27 16:10:38 +02:00
"contour_plot(X_normalized[:, [0, 2, 3]], y)\n"
2022-10-14 11:34:46 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Teraz funkcja kosztu ma wykres o bardzo regularnym kształcie algorytm gradientu prostego zastosowany w takim przypadku bardzo szybko znajdzie minimum funkcji kosztu."
]
2022-10-27 16:10:38 +02:00
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Liczba kroków: 9511\n",
"Koszt: 80221516127.09409\n"
]
}
],
"source": [
"theta_start = np.zeros((n + 1, 1))\n",
2022-10-28 09:00:37 +02:00
"theta_best, history = gradient_descent(\n",
" J, dJ, theta_start, X_normalized, y, alpha=0.1, eps=0.1\n",
")\n",
2022-10-27 16:10:38 +02:00
"print(f\"Liczba kroków: {len(history)}\")\n",
2022-10-28 09:00:37 +02:00
"print(f\"Koszt: {history[-1][0]}\")\n"
2022-10-27 16:10:38 +02:00
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
2022-10-28 09:00:37 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0sAAAH+CAYAAABN3JWZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMSwgaH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MrMxoNdYzNzTXTW0aSJLyi0r14Iz1OplfdIlXAQAAAHCpmaVBgwZpwoQJiomJUWJiopKTkzV58mSNHDnSsc+4ceOUnp6umTNnSpLefPNNxcXFKTExUYWFhfroo4+0ZMkSLVy40KyP4VLc3Cx64672Opy1WlvTc3Tk9Fk9+slGffpQN3m5W82OBwAAALgsl5pZevvtt3XnnXfq8ccfV8uWLfWnP/1JjzzyiF5++WXHPhkZGUpLS3M8Ly4u1rPPPqs2bdqoV69e2rx5s3766Sf16dPHjI/gknw8rfrwgc6KCCg7/XDDodMa99VWVsgDAAAALsKlFngwgzMXeNV029JzdNd7q3W2xCZJeq5fC426rqnJqQAAAIDq48z3f5eaWULVat0wUP+8p53j+Ws/7tb3WzNMTAQAAAC4LspSHdO/daSe69fC8fzpuSnaeiTHxEQAAACAa6Is1UGP947X7R0bSpIKS+z648z1OpZTaHIqAAAAwLVQluogi8Wiibe3UefY+pKk47lF+uPM9SooLjU5GQAAAOA6KEt1lJe7Ve8P7aRG9X0kSdvSczV6drJs9jq93gcAAADgQFmqw0L8vDRteBf5e5fdbuunnZl6+dsdJqcCAAAAXANlqY5rFuG
2022-10-27 16:10:38 +02:00
"text/plain": [
"<Figure size 960x540 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
2022-10-28 09:00:37 +02:00
"costchangeplot(history)\n"
2022-10-27 16:10:38 +02:00
]
2022-10-14 11:34:46 +02:00
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"livereveal": {
"start_slideshow_at": "selected",
"theme": "white"
},
"vscode": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}