How to determine the best fit slope of a line? I was going back through linear regression and I found two equations for finding the best fit slope given X and y. This is one: $$m=\frac{\sum_{i=0}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=0}^{n}(x_i - \bar{x}^2)}$$ and this is the other: $$m=\frac{\bar{x}.\bar{y}-\bar{xy}}{\bar{x}^2 - \bar{x^2}}$$ I programmed both of them with Numpy and tested the second implementation with the same set of numbers and got the same slope value, but the first implementation yielded different results). What's the difference between those three and which should I use?
Here is my implementation of the first equation:
m = (np.sum(np.dot(X - np.mean(X), y - np.mean(y)))) / (np.sum(np.square(X - np.mean(X))))

and the second:
m = (np.dot(np.mean(X), np.mean(y)) - np.mean(np.dot(X, y))) / (np.square(np.mean(X)) - np.mean(np.square(X)))


It looks like there are a couple of problems.
First, the python code for the 1st equation does not implement it correctly. The first line of code (for the first equation in the OP) should be
m = (np.sum(np.dot(X - np.mean(X), y - np.mean(y)))) / np.sum(X - np.square(np.mean(X)))

However the first equation is wrong.  It's not:
$$m=\frac{\sum_{i=0}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=0}^{n}(x_i - \bar{x}^2)}$$
It's
$$
m=\frac{\sum_{i=0}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=0}^{n}(x_i - \bar{x})^2}
$$
So in (Num)Python that would be:
m = (np.sum(np.dot(X - np.mean(X), y - np.mean(y)))) / (np.sum(np.square(X - np.mean(X))))

