当拟合函数只有一个参数时,scipy.optimize.curve_fit不起作用

时间:2013-08-14 14:16:58

标签: scipy curve-fitting

我正在尝试将正弦+余弦曲线拟合到我的数据中,幅度仅作为参数,看起来像curve_fit不能用于只有一个参数的函数...

import numpy as np
import scipy.optimize as scio

def function(x,a):
    y = a*np.sin(np.radians(x))
    return y

x = np.array(range(360))
y = function(x,3)
yn = y +0.2 * np.random.normal(size=len(x))
popt,pcov = scio.curve_fit(function,x,yn)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/mj/Documents/NACO/VLT/DataReduction/<ipython-input-62-388a2d20a0c8> in <module>()
----> 1 popt,pcov = scio.curve_fit(function,x,yn)

 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, **kw)
     416 
     417    if (len(ydata) > len(p0)) and pcov is not None:
 --> 418        s_sq = (func(popt, *args)**2).sum()/(len(ydata)-len(p0))
    419        pcov = pcov * s_sq
    420    else:

/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.pyc in _general_function(params, xdata, ydata, function)
    319 
    320 def _general_function(params, xdata, ydata, function):
--> 321     return function(xdata, *params) - ydata
    322 
    323 def _weighted_general_function(params, xdata, ydata, function, weights):

TypeError: function() argument after * must be a sequence, not numpy.float64

为什么这对curve_fit来说是个问题? 我做错了吗?

提前致谢! MJO

0 个答案:

没有答案