tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
232 stars 17 forks source link

Fitting multidimensional datasets by points #337

Closed nfhktwrbq closed 2 years ago

nfhktwrbq commented 2 years ago

In example Fitting multidimensional datasets shape of x = 50, y = 50 and z = (50, 50). This is a case when z is known for any combination x and y. But I need fit by points (x, y, z) and in this case only diagonal elements - (x1, y1, z1) ... (xn, yn, zn) are known. How can I fit data like this?

tBuLi commented 2 years ago

What is the model to which you would like to fit?

nfhktwrbq commented 2 years ago

What is the model to which you would like to fit?

Polinomial model_dict = { z: Poly({(0, 0): p00, (1, 0): p10, (0, 1): p01, (2, 0): p20, (1, 1): p11, (3, 0): p30, (2, 1): p21}, x, y).as_expr() }

tBuLi commented 2 years ago

Then if I understood everything correctly, you can just use the code from the example but skip the line where we make the meshgrid. So:

x, y, z = variables('x, y, z')
c1, c2 = parameters('c1, c2')
model_dict = {z: Poly( {(1, 2): c1, (4, 5): c2}, x ,y)}
model = Model(model_dict)

xdata = np.array([x1, ..., xn])
ydata = np.array([y1, ..., yn])
zdata = np.array([z1, ..., zn])

fit = Fit(model, x=xdata, y=ydata, z=zdata)
fit_result = fit.execute()

All you have to do is adapt it to your model :).