![]() |
Lineare Regression |
Am 1. Januar 1801 entdeckte der Astronom Giuseppe Piazzi den Zwergplanet Ceres. Er machte sehr detaillierte Notizen über Zeit und Position der Beobachtungen.
https://en.wikipedia.org/wiki/File:Costanzo_Angelini,_L%27astronomo_Piazzi_1825_ca.jpg
Wann kann man Ceres wieder beobachten?
https://en.wikipedia.org/wiki/File:Ceres-Beobachtung_von_Piazzi.png
Im September 1801 erstellte Carl F. Gauss ein Modell, welches die Sichtung von Ceres auf 0,5 Grad genau vorhersagte. Dazu verwendete er Linear Least-Squares Regression.
https://commons.wikimedia.org/wiki/File:Carl_Friedrich_Gauss_1840_by_Jensen.jpg
Beispiel
Wir wollen Grundstückspreise vorhersagen.
Grundstück (qm) | Preis (1000 €) |
---|---|
785 | 194 |
892 | 169 |
1045 | 208 |
887 | 130 |
1325 | 232 |
Welche Hypothesen $h$?
Lineare Funktionen \[ h(x) = w_0 + w_1 x_1 \]
z.B.: \[ h(x^{(1)}) = w_0 + w_1 \cdot 785\\ x^{(1)}_1 = 785 \]
Beispiel: $h(x) = 90 + 0.075 x$
Allgemeiner: \[ h_w(x) = w_0 + \sum_{j=1}^n w_j x_j \]
Für $$h(x) = w_0 + w_1 x_1 + w_2 x_2 + \ldots$$ nennen wir $w_0$ den Bias Term (oder Intercept)
Und noch allgemeiner: \[ h_w(x) = w \cdot \phi(x) \]
mit\[ \underbrace{ w = \left[ {\begin{array}{c} w_0 \\ w_1 \\ w_2 \\ \vdots \end{array}} \right]}_\text{Gewichte / Parameter} \text{ und }\, \underbrace{ \phi(x) = \left[ {\begin{array}{c} 1 \\ x_1 \\ x_2 \\ \vdots \end{array}} \right]}_\text{Features} \]
$\phi$ nennen wir Feature Extractor.
Wir gehen erstmal immer von folgender Form aus:
\[
\phi(x) = \left[ {\begin{array}{c}
1 \\ x_1 \\ x_2 \\ \vdots
\end{array}} \right]
\]
Wenn die Feature $x$ schon passend sind brauchen wir nicht notwendigerweise einen Feature Extractor.
Noch etwas Notation
Was ist unser Ziel?
$h_w(x)$ soll nah an $y$ liegen
\[ \operatorname{Loss}(x, y, w) = (h_w(x) - y)^2 \]
\[ \operatorname{TrainLoss}(w) = \frac{1}{m} \sum_{(x,y) \in \mathcal{D}_\text{train}} ( h_w(x) - y )^2 \] (Squared Loss)
Ziel: \[ \min_w \operatorname{TrainLoss}(w) \]
\[ = \min \frac{1}{m} \sum_{(x,y) \in \mathcal{D}_\text{train}} ( h_w(x) - y )^2 \]
Beispiel: \[ {\color{red}w} = \left[ {\color{red} \begin{array}{c} 100 \\ 0.075 \end{array}} \right] \]
$\operatorname{Loss}({\color{blue} 785},{\color{blue} 194},{\color{red} w})$ | $= ({\color{red} 100} + {\color{red} 0.075} \cdot {\color{blue} 785} - {\color{blue} 194})^2$ | $= 1233.77$ |
$\operatorname{Loss}({\color{blue} 892},{\color{blue} 169},{\color{red} w})$ | $= ({\color{red} 100} + {\color{red} 0.075} \cdot {\color{blue} 892} - {\color{blue} 169})^2$ | $= 4.41$ |
$\operatorname{Loss}({\color{blue} 1045},{\color{blue} 208},{\color{red} w})$ | $= ({\color{red} 100} + {\color{red} 0.075} \cdot {\color{blue} 1045} - {\color{blue} 208})^2$ | $= 877.64$ |
$\operatorname{Loss}({\color{blue} 887},{\color{blue} 130},{\color{red} w})$ | $= ({\color{red} 100} + {\color{red} 0.075} \cdot {\color{blue} 887} - {\color{blue} 130})^2$ | $= 1334.08$ |
$\operatorname{Loss}({\color{blue} 1325},{\color{blue} 232},{\color{red} w})$ | $= ({\color{red} 100} + {\color{red} 0.075} \cdot {\color{blue} 1325} - {\color{blue} 232})^2$ | $= 1064.39$ |
$\operatorname{TrainLoss}({\color{red} w})$ | $= 902.86$ |
Wie finden wir optimale Gewichte $w$?
Option 1: Analytisch
$$w = (X^T X)^{-1} X^T Y$$
Option 2: Iterativ
Idee: Gradient $\nabla_w \text{TrainLoss}(w)$ zeigt in die Richtung in der die Loss Funktion am stärksten wächst.
Algorithmus: Gradient descent
$\eta$: Lernrate / step size
Gradient Descent
Wenn wir viele Daten haben können wir Schritte für einzelne Datenpunkte machen:
Algorithmus: Stochastic gradient descent (SGD)
sklearn: sklearn.linear_model.LinearRegression
Pattern:
model = LinearRegression()
model.fit(features, targets)
model.predict(features)
Berechnet analytische Lösung
SGD mit sklearn:
sklearn.linear_model.SGDRegressor
Jetzt können wir die Gewichte $w$ unserer Hypothese berechnen: $$ h(x) = w_0 + w_1 x_1 + w_2 x_2 + \ldots + w_n x_n $$
Wie interpretieren wir die vorhergesagten Werte?
Andere Sichtweise auf Least Squares Loss
Annahme: \[ y^{(i)} = \hat{w}_0 + \hat{w}_1 x_1^{(i)} + \hat{w}_2 x_2^{(i)} + \ldots {\color{red} + e^{(i)}} \\ \]
"Wahre" Gewichte: $\hat{w}$
Fehlerterm $e^{(i)} \sim \mathcal{N}(0, \sigma^2)$
$\mathcal{N}(\mu, \sigma^2)$: Normalverteilung (Gauß Verteilung)
$\frac{1}{\sigma \sqrt{2 \pi}} e^{-\frac{(x-\mu)^2}{2 \sigma^2}}$
https://en.wikipedia.org/wiki/File:Standard_deviation_diagram.svg
Wahrscheinlichkeitstheorie
Central Limit Theorem (Zentraler Grenzwertsatz)
In etwa: Die Summe vieler unabhängiger Zufallsvariablen ist normalverteilt.
Darum sind so viele Dinge (ungefähr) normalverteilt.
Wenn \[ y_i = w \cdot x_i + e_i \\ e_i \sim \mathcal{N}(0, \sigma^2) \]
dann \[ y_i \sim \mathcal{N}(w \cdot x_i, \sigma^2) \]
Mit linearer Regression (und Squared Loss) berechnen wir die "Maximum Likelihood" Parameter $w$.
Idee: Welche Parameter sind am wahrscheinlichsten richtig, gegeben unsere Daten?
![]() |
vs |
![]() |
Was, wenn wir eine andere Verteilung haben?
Wenn wir von normalverteiltem Fehler ausgehen, dann gibt uns der Squared Loss die Parameter, die am wahrscheinlichsten die "richtigen" sind.
Andere Loss Funktionen sind möglich, zum Beispiel \[ Loss_\text{Abs.}(x,y,w) = | h_w(x) - y | \]
Absolute Loss
Auch bekannt als Median Regression.
Und wie interpretieren wir jetzt unser Modell?
Wir haben angenommen dass $$y^{(i)} = h_w(x^{(i)}) {\color{red} + e^{(i)}}$$ (für richtig geschätzte Parameter $w$)
Der Squared Loss (oder Mean Squared Error - MSE) \[ \frac{1}{m} \sum_{(x,y) \in \mathcal{D}_\text{train}} ( h_w(x) - y )^2 \] ist die Varianz unserer Fehler $\color{red} e$.
Der Root Mean Squared Error - RMSE \[ \sqrt{\text{MSE}} = \sqrt{\frac{1}{m} \sum_{(x,y) \in \mathcal{D}_\text{train}} ( h_w(x) - y )^2} \] ist die Standard Abweichung unserer Fehler $\color{red} e$.
Annahme: $e^{(i)} \sim \mathcal{N}(0, \sigma^2)$ mit $\sigma = \text{RMSE}$
68 - 95 - 99.7 Regel
Meistens stimmt die Annahme über normalverteilte Fehler nur ungefähr.
Daher stimmt die Interpretation meist auch nur ungefähr.
RMSE ist meist ein guter Indikator ob unser Modell "gut genug" ist.
R2 Score
Coefficient of determination
Gibt an, wie viel besser wir sind, als einfach den Mittelwert vorherzusagen
\[ \text{R2} = 1 - \frac{\color{blue} \text{SS}_\text{res}}{\color{red} \text{SS}_\text{tot}} \]
https://en.wikipedia.org/wiki/File:Coefficient_of_Determination.svg
Es macht Sinn, immer mehrere Metriken zu tracken um potentielle Probleme zu finden.