i have questions , problems regarding scipy's optimize.minimize routine. minimize function:
f(eta) = sum_i |eta*x_i - y_i|
with regard eta. since not familiar minimize routine , corresponding methods, tried out. however, using method bfgs raises following error:
file "/usr/local/lib/python3.4/dist-packages/scipy/optimize/_minimize.py", line 441, in minimize return _minimize_bfgs(fun, x0, args, jac, callback, **options) file "/usr/local/lib/python3.4/dist-packages/scipy/optimize/optimize.py", line 904, in _minimize_bfgs a1 = - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok indexerror: 0-d arrays can use single () or list of newaxes (and single ...) index
which not able solve. please find code, causes error below. using python3 scipy 0.17.0 , numpy 1.8.2 on ubuntu 14.04.3 lts.
furthermore, method conjugate gradient seems perform worse other methods.
last not least, favour estimating minimum finding 0 of first derivative via scipy.optimize.brentq. fine or recommend approach? prefer robustness on speed.
here code illustrating problems , questions:
from scipy import optimize import numpy np def function(x, bs, cs): sum = 0. b, c in zip(bs, cs): sum += np.abs(x*b - c) return sum def derivativefunction(x, bs, cs): sum = 0. b, c in zip(bs, cs): if x*b > c: sum += b else: sum -= b return sum np.random.seed(1000) bs = np.random.rand(10) cs = np.random.rand(10) eta0 = 0.5 res = optimize.minimize(fun=function, x0=eta0, args=(bs, cs), method='nelder-mead', tol=1e-6) print('nelder-mead:\t', res.x[0], function(res.x[0], bs, cs)) res = optimize.minimize(fun=function, x0=eta0, args=(bs, cs,), method='cg', jac=derivativefunction, tol=1e-6) print('cg:\t', res.x[0], function(res.x[0], bs, cs)) x = optimize.brentq(f=derivativefunction, a=0, b=2., args=(bs, cs), xtol=1e-6, maxiter=100) print('brentq:\t', x, function(x, bs, cs)) #throwing error res = optimize.minimize(fun=function, x0=eta0, args=(bs, cs), method='bfgs', jac=derivativefunction, tol=1e-6) print('bfgs:\t', res.x[0], function(res.x[0], bs, cs))
its output is:
nelder-mead: 0.493537902832 3.71986334101 cg: 0.460178525461 3.72659733011 brentq: 0.49353725172947666 3.71986347245
where first value position of minimum , second value minimum itself. output misses error message above.
thank help!
Comments
Post a Comment