python - Scipy, differential evolution -


the thing is, im trying design of fitting procedure purposes , want use scipy`s differential evolution algorithm general estimator of initial values used in lm algorithm better fitting. function want minimize de least squares between analytically defined non-linear function , experimental values. point @ stuck function design. stated in scipy reference: "function must in form f(x, *args) , x argument in form of 1-d array , args tuple of additional fixed parameters needed specify function"

there ugly example of code wrote illustrative purposes:

def func(x, *args):     """args[0] = x        args[1] = y"""     result = 0     in range(len(args[0][0])):         result += (x[0]*(args[0][0][i]**2) + x[1]*(args[0][0][i]) + x[2] - args[0][1][i])**2        return result**0.5  if __name__ == '__main__':     bounds = [(1.5, 0.5), (-0.3, 0.3), (0.1, -0.1)]     x = [0,1,2,3,4]     y = [i**2 in x]     args = (x, y)     result = differential_evolution(func, bounds, args=args)     print(func(bounds, args)) 

i wanted supply raw data tuple function seems not how suppose since interpreter isn't happy function. problem should easy solvable, frustrated, advice appreciated.

this kinda straightforward solution shows idea, code isn`t pythonic simplicity think enough. ok example want fit equation of kind y = ax^2 + bx + c data obtained equation y = x^2. obvious parameter = 1 , b,c should equal 0. since differential evolution algorithm finds minimum of function want find minimum of root mean square deviation (again, simplicity) of analytic solution of general equation (y = ax^2 + bx + c) given parameters (providing initial guess) vs "experimental" data. so, code:

from scipy.optimize import differential_evolution  def func(parameters, *data):      #we have 3 parameters passed parameters ,     #"experimental" x,y passed data      a,b,c = parameters     x,y = data      result = 0      in range(len(x)):         result += (a*x[i]**2 + b*x[i]+ c - y[i])**2      return result**0.5  if __name__ == '__main__':     #initial guess variation of parameters     #                        b            c     bounds = [(1.5, 0.5), (-0.3, 0.3), (0.1, -0.1)]      #producing "experimental" data      x = [i in range(6)]     y = [x**2 x in x]      #packing "experimental" data args     args = (x,y)      result = differential_evolution(func, bounds, args=args)     print(result.x) 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -