简体   繁体   中英

Scipy fitting with parameters in a vector

As far as I know, the Scipy curve_fit function takes in the fitting parameters explicitly. Eg, when fitting a polynomial:

def func(x, c0, c1, c2):
    return c0 + c1 * x + c2 * x**2

Is there possibly a way (perhaps another equivalent function) to define the parameters via a vector? Eg:

def func(x, C):
    y = 0.0
    for i, ci in enumerate(C):
        y += ci * x**i
    return y

I am trying to fit a complicated function with 24 parameters and explicitly defining parameters is rather painful.

Yes, it is possible, but you must know the number of arguments beforehand (which you seem to).

Example:

from scipy.optimize import curve_fit

def func(x, *C):
    y = sum(c * x ** n for n, c in enumerate(C))
    return y

However, you need to specify the p0 argument in the call to curve_fit ; in this case, since you know you have 24 parameters, if you have an initial guess for their values, you could pass an array containing 24 values. If not, you can just use np.ones(24) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM