pandas - Python Scipy Optimize Error "ValueError: Lengths must match to compare" -
i have python algorithm works fine. input minimum critieria ("minscore") , algorithm runs , preoduces result. typical minscore values between 0.2 , 0.99.
however why use scipy optimize try , find lowest algorithm output minscore value, following error: "valueerror: lengths must match compare".
this how call optimize function:
optimal_score = spo.minimize(brmalg, 0.81, method='slsqp', options={'disp':true}, bounds=[(-1.00,1.00)]) this algorthim errors optimise process:
minholdscore = minscore stocks['hold'] = (stocks['hold'].where(stocks['total score'].shift(1) < minscore, true).where(stocks['total score'].shift(1) >= minholdscore, false).ffill().fillna(false).astype(bool)) this full error message:
> traceback (most recent call last): file "\\python code.py", line 327, in <module> > optimal_score = spo.minimize(brmalg, 0.81, method='slsqp', options={'disp':true}, bounds=[(-1.00,1.00)]) file "c:\\scipy\optimize\_minimize.py", > line 455, in minimize constraints, callback=callback, **options) file "c:\users\\scipy\optimize\slsqp.py", > line 363, in _minimize_slsqp fx = func(x) file "c:\users\\scipy\optimize\optimize.py", > line 289, in function_wrapper return function(*(wrapper_args + args)) file "t:\\python code.py", line 288, in brmalg > stocks[ticker]['hold'] = (stocks[ticker]['hold'].where(stocks[ticker]['total_score_'+marketindex+'_'+str(betawindow)].shift(1) < minbuyscore, true) file "c:\users\\pandas\core\ops.py", > line 740, in wrapper raise valueerror('lengths must match compare') valueerror: lengths must match compare any ideas?? strange works when not using scipy optimize , passing value of minscore = 0.82 example.
look forward advice! :-)
okay think found root cause here.
the docs state scipy.optimize.minimize returns optimization result represented optimizeresult object. important attributes are: x solution array.
in example, optimal_score object. suspect tried pass optimal_score.x float minbuyvalue, it's list.
let me illustrate on toy version.
brmalg = lambda x: 1 + x + x ** 2 - x ** 3 optimal_score = minimize(brmalg, 0.5, method='slsqp', options={'disp': true}, bounds=[(-1.00, 1.00)]) print(optimal_score.x) adict = {'ticker': ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], 'total_score': [1, 2, 3, 4, 5, 6], 'hold': [false, false, false, false, false, false]} stocks = pd.dataframe(adict) stocks.set_index('ticker', inplace=true) stocks['hold'] = stocks['hold'].where(stocks['total_score'].shift(1) < optimal_score.x, true) this returns pandas valueerror:
optimization terminated successfully. (exit mode 0) traceback (most recent call last): stocks['hold'] = stocks['hold'].where(stocks['total_score'].shift(1) < optimal_score.x, true) file "\pandas\core\ops.py", line 822, in wrapper raise valueerror('lengths must match compare') valueerror: lengths must match compare [-0.33315628] process finished exit code 1 if index float list 'optimal_score.x' issue resolved.
stocks['hold'] = stocks['hold'].where(stocks['total_score'].shift(1) < optimal_score.x[0], true) optimization terminated successfully. (exit mode 0) current function value: [ 0.81481488] iterations: 7 function evaluations: 21 gradient evaluations: 7 [-0.33315628]
Comments
Post a Comment