【发布时间】:2020-09-04 02:16:47
【问题描述】:
我一直在使用 scipy 将 3d 数据拟合到表面,该表面被定义为多项式函数。但结果看起来与数据并没有那么接近。如何改善拟合?
import numpy as np
from scipy.optimize import curve_fit
# import my data
data = my_data_matrix
# define polynomial function
def func(X, A, B, C, D, E, F):
# unpacking the multi-dim. array column-wise, that's why the transpose
x, y, z = X.T
return (A * x ** 2) + (B * y ** 2) + (C * x * y) + (D * x) + (E * y) + F
# fit the polynomial function to the 3d data
popt, _ = curve_fit(func, data, data[:,2])
# print coefficients of the polynomial function, i.e., A, B, C, D, E and F
from string import ascii_uppercase
for i, j in zip(popt, ascii_uppercase):
print(f"{j} = {i:.3f}")
在这种情况下,我得到了:
A = 0.903
B = 0.022
C = 0.325
D = -362.140
E = -52.875
F = 31057.352
拟合曲面与原始数据(散点)进行比较:
【问题讨论】:
-
嗨,你能发布一个完整的工作示例,包括数据吗?你能更准确地说“看起来不太近”是什么意思吗?
-
顺便说一句...我会定义
...+ 2 * C * x * y + ...
标签: python math scipy data-fitting