forked from filipg/aitech-eks-pub
215 lines
5.8 KiB
Plaintext
215 lines
5.8 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "continent-intermediate",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# Regresja liniowa\n",
|
|||
|
"\n",
|
|||
|
"Regresja liniowa jest prosta...\n",
|
|||
|
"\n",
|
|||
|
"![Ceny mieszkań](./08_files/linregr1.png)\n",
|
|||
|
"\n",
|
|||
|
"... dosłownie — dopasuj prostą $y = ax + b$ do punktów\n",
|
|||
|
"\n",
|
|||
|
"Należy odgadnąć $a$ i $b$ tak, aby zminimalizować błąd\n",
|
|||
|
"kwadratowy, tj. wartość:\n",
|
|||
|
"\n",
|
|||
|
"$$\\sum_{i=1}^n (y_i - (ax_i + b))^2$$\n",
|
|||
|
"\n",
|
|||
|
"\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "original-speed",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Regresje liniowa (jednej zmiennej) jest łatwa do rozwiązania — wystarczy podstawić do wzoru!\n",
|
|||
|
"\n",
|
|||
|
"$$\\hat{b} = \\frac{ \\sum_{i=1}^{n}{x_i y_i} - \\frac{1}{n} \\sum_{i=1}^n x_i\n",
|
|||
|
" \\sum_{j=1}^n y_j}{ \\sum_{i=1}^n {x_i^2} - \\frac{1}{n} (\\sum_{i=1}^n\n",
|
|||
|
" x_i)^2 }$$\n",
|
|||
|
"\n",
|
|||
|
"$$\\hat{a} = \\bar{y} - \\hat{b}\\,\\bar{x}$$\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"Na przykład dla mieszkań: $b =$ -30809.203 zł, $a =$ 5733.693 zł/m$^2$.\n",
|
|||
|
"\n",
|
|||
|
"![Ceny mieszkań](./08_files/linregr2.png)\n",
|
|||
|
"\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "significant-relaxation",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Regresja wielu zmiennych\n",
|
|||
|
"\n",
|
|||
|
"W praktyce mamy do czynienia z **wielowymiarową** regresją\n",
|
|||
|
"liniową.\n",
|
|||
|
"\n",
|
|||
|
"Cena mieszkań może być prognozowana na podstawie:\n",
|
|||
|
"\n",
|
|||
|
"* powierzchni ($x_1 = 32.3$) \n",
|
|||
|
"\n",
|
|||
|
"* liczby pokoi ($x_2 = 3$)\n",
|
|||
|
" \n",
|
|||
|
"* piętra ($x_3 = 4$)\n",
|
|||
|
"\n",
|
|||
|
"* wieku ($x_4 = 13$)\n",
|
|||
|
"\n",
|
|||
|
"* odległości od Dworca Centralnego w Warszawie ($x_5 = 371.3$)\n",
|
|||
|
"\n",
|
|||
|
"* cech zerojedynkowych:\n",
|
|||
|
"\n",
|
|||
|
" * czy wielka płyta? ($x_6 = 0$)\n",
|
|||
|
"\n",
|
|||
|
" * czy jest jacuzzi? ($x_7 = 1$)\n",
|
|||
|
"\n",
|
|||
|
" * czy jest grzyb? ($x_8 = 0$)\n",
|
|||
|
"\n",
|
|||
|
" * czy to Kielce? ($x_9 = 1$)\n",
|
|||
|
"\n",
|
|||
|
"* ...\n",
|
|||
|
"\n",
|
|||
|
"... więc uogólniamy na wiele ($k$) wymiarów:\n",
|
|||
|
"\n",
|
|||
|
"$$ y = w_0 + w_1x_1 + \\ldots + w_kx_k = w_0 + \\sum_{j=1}^{k} w_jx_j $$\n",
|
|||
|
"\n",
|
|||
|
"gdzie:\n",
|
|||
|
"\n",
|
|||
|
"* $x_1,\\dots,x_k$ -- zmienne, na podstawie których zgadujemy\n",
|
|||
|
"\n",
|
|||
|
"* $w_0, w_1,\\dots,w_k$ -- wagi modelu (do wymyślenia na\n",
|
|||
|
" podstawie przykładów)\n",
|
|||
|
"\n",
|
|||
|
"* $y$ -- odgadywana wartość\n",
|
|||
|
"\n",
|
|||
|
"Też istnieje wzór ładny wzór na wyliczenie wektora wag!\n",
|
|||
|
"\n",
|
|||
|
"$$\\mathbf{w} = (\\mathbf{X}^{\\rm T}\\mathbf{X})^{-1} \\mathbf{X}^{\\rm T}\\mathbf{y}$$\n",
|
|||
|
"\n",
|
|||
|
"... niestety odwracanie macierzy nie jest tanie :("
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "ordinary-appendix",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Kilka sporzeżeń\n",
|
|||
|
"\n",
|
|||
|
"Regresja liniowa to najprostszy możliwy model:\n",
|
|||
|
"\n",
|
|||
|
"* im czegoś więcej na wejściu, tym proporcjonalnie (troszkę) więcej/mniej na wyjściu\n",
|
|||
|
"\n",
|
|||
|
"* nic prostszego nie da się wymyślić (funkcja stała??)\n",
|
|||
|
"\n",
|
|||
|
"* niestety model liniowy czasami kompletnie nie ma sensu (np. wzrost człowieka w\n",
|
|||
|
" stosunku do wieku)\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "egyptian-austria",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Uczenie\n",
|
|||
|
"\n",
|
|||
|
"A jak nauczyć się wag z przykładów?\n",
|
|||
|
"\n",
|
|||
|
"* wzór (z odwracaniem macierzy) — problematyczny\n",
|
|||
|
"\n",
|
|||
|
"### Metoda gradientu prostego\n",
|
|||
|
"\n",
|
|||
|
"![Morskie Oko - Krzysztof Dudzik](08_files/morskieoko.jpg)\n",
|
|||
|
"\n",
|
|||
|
"Schodź wzdłuż lokalnego spadku funkcji błędu.\n",
|
|||
|
"\n",
|
|||
|
"Tak więc w praktyce zamiast podstawiać do wzoru lepiej się uczyć iteracyjnie -\n",
|
|||
|
" metodą **gradientu prostego** (ang. _gradient descent_).\n",
|
|||
|
"\n",
|
|||
|
"1. Zacznij od byle jakich wag $w_i$ (np. wylosuj)\n",
|
|||
|
"2. Weź losowy przykład uczący $x_1,\\dots,x_n$, $y$.\n",
|
|||
|
"3. Oblicz wyjście $\\hat{y}$ na podstawie $x_1,\\dots,x_n$.\n",
|
|||
|
"4. Oblicz funkcję błędu między $y$ a $\\hat{y}$.\n",
|
|||
|
"5. Zmodyfikuj lekko wagi $(w_i)$ w kierunku spadku funkcji błędu.\n",
|
|||
|
"6. Jeśli błąd jest duży, idź do 2.\n",
|
|||
|
"\n",
|
|||
|
"Modyfikacja wag:\n",
|
|||
|
"\n",
|
|||
|
"$$w_i := w_i - x_i (\\hat{y} - y) \\eta$$\n",
|
|||
|
"\n",
|
|||
|
"gdzie $\\eta$ to **współczynnik uczenia** _learning rate_.\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "exact-train",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Ewaluacja regresji\n",
|
|||
|
"\n",
|
|||
|
"To miary błędu (im mniej, tym lepiej!)}\n",
|
|||
|
"\n",
|
|||
|
"### Błąd bezwzględny (Mean Absolute Error, MAE)\n",
|
|||
|
"\n",
|
|||
|
"$$\\frac{1}{n}\\sum_{i=1}^n |\\hat{y}_i - y_i| $$\n",
|
|||
|
"\n",
|
|||
|
"### Mean Squared Error (MSE)\n",
|
|||
|
"\n",
|
|||
|
"$$\\frac{1}{n}\\sum_{i=1}^n (\\hat{y}_i - y_i)^2$$\n",
|
|||
|
"\n",
|
|||
|
"### Root Mean Squared Error (RMSE)\n",
|
|||
|
"\n",
|
|||
|
"$$\\sqrt{\\frac{1}{n}\\sum_{i=1}^n (\\hat{y}_i - y_i)^2}$$\n",
|
|||
|
"\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "selective-agriculture",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Regresja liniowa dla tekstu\n",
|
|||
|
"\n",
|
|||
|
"Czym jest wektor $\\vec{x} = (x_1,\\dots,x_n)$? Wiemy, np. reprezentacja tf-idf (być z trikiem z haszowaniem, Word2vec etc.).\n",
|
|||
|
"\n",
|
|||
|
"![schemat regresji liniowej](08_files/regresja-liniowa-tekst.png)\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": null,
|
|||
|
"id": "numerous-limitation",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": []
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"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.9.2"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 5
|
|||
|
}
|