pure python polyfit Wiki
python2/3: compute polyfit (1D, 2D, N-D) without thirdparty libraries
Brought to you by:
domsen
Welcome to pure python polyfit, the polynomial fitting without any third party module like numpy, scipy, etc.
You can fit polynomials in 1D, 2D or generally in N-D.
Also you can solve a system of least squares or compute A= Q*R for a matrix A.
It is all based on list representations of coordinates and matrices.
The functionality is tested with the unit test script "testPurePythonPolyFit.py". This test actually needs numpy for verification.
In this section you can find examples for polyfitting, QR decomposition and least squares solving
(conceptual, see also end of "purePythonPolyFit.py" for example usage):
# estimate 1D, x and y lists of floats
p = PolyFit(x, y, order=2)
# evaluate 1D
p[0.5]
# estimate 2D, pts list of tuples/list of length 2
# (e.g. pts = [(1.0, 4.1),(4.2, 0.1), ...])
# vals: list of floats
p2 = PolyFit2D(pts, vals, order=3)
# evaluate 2D
p2[(0.5, 1.0)]
# estimate N-D, pts list of tuples/list of length N
# (e.g. [(1.0, 4.1, 0, 0),(0, 4.2, 0, 0.1), ...])
# vals: list of floats
pn = PolyFitND(pts, vals, order=3)
# evaluate N-D
pn[(0.5, 1.0, 0.2, 0.2)]
# matrix are represented with lists of lists/tuples:
A=[[1,2,3],[4,5,6]]
# the result:
Q,R = qr(A)
Find a least squares solution x, for the system Ax =y
# a random overdetermined matrix:
A=[[1,2],[2,5],[5,4]]
# a noise measurement vector:
b=[1,2,3]
# the result:
solution, residual = leastSquareSolution(A,b)