Skip to content
Advertisement

How to find the regression line for multiple independent variables?

I’m trying to understand how the Multiple Line Regression works in code for machine learning. The issue I’m having is that I don’t get how to set up my regression line properly or if my coefficients are correct.

So I guess I can divide my thoughts into three questions.

  • Is my method of finding the coefficients for the regression line correct?
  • Is my method of setting up the regression line correct?
  • Is my method of plotting correct?

My code in Python 3.8.5:

JavaScript

My list of data (cars.csv)

JavaScript

Advertisement

Answer

In order,

  1. The method appears to be correct but rather long-winded. See below for a more compact alternative
  2. Not sure what you mean but I think this:
JavaScript

is not quite correct. The coefficients in coeffs_multi_reversed are in order dictated by X namely ‘constant’, ‘Weight’, ‘Volume’. In coeffs_multi they are then ‘Volume’, ‘Weight’, ‘constant’, so the above are in the wrong order

  1. For the plot I would not do x1, y1 etc but simply plot actual vs predicted by the model, like so:
JavaScript

the graph then looks like this: fit

  1. A much more standard way to do regression is as follows
JavaScript

prints

JavaScript

and plots

fit2

Edit: plotting 3D grid

To plot predicted output on a grid, you can do something like

JavaScript

for this kind of output:

fit3

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement