scipy.optimize.leastsq:不是一个正确的浮点数组

时间:2013-11-16 13:32:36

标签: python optimization scipy mathematical-optimization

我试图解决一个小玩具问题

import numpy as np
import scipy.optimize as opt

def f(a):
    return np.array([a+.2,a-.1])

opt.leastsq(f,2)

然而,它给了我一个错误

\site-packages\scipy\optimize\minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    362             maxfev = 200*(n + 1)
    363         retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
--> 364                 gtol, maxfev, epsfcn, factor, diag)
    365     else:
    366         if col_deriv:

error: Result from function call is not a proper array of floats.

但我的功能f确实给了一个ndarray。

1 个答案:

答案 0 :(得分:1)

所以它需要一个类似数组的输入。这有效:

import numpy as np
import scipy.optimize as opt

def f(a):
    return np.array([a[0]+.2,a[0]-.1])

print opt.leastsq(f,[2])