4

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?

10
  • I think the documentation can help you, it's not possible out of the box. Commented Dec 7, 2015 at 19:02
  • I did RTFM - but as a novice programmer I was hoping there was a trick I might be missing. Commented Dec 7, 2015 at 20:15
  • haha, kk, not a pro in this either, but what about variable numbers of argument for your lambda, as in here? Commented Dec 7, 2015 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. Commented Dec 7, 2015 at 21:10
  • 1
    done, sorry about the delay Commented Dec 11, 2015 at 15:13

4 Answers 4

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] 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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) 

1 Comment

Apparently, I hadn't heard of lambda functions at the point I wrote that answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.