I'd like to find a least-squares solution for the a
coefficients in
z = (a0 + a1*x + a2*y + a3*x**2 + a4*x**2*y + a5*x**2*y**2 + a6*y**2 +
a7*x*y**2 + a8*x*y)
given arrays x
, y
, and z
of length 20. Basically I'm looking for the equivalent of numpy.polyfit
but for a 2D polynomial.
This question is similar, but the solution is provided via MATLAB.
Here is an example showing how you can use
numpy.linalg.lstsq
for this task:the adjusting coefficients
coeff
are:Note that
coeff[3]
andcoeff[6]
respectively correspond toX**2
andY**2
, and they are close to1.
because the example data was created withZ = X**2 + Y**2 + small_random_component
.