python - Minimize function with Scipy minimize -




trying obtain d value (integer) std_diff objective function minimal, using scipy minimize.

my code:

def std_diff(d):     return std(diff(df['bn'].values,d));  scipy.optimize import minimize b=(3,) res = minimize(std_diff,(1,), method='slsqp', bounds = b)  **df['bn'].values**  out[72]:  array([ 2,  2,  2,  2,  3,  2,  7,  5,  7, 11,  8,  2, 11,  7, 15,  8,  7,        12, 21, 19, 32, 35, 40, 35, 21, 19, 25, 20, 40, 80, 99], dtype=int64)  error is"indexerror: many indices array " 

in case not use bounds: res = minimize(std_diff,(1,), method='slsqp'), error:

> in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, > ftol, iprint, disp, eps, callback, **unknown_options) >     368                 fx = float(np.asarray(func(x))) >     369             except: > --> 370                 raise valueerror("objective function must return scalar") >     371             # compute constraints >     372             if cons['eq']: valueerror: objective function must return scalar. 

thank in advance advice.

(i stumbling around @ start, i'll leave here can ideas of how debug.)


you invoke minimize with:

std_diff,(1,) 

that initial values scalar (or 1 number). minimize takes it's clue , sets search variable same. that's d passes function, std_diff. expects function return single value well. in other words, minimize scalar function of scalar value.

so std(diff(df['bn'].values,1)) should return scalar. evidently not.


ok, testing supposed values

in [115]: bf out[115]:  array([ 2,  2,  2,  2,  3,  2,  7,  5,  7, 11,  8,  2, 11,  7, 15,  8,  7,        12, 21, 19, 32, 35, 40, 35, 21, 19, 25, 20, 40, 80, 99], dtype=int64) in [116]: np.std(np.diff(bf,1)) out[116]: 9.9219733700285424 

so first guess wrong.


looking more @ error stack, see error occurs in function, not after. looks problem use of d.

/usr/local/lib/python3.5/dist-packages/numpy/lib/function_base.py in diff(a, n, axis)    1913         raise valueerror( -> 1914             "order must non-negative got " + repr(n))    1915     = asanyarray(a)  valueerror: order must non-negative got array([-64259548.28233695]) 

in unbounded case, search variable can go negative (very so), raising error in np.diff.

(the error show during handling of above exception, exception occurred:. it's not primary error, secondary one.)


the problem when specifying bounds, specification incomplete. requires (min,max) tuple each variable. works:

in [147]: minimize(std_diff,1, method='slsqp', bounds=((3,none),)) ... out[147]:       fun: 9.921973370028542      jac: array([ 64259549.28233695])  message: 'positive directional derivative linesearch'     nfev: 3      nit: 5     njev: 1   status: 8  success: false        x: array([ 1.]) 

bounds variables (only l-bfgs-b, tnc , slsqp). (min, max) pairs each element in x, defining bounds on parameter. use none 1 of min or max when there no bound in direction.

look @ error line:

--> 341         bnderr = bnds[:, 0] > bnds[:, 1] 

it expects bnds 2d array 2 columns. example:

in [153]: np.array(((3,10),)) out[153]: array([[ 3, 10]]) in [154]: np.array((3,)) out[154]: array([3]) 

i modified function have clearer idea of how values changed

def std_diff(d):     print(d)     r = np.std(np.diff(bf,d))     print(r)     return r 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -