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)))