Understanding the error for scipy.optimize.minimize() function

前端 未结 1 787
独厮守ぢ
独厮守ぢ 2020-12-21 22:31

I have a simple equation that I plotted via

def chernoff_bound(beta):
    return 0.5 * np.exp(-beta * (1-beta))

betas = np.arange(0, 1, 0.01)
c_bound = cher         


        
相关标签:
1条回答
  • 2020-12-21 23:01

    The second argument of optimize.minimize is an initial guess -- your guess for the minimum x-value that you wish optimize.minimize to find. So, for example,

    import numpy as np
    from scipy import optimize
    x0 = 0.1
    fun = lambda x: 0.5 * np.exp(-x * (1-x))
    res = optimize.minimize(fun, x0, method='Nelder-Mead')
    print(res)
    

    yields

      status: 0
        nfev: 36
     success: True
         fun: 0.38940039153570244
           x: array([ 0.5])
     message: 'Optimization terminated successfully.'
         nit: 18
    

    x0 need not always be a scalar. It could be an array -- it depends on fun. In the above example, x0 = np.array([0.1]) would also work. The key point is that for whatever you guess, fun(x0) should be a scalar.

    0 讨论(0)
提交回复
热议问题