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
