Use optimize.minimize from scipy with 2 variables and interpolated function

后端 未结 2 1474
挽巷
挽巷 2021-02-11 02:27

I didn\'t find a way to perform optimize.minimize from scipy with a multidimensional function. In nearly all examples an analytical function is optimized while my function is in

相关标签:
2条回答
  • 2021-02-11 02:46

    One more possible solution (hope you get the idea):

    One more function is created (f), and the minimized values are sent as arguments to this function.

    from scipy.optimize import minimize
    
    x = data.Height.values
    y = data.Weight.values
    
    def f(params):
        w0, w1 = params
        return mse(w0, w1, x, y)
    
    optimum = minimize(f, (0,0), method = 'L-BFGS-B', bounds = ((-100, 100), (-5,5)) )
    w0 = optimum.x[0]
    w1 = optimum.x[1]
    

    Also tried implementation with lambda function, but had no luck.

    0 讨论(0)
  • 2021-02-11 02:54

    First, to find global maximum (instead of minimum) you need to interpolate your function with opposite sign:

    F2 = interp2d(x, y, -z)
    

    Second, the callable in minimize takes a tuple of arguments, and interp2d object needs input coordinates to be given as separate positional arguments. Therefore, we cannot use interp2d object in minimize directly; we need a wrapper that will unpack a tuple of arguments from minimize and feed it to interp2d:

    f = lambda x: F2(*x)
    

    And third, to use minimize you need to specify an initial guess for minimum (and bounds, in your case). Any reasonable point will do:

    x0 = (2200, 12)
    bounds = [(2000,3500),(10,50)]
    print minimize(f, x0, method='SLSQP', bounds=bounds)
    

    This yields:

      status: 0
     success: True
        njev: 43
        nfev: 243
         fun: array([-59.99999488])
           x: array([ 2500.00002708,    24.99999931])
     message: 'Optimization terminated successfully.'
         jac: array([ 0.07000017,  1.        ,  0.        ])
         nit: 43
    
    0 讨论(0)
提交回复
热议问题