3

I have a fitting function which has the form:

def fit_func(x_data, a, b, c, N)

where a, b, c are lists of lenth N, every entry of which is a variable parameter to be optimized in scipy.optimize.curve_fit(), and N is a fixed number used for loop index control.

Following this question I think I am able to fix N, but I currently am calling curve_fit as follows:

params_0 = [a_init, b_init, c_init]
popt, pcov = curve_fit(lambda x, a, b, c: fit_func(x, a, b, c, N), x_data, y_data, p0=params_0)

I get an error: lambda() takes exactly Q arguments (P given)

where Q and P vary depending on how I am settings things up.

So: is this even possible, for starters? Can I pass lists as arguments to curve_fit and have the behavior I am hoping for wherein it treats list elements as individual parameters? And assuming that the answer is yes, what I am doing wrong with my function call?

Community
  • 1
  • 1
KBriggs
  • 1,220
  • 2
  • 18
  • 43
  • I think the [documentation](http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.curve_fit.html) can help you, it's not possible out of the box. – lhcgeneva Dec 07 '15 at 19:02
  • I did RTFM - but as a novice programmer I was hoping there was a trick I might be missing. – KBriggs Dec 07 '15 at 20:15
  • haha, kk, not a pro in this either, but what about variable numbers of argument for your lambda, as in [here](http://stackoverflow.com/questions/2914883/varargs-in-lambda-functions-in-python)? – lhcgeneva Dec 07 '15 at 20:44
  • I think the issue here is that curve_fit() does not know how to vary paramters that aren't simple numerical paramters. Though it looks like I might be able to bypass curve_fit and use leastsq directly, since it accepts a parameter tuple of arbitrary length in a residual function. – KBriggs Dec 07 '15 at 21:10
  • Yep, that's probably possible, but I was thinking more in the direction of dynamically 'expanding' each list into individual variables as you are passing them to curve_fit – lhcgeneva Dec 07 '15 at 21:12
  • Ah, interesting. I'll try to hack a script together that does that. – KBriggs Dec 07 '15 at 22:13
  • It's a bit simpler than that - you just need a wrapper_func(x, *params) and map those to variables that fit_finc() can understand, and call wrapper_func() with curve_fit() – KBriggs Dec 08 '15 at 20:49
  • If you want and feel like sharing your solution, you could write an answer for your own question, I'd be quite interested in seeing it, anyway... – lhcgeneva Dec 09 '15 at 12:00
  • 1
    done, sorry about the delay – KBriggs Dec 11 '15 at 15:13
  • I have [another suggestion](https://stackoverflow.com/questions/58463550/using-scipy-curve-fit-with-variable-number-of-parameters-to-optimize/58463551#58463551) which might be more intuitive – Alejandro Oct 19 '19 at 12:18

4 Answers4

5

The solution here is to write a wrapper function that takes your argument list and translates it to variables that the fit function understands. This is really only necessary since I am working qwith someone else's code, in a more direct application this would work without the wrapper layer. Basically

def wrapper_fit_func(x, N, *args):
    a, b, c = list(args[0][:N]), list(args[0][N:2*N]), list(args[0][2*N:3*N])
    return fit_func(x, a, b, c, N)

and to fix N you have to call it in curve_fit like this:

popt, pcov = curve_fit(lambda x, *params_0: wrapper_fit_func(x, N, params_0), x, y, p0=params_0)

where

params_0 = [a_1, ..., a_N, b_1, ..., b_N, c_1, ..., c_N]
KBriggs
  • 1,220
  • 2
  • 18
  • 43
1

I was able to solve the same problem a little bit differently. I used scip.optimize.least_squares for solving rather than curv_fit. I have discussed my solution under the link- https://stackoverflow.com/a/60409667/11253983

codingtoddler
  • 41
  • 1
  • 6
0

Please take a look at this post https://stackoverflow.com/a/73951825/20160627, where I propose a use of scipy.optimize.curve_fit with arbitrary number and positioning of parameters to fit or fix in a list

0

Probably not an optimal solution, but I ran into a similar issue. The way I solved it was to hard code "pass" functions that literally output my generalized function, but require a specific number of parameters.

# Defines General Temperature Function
def general_temp_function(t, a, *params):
   # Seperates params arguments in respective lists, i.e.
   # general_temp_regress(t, a, b1, tau1, b2, tau2...)
   # b_list = [b1, b2, ...]
   # tau_list = [tau1, tau2, ...]
   b_list = []
   tau_list = []
   for i in range(0, len(params) // 2):
      pair = [params[i * 2], params[i * 2 + 1]]
      b_list.append(pair[0])
      tau_list.append(pair[1])

   # Calculates
   N = len(params) // 2
   sum_ = 0
   for term in range(0, N):
      sum_ += b_list[term] * np.exp(-t / tau_list[term])

   return a + sum_ 

# Defines Passing Functions
def pass1(t, a, b1, tau1):
   return general_temp_function(t, a, b1, tau1)
def pass2(t, a, b1, tau1, b2, tau2):
   return general_temp_function(t, a, b1, tau1, b2, tau2)
def pass3(t, a, b1, tau1, b2, tau2, b3, tau3):
   return general_temp_function(t, a, b1, tau1, b2, tau2, b3, tau3)
def pass4(t, a, b1, tau1, b2, tau2, b3, tau3, b4, tau4):
   return general_temp_function(t, a, b1, tau1, b2, tau2, b3, tau3, b4, tau4)
def pass5(t, a, b1, tau1, b2, tau2, b3, tau3, b4, tau4, b5, tau5):
   return general_temp_function(t, a, b1, tau1, b2, tau2, b3, tau3, b4, tau4, b5, tau5)

T_fit1_N1params, T_fit1_N1covars = curve_fit(pass1, t, T_data1)