How to find length of parameter input of function?

104 views Asked by At

I'm trying to use Scipy's ODR to fit various different curves to data. These curves have to be given as an ODR Model, which is defined by a function. This function has two arguments: p and x. p is a list of the parameters that will be optimised. Example:

def f(p, x):
  m, c = p
  return m*x + c

model = Model(f)
data = RealData(xdata, ydata)
odr_setup = ODR(data, model, beta0=[0,0], partol=0.001)
odr_result = odr_setup.run()

My problem: the length of p depends on the function I am fitting. If the length of beta0 and p are not the same, it gives a ValueError. To get round this, I have constructed some nested try-except statements to get the matching length. Is there a more elegant way of achieving the same thing?

Ideally I would like something like ODR(data, model, ***beta0=[0]*n_params***, partol=0.001). How would I find n_params? Or is there a better way?

Thanks!

0

There are 0 answers