diff --git a/wyk/03_Regresja_liniowa_2.ipynb b/wyk/03_Regresja_liniowa_2.ipynb
index 605ad58..4d849ae 100644
--- a/wyk/03_Regresja_liniowa_2.ipynb
+++ b/wyk/03_Regresja_liniowa_2.ipynb
@@ -1496,7 +1496,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.10.4"
+ "version": "3.10.6"
},
"livereveal": {
"start_slideshow_at": "selected",
diff --git a/wyk/04_Regresja_logistyczna.ipynb b/wyk/04_Regresja_logistyczna.ipynb
index b61990d..ceeabc6 100644
--- a/wyk/04_Regresja_logistyczna.ipynb
+++ b/wyk/04_Regresja_logistyczna.ipynb
@@ -8,8 +8,8 @@
}
},
"source": [
- "### AITech — Uczenie maszynowe\n",
- "# 3. Regresja logistyczna"
+ "### Uczenie maszynowe\n",
+ "# 4. Regresja logistyczna"
]
},
{
@@ -86,7 +86,7 @@
}
},
"source": [
- "## 3.1. Dwuklasowa regresja logistyczna"
+ "## 4.1. Dwuklasowa regresja logistyczna"
]
},
{
@@ -115,7 +115,7 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 147,
"metadata": {
"slideshow": {
"slide_type": "notes"
@@ -127,7 +127,7 @@
"\n",
"import numpy as np\n",
"import matplotlib\n",
- "import matplotlib.pyplot as pl\n",
+ "import matplotlib.pyplot as plt\n",
"import pandas\n",
"import ipywidgets as widgets\n",
"\n",
@@ -138,8 +138,8 @@
"\n",
"# Przydatne funkcje\n",
"\n",
- "# Wyświetlanie macierzy w LaTeX-u\n",
"def LatexMatrix(matrix):\n",
+ " \"\"\"Wyświetlanie macierzy w LaTeX-u\"\"\"\n",
" ltx = r'\\left[\\begin{array}'\n",
" m, n = matrix.shape\n",
" ltx += '{' + (\"r\" * n) + '}'\n",
@@ -148,13 +148,13 @@
" ltx += r'\\end{array}\\right]'\n",
" return ltx\n",
"\n",
- "# Hipoteza (wersja macierzowa)\n",
- "def hMx(theta, X):\n",
+ "def h(theta, X):\n",
+ " \"\"\"Hipoteza (wersja macierzowa)\"\"\"\n",
" return X * theta\n",
"\n",
- "# Wykres danych (wersja macierzowa)\n",
- "def regdotsMx(X, y, xlabel, ylabel): \n",
- " fig = pl.figure(figsize=(16*.6, 9*.6))\n",
+ "def regdots(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",
@@ -162,12 +162,12 @@
" ax.set_xlabel(xlabel)\n",
" ax.set_ylabel(ylabel)\n",
" ax.margins(.05, .05)\n",
- " pl.ylim(y.min() - 1, y.max() + 1)\n",
- " pl.xlim(np.min(X[:, 1]) - 1, np.max(X[:, 1]) + 1)\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",
- "# Wykres krzywej regresji (wersja macierzowa)\n",
- "def reglineMx(fig, fun, theta, X):\n",
+ "def regline(fig, fun, theta, X):\n",
+ " \"\"\"Wykres krzywej regresji (wersja macierzowa)\"\"\"\n",
" ax = fig.axes[0]\n",
" x0 = np.min(X[:, 1]) - 1.0\n",
" x1 = np.max(X[:, 1]) + 1.0\n",
@@ -179,8 +179,8 @@
" theta1=(float(theta[1][0]) if theta[1][0] >= 0 else float(-theta[1][0])),\n",
" op='+' if theta[1][0] >= 0 else '-')))\n",
"\n",
- "# Legenda wykresu\n",
"def legend(fig):\n",
+ " \"\"\"Legenda wykresu\"\"\"\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",
@@ -189,20 +189,19 @@
" except AttributeError:\n",
" pass\n",
"\n",
- "# Wersja macierzowa funkcji kosztu\n",
- "def JMx(theta,X,y):\n",
+ "def J(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",
- "# Wersja macierzowa gradientu funkcji kosztu\n",
- "def dJMx(theta,X,y):\n",
+ "def dJ(theta,X,y):\n",
+ " \"\"\"Wersja macierzowa gradientu funkcji kosztu\"\"\"\n",
" return 1.0 / len(y) * (X.T * (X * theta - y)) \n",
"\n",
- "# Implementacja algorytmu gradientu prostego za pomocą numpy i macierzy\n",
- "def GDMx(fJ, fdJ, theta, X, y, alpha=0.1, eps=10**-3):\n",
+ "def GD(fJ, fdJ, theta, X, y, alpha=0.1, eps=10**-3):\n",
+ " \"\"\"Implementacja algorytmu gradientu prostego za pomocą numpy i macierzy\"\"\"\n",
" current_cost = fJ(theta, X, y)\n",
- " logs = [[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",
@@ -210,13 +209,12 @@
" break\n",
" if abs(prev_cost - current_cost) <= eps:\n",
" break\n",
- " logs.append([current_cost, theta]) \n",
- " return theta, logs\n",
+ " return theta\n",
"\n",
- "thetaStartMx = np.matrix([0, 0]).reshape(2, 1)\n",
+ "theta_start = np.matrix([0, 0]).reshape(2, 1)\n",
"\n",
- "# Funkcja, która rysuje próg\n",
"def threshold(fig, theta):\n",
+ " \"\"\"Funkcja, która rysuje próg\"\"\"\n",
" x_thr = (0.5 - theta.item(0)) / theta.item(1)\n",
" ax = fig.axes[0]\n",
" ax.plot([x_thr, x_thr], [-1, 2],\n",
@@ -226,7 +224,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 148,
"metadata": {
"slideshow": {
"slide_type": "subslide"
@@ -250,13 +248,13 @@
"source": [
"# Wczytanie pełnych (oryginalnych) danych\n",
"\n",
- "data_iris = pandas.read_csv('iris.csv')\n",
- "print(data_iris[:6])"
+ "data_iris = pandas.read_csv(\"iris.csv\")\n",
+ "print(data_iris[:6])\n"
]
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 149,
"metadata": {
"scrolled": true,
"slideshow": {
@@ -282,14 +280,16 @@
"# Ograniczenie danych do 2 klas i 1 cechy\n",
"\n",
"data_iris_setosa = pandas.DataFrame()\n",
- "data_iris_setosa['dł. płatka'] = data_iris['pl'] # \"pl\" oznacza \"petal length\"\n",
- "data_iris_setosa['Iris setosa?'] = data_iris['Gatunek'].apply(lambda x: 1 if x=='Iris-setosa' else 0)\n",
- "print(data_iris_setosa[:6])"
+ "data_iris_setosa[\"dł. płatka\"] = data_iris[\"pl\"] # \"pl\" oznacza \"petal length\"\n",
+ "data_iris_setosa[\"Iris setosa?\"] = data_iris[\"Gatunek\"].apply(\n",
+ " lambda x: 1 if x == \"Iris-setosa\" else 0\n",
+ ")\n",
+ "print(data_iris_setosa[:6])\n"
]
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 150,
"metadata": {
"slideshow": {
"slide_type": "notes"
@@ -304,16 +304,16 @@
"n = n_plus_1 - 1\n",
"Xn = data_iris_setosa.values[:, 0:n].reshape(m, n)\n",
"\n",
- "XMx3 = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
- "yMx3 = np.matrix(data_iris_setosa.values[:, 1]).reshape(m, 1)\n",
+ "X = np.matrix(np.concatenate((np.ones((m, 1)), Xn), axis=1)).reshape(m, n_plus_1)\n",
+ "y = np.matrix(data_iris_setosa.values[:, 1]).reshape(m, 1)\n",
"\n",
"# Regresja liniowa\n",
- "theta_e3, logs3 = GDMx(JMx, dJMx, thetaStartMx, XMx3, yMx3, alpha=0.03, eps=0.000001)"
+ "theta_lin = GD(J, dJ, theta_start, X, y, alpha=0.03, eps=0.000001)\n"
]
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": 151,
"metadata": {
"slideshow": {
"slide_type": "subslide"
@@ -323,969 +323,997 @@
{
"data": {
"image/svg+xml": [
- "\r\n",
- "\r\n",
- "\r\n",
- "\r\n"
+ "\n",
+ "\n",
+ "\n"
],
"text/plain": [
- "