Dynamically identify function

I am trying to write a curve fitting function that returns the optimal parameters a, b and c, here is a simplified example:

import numpy
import scipy
from scipy.optimize import curve_fit

def f(x, a, b, c):
    return x * 2*a + 4*b - 5*c

xdata = numpy.array([1,3,6,8,10])
ydata = numpy.array([  0.91589774,   4.91589774,  10.91589774,  14.91589774,  18.91589774])
popt, pcov = scipy.optimize.curve_fit(f, xdata, ydata)

This works fine, but I want to give the user the opportunity to provide some (or none) of the parameters a, b or c, in which case they should be considered as constants and not evaluated. How to write fso that it matches only parameters not provided by the user?

Basically, I need to dynamically determine fusing the correct arguments. For example, if it awas known to the user, fit becomes:

def f(x, b, c):
    a = global_version_of_a
    return x * 2*a + 4*b - 5*c
+5
source share
5 answers

collection.namedtuple playbook, exec "" func:

import numpy as np
import scipy.optimize as optimize
import textwrap

funcstr=textwrap.dedent('''\
def func(x, {p}):
    return x * 2*a + 4*b - 5*c
''')
def make_model(**kwargs):
    params=set(('a','b','c')).difference(kwargs.keys())
    exec funcstr.format(p=','.join(params)) in kwargs
    return kwargs['func']

func=make_model(a=3, b=1)

xdata = np.array([1,3,6,8,10])
ydata = np.array([  0.91589774,   4.91589774,  10.91589774,  14.91589774,  18.91589774])
popt, pcov = optimize.curve_fit(func, xdata, ydata)
print(popt)
# [ 5.49682045]

func=make_model(a=3, b=1)

, makemodel. , make_model, func. , optimize.curve_fit .

, , a = 3 b = 1 func. exec func. func x c. , popt - 1, c.


textwrap.dedent: textwrap.dedent . " " script, funcstr , textwrap.dedent

def foo():
    funcstr=textwrap.dedent('''\
        def func(x, {p}):
            return x * 2*a + 4*b - 5*c
        ''')

def foo():
    funcstr='''\
def func(x, {p}):
    return x * 2*a + 4*b - 5*c
'''

def foo():
    funcstr=(
        'def func(x, {p}):\n'
        '    return x * 2*a + 4*b - 5*c'
        )

EOL . , .

+5

.

user_b, user_c = get_user_vals()
opt_fun = lambda x, a: f(x, a, user_b, user_c)
popt, pcov = scipy.optimize.curve_fit(opt_fun, xdata, ydata)
+1

, :

https://lmfit.imtqy.com/lmfit-py/index.html

README:

"LMfit-py , . . , ".

+1

curve_fit, . :

import numpy
from scipy.optimize import curve_fit

class FitModel(object):
    def f(self, x, a, b, c):
        return x * 2*a + 4*b - 5*c

    def f_a(self, x, b, c):
        return self.f(x, self.a, b, c)

# user supplies a = 1.0
fitModel = FitModel()
fitModel.a = 1.0

xdata = numpy.array([1,3,6,8,10])
ydata = numpy.array([  0.91589774,   4.91589774,  10.91589774,  14.91589774,  18.91589774])
initial = (1.0,2.0)
popt, pconv = curve_fit(fitModel.f_a, xdata, ydata, initial)
0
def f(x, a = 10, b = 15, c = 25):
    return x * 2*a + 4*b - 5*c

If the user does not provide an argument for the parameter in question, everything that you specify on the right side of the sign =will be used:

e.g. .:
f(5, b = 20)will be evaluated before return 5 * 2*10 + 4*20 - 5*25and
f(7)will be evaluated untilreturn 7 * 2*10 + 4*15 - 5*25

-2
source

All Articles