Least-square fitting

We want to find the best that minimizes the error .

The brute-force strategy is to find the solution for the equation . Because as a projection operator, keeps only the part of that is representable by the basis vectors from , effectively removing the error :

In Python, least square fitting is done with numpy.linalg.lstsq, or scipy.linalg.lstsq.

Polynomial fitting (with numpy.polyfit) is a special case for least-square fitting, where is a Vandermonde matrix (numpy.vander).