了解scipy.optimize.minimize()函数的错误 [英] Understanding the error for scipy.optimize.minimize() function
问题描述
我有一个简单的方程式,通过它绘制
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 = chernoff_bound(betas)
plt.plot(betas, c_bound)
plt.title('Chernoff Bound')
plt.ylabel('P(error)')
plt.xlabel('parameter beta')
plt.show()
现在,我想找到P(error)最小的值.
我试图通过scipy.optimize.minimize()
函数来做到这一点(老实说,我以前没有使用过它,这里可能存在一些思想上的错误……)
Now, I want to find the value where P(error) is minimum.
I tried to do it via the scipy.optimize.minimize()
function (honestly, I haven't used it before and there is maybe some error of thought here ...)
from scipy.optimize import minimize
x0 = [0.1,0.2,0.4,0.5,0.9]
fun = lambda x: 0.5 * np.exp(-x * (1-x))
res = minimize(fun, x0, method='Nelder-Mead')
我得到的错误是:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-2b04597c4341> in <module>()
3 x0 = [0.1,0.2,0.4,0.5,0.9]
4 fun = lambda x: 0.5 * np.exp(-x * (1-x))
----> 5 res = minimize(fun, x0, method='Nelder-Mead')
6 print(res)
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
364
365 if meth == 'nelder-mead':
--> 366 return _minimize_neldermead(fun, x0, args, callback, **options)
367 elif meth == 'powell':
368 return _minimize_powell(fun, x0, args, callback, **options)
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/scipy/optimize/optimize.py in _minimize_neldermead(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, return_all, **unknown_options)
436 if retall:
437 allvecs = [sim[0]]
--> 438 fsim[0] = func(x0)
439 nonzdelt = 0.05
440 zdelt = 0.00025
ValueError: setting an array element with a sequence.
如果有人能在这里指出我正确的方向,我将不胜感激!
I would very much appreciate it if someone can point me to the right track here!
推荐答案
optimize.minimize
的第二个参数是一个初始猜测-您对希望optimize.minimize
找到的最小x
值的猜测.例如,
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)
收益
status: 0
nfev: 36
success: True
fun: 0.38940039153570244
x: array([ 0.5])
message: 'Optimization terminated successfully.'
nit: 18
x0
不必总是标量.它可能是一个数组-取决于fun
.在上面的示例中,x0 = np.array([0.1])
也将起作用.关键是无论您猜什么,fun(x0)
应该是一个标量.
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.
这篇关于了解scipy.optimize.minimize()函数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!