How to use seaborn.residplot() In Python
seaborn.residplot(): This function will regress y on x and then plot the residuals as a scatterplot. You can fit a lowess smoother to the residual plot as an option, which can aid in detecting whether the residuals have structure.
Syntax: seaborn.residplot(*, x=None, y=None, data=None, lowess=False, x_partial=None, y_partial=None, order=1, robust=False, dropna=True, label=None, color=None, scatter_kws=None, line_kws=None, ax=None)
Parameters:
- x : column name of the independent variable (predictor) or a vector.
- y: column name of the dependent variable(response) or a vector.
- data: optional parameter. dataframe
- lowess: by default itās false.
Below is an example of a simple residual plot where x(independent variable) is head_size from the dataset and y(dependent variable) is the brain_weight column of the dataset.
Python3
# import packages and libraries import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # reading the csv file data = pd.read_csv( 'headbrain3.csv' ) sns.residplot(x = 'Head_size' , y = 'Brain_weight' , data = data) plt.show() |
Output:
We can see that the points are plotted in a randomly spread, there is no pattern and points are not based on one side so thereās no problem of heteroscedasticity.
How to Create a Residual Plot in Python
A residual plot is a graph in which the residuals are displayed on the y axis and the independent variable is displayed on the x-axis. A linear regression model is appropriate for the data if the dots in a residual plot are randomly distributed across the horizontal axis. Letās see how to create a residual plot in python.
Contact Us