4

I'm trying to fit a surface model to a 3D data-set (x,y,z) using matplotlib.
Where z = f(x,y).
So, I'm going for the quadratic fitting with equation:

f(x,y) = ax^2+by^2+cxy+dx+ey+f 

So far, I have successfully plotted the 3d-fitted-surface using least-square method using:

# best-fit quadratic curve    
   A = np.c_[np.ones(data.shape[0]), data[:,:2], np.prod(data[:,:2], axis=1), data[:,:2]**2]    
   C,_,_,_ = scipy.linalg.lstsq(A, data[:,2])    
   #evaluating on grid      
   Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX*YY, XX**2, YY**2], C).reshape(X.shape)

But, how can I be able to print/get the fitted equation of the surface(with coefficient values) ?

I little help will be highly appreciated.
thank you.

diffracteD
  • 758
  • 3
  • 10
  • 32
  • Can you post the code corresponding to "So far, I have successfully plotted the 3d-fitted-surface using least-square method"? – etna May 14 '15 at 11:32
  • @etna added the fitting section used as per your comment. – diffracteD May 14 '15 at 11:37
  • 1
    Ok... according to the documentation of the function scipy.linalg.lstsq http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.linalg.lstsq.html the estimated coefficients should be stored in your variable `C` so `print C` seems a reasonable thing to do :) – etna May 14 '15 at 11:41
  • @etna wow! that did the work... But can I print the equation on canvas with the corresponding coefficients ? – diffracteD May 14 '15 at 11:47
  • @etna what is the order of printed coefficients w.r.t. the a,b,c,d,e,f sequence ? – diffracteD May 14 '15 at 11:54
  • 1
    The same as in A so here constant first etc. A way to print your equation is this: `print 'f(x,y) = {:.2f}x^2+{:.2f}y^2+{:.2f}xy+{:.2f}x+{:.2f}y+{:.2f}'.format(C[4],C[5],C[3],C[1],C[2],C[0])` (But make sure I ordered the coeffs right). Another way: `print 'f(x,y) = {4:.2f}x^2+{5:.2f}y^2+{3:.2f}xy+{1:.2f}x+{2:.2f}y+{0:.2f}'.format(*C)`. – etna May 14 '15 at 11:58

1 Answers1

3

According to the documentation of the function scipy.linalg.lstsq http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.linalg.lstsq.html the estimated coefficients should be stored in your variable C (the order corresponding to columns in A).

To print your equation with estimated coefficients showing 2 digits after decimal point:

print 'f(x,y) = {:.2f}x^2+{:.2f}y^2+{:.2f}xy+{:.2f}x+{:.2f}y+{:.2f}'.format(C[4],C[5],C[3],C[1],‌​C[2],C[0])

or:

print 'f(x,y) = {4:.2f}x^2+{5:.2f}y^2+{3:.2f}xy+{1:.2f}x+{2:.2f}y+{0:.2f}'.format(*C)

By the way, libraries pandas and statsmodels can be very helpful for this kind of task (e.g. check Run an OLS regression with Pandas Data Frame )

Community
  • 1
  • 1
etna
  • 1,083
  • 7
  • 13
  • thanks for the suggestion regarding panda. But can I also check the goodness of fit(or p-value) using the current code. ? – diffracteD May 14 '15 at 12:31
  • To the best of my knowledge, there is no "shortcut" so you need to compute the coefficients' standard errors (e.g. https://thetarzan.wordpress.com/2012/10/27/calculate-ols-regression-manually-in-python-using-numpy/), then compute the t statistic and finally get your p value... – etna May 14 '15 at 21:22
  • Do this least square fitting function provide us some R value ? I'm not being able to locate it. – diffracteD May 15 '15 at 15:16