简体   繁体   English

在python中打印表面拟合方程

[英]print surface fit equation in python

I'm trying to fit a surface model to a 3D data-set (x,y,z) using matplotlib. 我正在尝试使用matplotlib将表面模型拟合到3D数据集(x,y,z)。
Where z = f(x,y) . 其中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. 谢谢。

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). 根据函数scipy.linalg.lstsq http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.linalg.lstsq.html的文档,估计的系数应该存储在你的变量中C(与A中的列对应的顺序)。

To print your equation with estimated coefficients showing 2 digits after decimal point: 要使用小数点后2位数的估计系数打印等式:

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 (eg check Run an OLS regression with Pandas Data Frame ) 顺便说一句,库pandasstatsmodels对这类任务非常有用(例如,检查使用Pandas Data Frame运行OLS回归

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM