I have python code that produces a list of 3-tuples of numbers x, y and z. I would like to fit z= f(x,y) using scipy curve_fit. Here is some non-working code
A = [(19,20,24), (10,40,28), (10,50,31)]
def func(x,y,a, b):
return x*y*a + b
How can I get python to fit this function to the data in the list A
?
func
must be the data (both x and y).func
represent the parameters.So you need to modify your func
a bit:
def func(data, a, b):
return data[:,0]*data[:,1]*a + b
curve_fit
is the function.data
(x
and y
in the
form of one array).z
).a
and b
in this case.)So, for example:
params, pcov = optimize.curve_fit(func, A[:,:2], A[:,2], guess)
import scipy.optimize as optimize
import numpy as np
A = np.array([(19,20,24), (10,40,28), (10,50,31)])
def func(data, a, b):
return data[:,0]*data[:,1]*a + b
guess = (1,1)
params, pcov = optimize.curve_fit(func, A[:,:2], A[:,2], guess)
print(params)
# [ 0.04919355 6.67741935]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With