Understand the idea
A residual is observed y minus predicted y. A residual plot helps inspect what a fitted straight line fails to explain.
A small example
If observed y=12 and predicted y=10, residual=+2. If observed y=8, residual=−2.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(6, 4))
sns.residplot(data=df, x="hours", y="score", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Residual")
fig.tight_layout()
plt.show()What each part does
sns.residplot- x against residuals after a linear fit
zero line- predictions equal observations
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| student | club | hours | score | group |
|---|---|---|---|---|
| Ari | Art | 1 | 52 | A |
| Bo | Code | 3 | 71 | A |
| Cy | Art | 2 | 65 | B |
| Dee | Code | 4 | 82 | B |
| Eli | Art | 5 | 89 | A |
| Flo | Code | 3 | 76 | A |
| Gus | Art | 6 | 93 | B |
| Han | Code | 2 | 61 | B |
Your task · Follow
- Using df, call sns.residplot with x=hours and y=score.
- Chart: title "Study club"; x "hours"; y "Residual".
Hint
Residuals are observed minus predicted, not the original measured y values.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(6, 4))
sns.residplot(data=df, x="hours", y="score", ax=ax)
ax.set(title="Study club", xlabel="hours", ylabel="Residual")
fig.tight_layout()
plt.show()