Lesson 38 of 60 · python
Seaborn Regression Plots
Duration: 20 minutes
Regression Plots in Seaborn
Understanding relationships between variables is crucial for data science.
regplot – simple linear regression
sns.regplot(x='hours', y='score', data=tips, scatter_kws={'alpha':0.6}, line_kws={'color':'red'})
plt.title('Regression of Score on Study Hours')
plt.show()
lmplot – regression with facets
sns.lmplot(x='hours', y='score', hue='sex', data=tips, col='day', height=4, aspect=1)
plt.show()
Residual plot
sns.residplot(x='hours', y='score', data=tips, lowess=True, color='green')
plt.title('Residual Plot')
plt.show()
Polynomial regression (degree=2)
sns.lmplot(x='hours', y='score', data=tips, order=2, ci=None, line_kws={'color':'purple'})
plt.show()
Interpreting the plots
- Slope indicates direction/magnitude of relationship.
- Confidence interval (shaded area) shows uncertainty.
- Residuals help assess model fit; random scatter suggests a good fit.