Skip to content

Commit 052ea5e

Browse files
committed
ensure only one figure is returned
1 parent 8fa667b commit 052ea5e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

lectures/simple_linear_regression.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ We can use a scatter plot of the data to see the relationship between $y_i$ (ice
6161
mystnb:
6262
figure:
6363
caption: "Scatter plot"
64-
name: sales-v-temp
64+
name: sales-v-temp1
6565
---
6666
ax = df.plot(
6767
x='X',
@@ -97,7 +97,8 @@ mystnb:
9797
---
9898
fig, ax = plt.subplots()
9999
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
100-
df.plot(x='X',y='Y_hat', kind='line', ax=ax)
100+
ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax)
101+
plt.show()
101102
```
102103

103104
We can see that this model does a poor job of estimating the relationship.
@@ -113,12 +114,13 @@ df['Y_hat'] = α + β * df['X']
113114
---
114115
mystnb:
115116
figure:
116-
caption: "Scatter plot with a line of fit"
117+
caption: "Scatter plot with a line of fit #2"
117118
name: sales-v-temp3
118119
---
119120
fig, ax = plt.subplots()
120121
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
121-
df.plot(x='X',y='Y_hat', kind='line', ax=ax)
122+
ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax)
123+
plt.show()
122124
```
123125

124126
```{code-cell} ipython3
@@ -130,12 +132,13 @@ df['Y_hat'] = α + β * df['X']
130132
---
131133
mystnb:
132134
figure:
133-
caption: "Scatter plot with a line of fit"
135+
caption: "Scatter plot with a line of fit #3"
134136
name: sales-v-temp4
135137
---
136138
fig, ax = plt.subplots()
137139
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
138-
df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
140+
ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
141+
plt.show()
139142
```
140143

141144
However we need to think about formalizing this guessing process by thinking of this problem as an optimization problem.
@@ -167,7 +170,8 @@ mystnb:
167170
fig, ax = plt.subplots()
168171
ax = df.plot(x='X',y='Y', kind='scatter', ax=ax)
169172
ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g')
170-
plt.vlines(df['X'], df['Y_hat'], df['Y'], color='r');
173+
plt.vlines(df['X'], df['Y_hat'], df['Y'], color='r')
174+
plt.show()
171175
```
172176

173177
The Ordinary Least Squares (OLS) method chooses $\alpha$ and $\beta$ in such a way that **minimizes** the sum of the squared residuals (SSR).
@@ -231,7 +235,7 @@ Plotting the error
231235
mystnb:
232236
figure:
233237
caption: "Plotting the error (2)"
234-
name: plt-errors2
238+
name: plt-errors-2
235239
---
236240
ax = pd.Series(errors).plot(xlabel='α', ylabel='error')
237241
plt.axvline(α_optimal, color='r');

0 commit comments

Comments
 (0)