在这种情况下,通常最好从一个能够再现错误的小玩具问题开始。
此特定错误可能来自this line。虽然神秘,但它试图将函数的输入转换为浮点数组(NPY_DOUBLE),并在失败时抛出错误。
OK,这里是如何引发的错误:
In [3]: import numpy as np
In [4]: from scipy.optimize import curve_fit
In [5]: x = np.array([1.0, 2., 3, 4.])
In [6]: y = x
In [7]: def f(x, a): # try a nan
...: return np.nan
...:
In [9]: curve_fit(f, x, y, 2.0)
/home/br/virtualenvs/scipy-dev/local/lib/python2.7/site-packages/scipy/optimize/minpack.py:604: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)
Out[9]: (array([ 2.]), array([[ inf]]))
不,nan
我国农产品一些不同的输出。让我们尝试一个字符串:
In [10]: def g(x, a):
....: return 'nonsense'
....:
In [11]: curve_fit(g, x, y, 2.0)
<snip>
TypeError: unsupported operand type(s) for -: 'str' and 'numpy.ndarray'
另外不同的错误。好吧,让我们试一下复数:
In [12]: def h(x, a):
....: return 1j
....:
In [13]: curve_fit(h, x, y, 2.0)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'
<snip>
error: Result from function call is not a proper array of floats.
宾果。现在我开始检查你的函数是否在一些输入中进入复杂平面。
我发现[lmfit](http://lmfit.github.io/lmfit-py/)对于拟合输入的某些值的模型非常有用 - 它比scipy模块强大得多。另外,如果您有单独的分子和分母,则还应该使用双参数[arctan2](http://docs.scipy.org/doc/numpy/reference/generated/numpy.arctan2.html)。 – chthonicdaemon