KBriggs
KBriggs

Reputation: 1412

Using scipy curve_fit for a variable number of parameters

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?

Upvotes: 3

Views: 9976

Answers (4)

Jacob Ivanov
Jacob Ivanov

Reputation: 199

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)

Upvotes: 0

Douglas Leite
Douglas Leite

Reputation: 1

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

Upvotes: 0

codingtoddler
codingtoddler

Reputation: 41

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

Upvotes: 1

KBriggs
KBriggs

Reputation: 1412

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]

Upvotes: 5

Related Questions