3
votes

I was performing Linear Regression using Numpy and encountered a problem with one of the Equations used to calculate the Slope 'm' and intercept 'b' for the model. Following through an online course, the implementation done by the teacher produced the correct result but was not easy to understand. I was not able to understand this implementation.

Equation to calculate Slope 'a' and intercept 'b'.

Simplified version of the equation

Now the code used to implement this equation using numpy functions:

denominator = X.dot(X) - X.mean() * X.sum()
a = (X.dot(Y) - Y.mean() * X.sum()) / denominator
b = (Y.mean() * X.dot(X) - X.mean() * X.dot(Y)) / denominator

On line 1 of this code: X.dot(X) calculates the Sum Squared X but not the mean. Where as the equation shows mean of X^2.

Why multiply X.mean() * X.sum() to calculate Squared Mean of X?

Why just the X.dot(X) to calculate Mean of Squared X?

Equation 2 States mean(xy)-mean(x)*mean(y)/denominator for calculating a. Where as the code states (x dot y) - mean(y) * sum(x)/denominator? Why?

Thank you

1
Using the simplified version of the equation try to take a part the term 1/N in the numerator and in the denominator.. you will answer it by yourself :) Note that X.mean() is equivalent to X.sum() * 1/N - Roberto Trani

1 Answers

0
votes

It is just a mathematical manipulation of the equation of a. Here you can find the steps from the simplified version to the one used in the code:

enter image description here

The manipulations needed for b are very similar.