Skip to content

Commit

Permalink
update numbered and captioned figures
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcky committed Feb 21, 2024
1 parent 380bedc commit 8fa667b
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions lectures/simple_linear_regression.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ We can use a scatter plot of the data to see the relationship between $y_i$ (ice
---
mystnb:
figure:
name: wpdisc
caption: "Scatter plot"
name: sales-v-temp
---
ax = df.plot(
x='X',
Expand Down Expand Up @@ -88,8 +89,14 @@ df['Y_hat'] = α + β * df['X']
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Scatter plot with a line of fit"
name: sales-v-temp2
---
fig, ax = plt.subplots()
df.plot(x='X',y='Y', kind='scatter', ax=ax)
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
df.plot(x='X',y='Y_hat', kind='line', ax=ax)
```

Expand All @@ -103,8 +110,14 @@ df['Y_hat'] = α + β * df['X']
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Scatter plot with a line of fit"
name: sales-v-temp3
---
fig, ax = plt.subplots()
df.plot(x='X',y='Y', kind='scatter', ax=ax)
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
df.plot(x='X',y='Y_hat', kind='line', ax=ax)
```

Expand All @@ -114,8 +127,14 @@ df['Y_hat'] = α + β * df['X']
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Scatter plot with a line of fit"
name: sales-v-temp4
---
fig, ax = plt.subplots()
df.plot(x='X',y='Y', kind='scatter', ax=ax)
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
```

Expand All @@ -139,9 +158,15 @@ df
```

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Plot of the residuals"
name: plt-residuals
---
fig, ax = plt.subplots()
df.plot(x='X',y='Y', kind='scatter', ax=ax)
df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
plt.vlines(df['X'], df['Y_hat'], df['Y'], color='r');
```

Expand Down Expand Up @@ -181,6 +206,12 @@ for β in np.arange(20,100,0.5):
Plotting the error

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Plotting the error"
name: plt-errors
---
ax = pd.Series(errors).plot(xlabel='β', ylabel='error')
plt.axvline(β_optimal, color='r');
```
Expand All @@ -196,6 +227,12 @@ for α in np.arange(-500,500,5):
Plotting the error

```{code-cell} ipython3
---
mystnb:
figure:
caption: "Plotting the error (2)"
name: plt-errors2
---
ax = pd.Series(errors).plot(xlabel='α', ylabel='error')
plt.axvline(α_optimal, color='r');
```
Expand Down Expand Up @@ -327,12 +364,18 @@ print(α)
Now we can plot the OLS solution
```{code-cell} ipython3
---
mystnb:
figure:
caption: "OLS line of best fit"
name: plt-ols
---
df['Y_hat'] = α + β * df['X']
df['error'] = df['Y_hat'] - df['Y']
fig, ax = plt.subplots()
df.plot(x='X',y='Y', kind='scatter', ax=ax)
df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
plt.vlines(df['X'], df['Y_hat'], df['Y'], color='r');
```
Expand Down

0 comments on commit 8fa667b

Please sign in to comment.