I want to calculate the least squares estimate for given data.
There are a few ways to do this, one is to use numpy's least squares:
While those two formulas are mathematically equivalent, they are not numerically equivalent! There are better ways to solve a system of linear equations Ax = b than by multiplying both sides by A^(-1), like Gaussian Elimination.
numpy.linalg.lstsq uses this (and more sophisticated) methods to solve the underlying linear system, plus it can handle a lot of corner cases. So use it when you can.
Matrix inversion is very numerically unstable. Don't do it unless you have to.