嘗試使用Scipy最小化來獲取Std_Diff目標函數最小的d值(整數)。使用Scipy最小化函數
我的代碼:
def Std_Diff(d):
return std(diff(df['BN'].values,d));
from scipy.optimize import minimize
b=(3,)
res = minimize(Std_Diff,(1,), method='SLSQP', bounds = b)
The **df['BN'].values** are
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: too many indices for array "
如果我不使用界:RES =最小化(Std_Diff,(1),方法= 'SLSQP'),我得到另一個錯誤:
> 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 a scalar")
> 371 # Compute the constraints
> 372 if cons['eq']: ValueError: Objective function must return a scalar.
非常感謝您的建議。
謝謝。有效。 –