what should be the parameters in residplot seaborn

Viewed 1667

I have made a simple linear regression model:

LR = LinearRegression()
kfold = model_selection.KFold(n_splits=10, random_state=12)
result_kfold = model_selection.cross_val_score(LR, X_train, Y_train, cv=kfold, scoring = 'r2')
print("Accuracy: %.2f%%" % (result_kfold.mean()*100.0)) 
LR.fit(X_train,Y_train)
Y_pred = LR.predict(X_test)
print("Y_pred:", Y_pred)

i want to plot the residual errors. I've used 'residplot' for the same. But i'm not sure if i've passed the right arguements. According to the documentation, we've to use predictor variable and result/response variable. Here's the code:

sns.set(style="whitegrid")
sns.residplot(Y_test, Y_pred, lowess=True, color="g")

Can anyone please tell me if it is right...also what should be the labels of X and Y axis?

Thank You in advance for help

1 Answers

You are plotting something very weird, so let's use an example dataset:

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import matplotlib. pyplot as plt
import seaborn as sns

iris = sns.load_dataset('iris')

X_train, X_test, Y_train, Y_test = train_test_split(iris.iloc[:,:3], iris.iloc[:,3],random_state=11)

LR = LinearRegression()
LR.fit(X_train,Y_train)
Y_pred = LR.predict(X_test)

If you just want to plot the residuals, you can do:

sns.set(style="whitegrid")
fig, ax = plt.subplots(figsize =(5,5))
sns.regplot(x=Y_pred,y=Y_test-Y_pred,ax=ax,lowess=True)
ax.set(ylabel='residuals',xlabel='fitted values')

enter image description here

What you are getting with sns.regplot() is the y variable regressed onto the x-variable and the residuals being plotted, which makes no sense in your case, and I illustrate below how the plot is obtained, first you fit the prediction (y variable) to actual (x variable), and get the residuals:

plotfit = LinearRegression()
plotfit.fit(Y_test.to_numpy().reshape(-1,1),Y_pred)
residual = Y_pred - plotfit.predict(Y_test.to_numpy().reshape(-1,1))

Then plotting it gives you exactly the same thing as your sns.residplot:

sns.set(style="whitegrid")
fig, ax = plt.subplots(1,2,figsize =(10,5))

sns.residplot(Y_test,Y_pred,lowess=True, color="g",ax=ax[0])
ax[0].set_xlim(0,2.5)
sns.regplot(x=Y_test,y=residual,lowess=True)
ax[1].set_xlim(0,2.5)

enter image description here

Related