scipy.optimize.curve_fit中的IndexError

问题描述 投票:0回答:1

我正在尝试在scipy中使用curve_fit函数对某些数据进行建模。我有5组数据将与不同的权重相加,以适合观察到的数据y。

def model(kidx, c10,c20,c40,c80,c160):
    mod = (c10*data['10'][kidx] + 
           c20*data['20'][kidx] + 
           c40*data['40'][kidx] + 
           c80*data['80'][kidx] + 
           c160*data['160'][kidx])
    return mod

curve_fit(model, k, y)

data是包含numpy数组的字典,k是索引列表,y是目标数据。当我运行此代码时,它将返回IndexError:

IndexError                                Traceback (most recent call last)
<ipython-input-18-51aba3bc37c2> in <module>
----> 1 curve_fit(model, k, y)

~/.conda/envs/heinrich/lib/python3.7/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
    750         # Remove full_output from kwargs, otherwise we're passing it in twice.
    751         return_full = kwargs.pop('full_output', False)
--> 752         res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
    753         popt, pcov, infodict, errmsg, ier = res
    754         cost = np.sum(infodict['fvec'] ** 2)

~/.conda/envs/heinrich/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    381     if not isinstance(args, tuple):
    382         args = (args,)
--> 383     shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
    384     m = shape[0]
    385 

~/.conda/envs/heinrich/lib/python3.7/site-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
     24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
     25                 output_shape=None):
---> 26     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
     27     if (output_shape is not None) and (shape(res) != output_shape):
     28         if (output_shape[0] != 1):

~/.conda/envs/heinrich/lib/python3.7/site-packages/scipy/optimize/minpack.py in func_wrapped(params)
    456     if transform is None:
    457         def func_wrapped(params):
--> 458             return func(xdata, *params) - ydata
    459     elif transform.ndim == 1:
    460         def func_wrapped(params):

<ipython-input-17-8bc44c07046b> in model(kidx, c10, c20, c40, c80, c160)
      1 def model(kidx, c10,c20,c40,c80,c160):
----> 2     mod = c10*data['10'][kidx] + c20*data['20'][kidx] + c40*data['40'][kidx] + c80*data['80'][kidx] + c160*data['160'][kidx]
      3     return mod

IndexError: arrays used as indices must be of integer (or boolean) type

这里发生了什么? k是一个整数数组,它是唯一可用作索引的整数。这些变量均未在笔记本中使用。

python numpy scipy linear-regression
1个回答
0
投票

您可能是[[正在调用 curve_fit,其中k是整数数组,但是该方法将其转换为浮点类型here

当您考虑时,这很有意义。 curve_fit方法通常用于从假定为连续的变量(预测变量)预测y数据。通过假定优化器可以对输入x数组进行细微调整并确定将改善拟合的方向,通常这就是优化器取得进展的方式。如果输入数据是索引数组,而不是搜索空间中的实际位置,则没有意义。

您应该重写model函数以将预测变量接受为浮点数数组。除了编写一个知道如何“搜索”索引的自定义优化器外,实际上没有其他方法可以解决此问题。这是很多工作,可能不是您想要的。因此,应首选前一种方法。

编辑:

实际上,看起来kidx在整个优化过程中可能是恒定的。也就是说,您始终选择data的相同元素进行预测。如果是这种情况,只需在开始时对其进行一次索引,然后将其传递给模型函数,但是将[[not

作为curve_fit例程的参数。
© www.soinside.com 2019 - 2024. All rights reserved.