我试图用matplotlib来拟合三维数据集(x,y,z)的曲面模型。
在哪里z = f(x,y)。
所以,我要用方程进行二次拟合:
f(x,y) = ax^2+by^2+cxy+dx+ey+f 到目前为止,我已经用最小二乘法成功地绘制了三维拟合曲面:
# 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)但是,我如何能够打印/得到曲面的拟合方程(有系数值)?
我会非常感激你的帮助。
谢谢。
发布于 2015-05-14 12:08:24
根据函数scipy.linalg.lstsq http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.linalg.lstsq.html的文档,估计的系数应该存储在变量C中(对应于A中列的顺序)。
若要使用估计系数在小数点后显示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])或者:
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)顺便说一句,库pandas和statsmodels对于这类任务非常有用(例如,检查Run an OLS regression with Pandas Data Frame )。
https://stackoverflow.com/questions/30235414
复制相似问题