gpt4 book ai didi

python - 在python中打印表面拟合方程

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:06 24 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 将表面模型拟合到 3D 数据集 (x,y,z)。
其中 z = f(x,y)
所以,我要用方程进行二次拟合:

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

到目前为止,我已经使用最小二乘法成功绘制了 3d 拟合曲面:

# 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)

但是,我怎样才能打印/得到表面的拟合方程(带有系数值)?

我将不胜感激。
谢谢。

最佳答案

根据函数 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)

顺便说一下,库 pandasstatsmodels 对这类任务很有帮助(例如检查 Run an OLS regression with Pandas Data Frame )

关于python - 在python中打印表面拟合方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30235414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com