Search code examples
pythonmatplotlibmachine-learningregressionlinear-regression

How do I plot for Multiple Linear Regression Model using matplotlib


I try to Fit Multiple Linear Regression Model

Y= c + a1.X1 + a2.X2 + a3.X3 + a4.X4 +a5X5 +a6X6

Had my model had only 3 variable I would have used 3D plot to plot. How can I plot this . I basically want to see how the best fit line looks like or should I plot multiple scatter plot and see the effect of individual variable Y = a1X1 when all others are zero and see the best fit line. What is the best approach for these models. I know it is not possible to visualize higher dimensions want to know what should be the best approach. I am desperate to see the best fit line


Solution

  • I found this post which is more helpful and followed
    https://stats.stackexchange.com/questions/73320/how-to-visualize-a-fitted-multiple-regression-model. Based on suggestions I am currently just plotting scatter plots like dependent variable vs. 1st independent variable, then vs. 2nd independent variable etc I am doing same thing . I may not be able to see best fit line for complete model but I know how it is dependent on individual variable

    from sklearn.linear_model import LinearRegression
    train_copy = train[['OverallQual', 'AllSF','GrLivArea','GarageCars']]
    train_copy =pd.get_dummies(train_copy)
    train_copy=train_copy.fillna(0)
    linear_regr_test = LinearRegression()
    
    fig, axes = plt.subplots(1,len(train_copy.columns.values),sharey=True,constrained_layout=True,figsize=(30,15))
    
    for i,e in enumerate(train_copy.columns):
      linear_regr_test.fit(train_copy[e].values[:,np.newaxis], y.values)
      axes[i].set_title("Best fit line")
      axes[i].set_xlabel(str(e))
      axes[i].set_ylabel('SalePrice')
      axes[i].scatter(train_copy[e].values[:,np.newaxis], y,color='g')
      axes[i].plot(train_copy[e].values[:,np.newaxis], 
      linear_regr_test.predict(train_copy[e].values[:,np.newaxis]),color='k')