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

会有一股神秘感。 提交于 2019-11-29 16:58:32

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!