【问题标题】:IndexError in scipy.optimize.curve_fitscipy.optimize.curve_fit 中的 IndexError
【发布时间】:2020-09-04 15:58:44
【问题描述】:

我正在尝试使用 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 是一个整数数组,它是唯一可以用作索引的数组。这些变量以前都没有在笔记本中使用过。

【问题讨论】:

  • model 函数中添加print(kidx) 行,这样您就可以清楚地知道fit 传递给该函数的值是什么。当使用像这样的scipy 函数时,请密切注意文档对您的函数的评价,以及它应该接受的参数。不要猜测。测试。

标签: python numpy scipy linear-regression


【解决方案1】:

您可能是调用 em> curve_fitk作为整数数组,但该方法将其转换为浮点类型here

这是有道理的,当你想到它时。 curve_fit方法通常用于预测来自变量(预测器)的987654325 @数据,假设是连续的。这通常是优化器如何通过假设对输入x阵列进行小调整并确定将改善拟合的方向进行小调整。如果输入数据是索引阵列,则这不是有意义的,而不是搜索空间中的实际位置。

您应该重写您的model函数以接受预测器作为浮点数。围绕此别无别的办法,短暂编写了一个自定义优化器,知道如何通过索引“搜索”。这是很多工作,可能不是你想要的。因此,前一种方法应该是优选的。

编辑:

实际上,它看起来像kidx可能是恒定的整个优化过程中。也就是说,您始终选择data的相同元素以使您的预测。如果是这种情况,只需在开头索引一次并将其传递给模型函数,但是 not em>作为curve_fit例程的参数。

【讨论】:

  • 我们正在努力解决的问题涉及将观察到的y比较为1d阵列,这是模拟数据,这是1d阵列的字典。没有函数模拟观察到的数据,因此我没有看到如何重写模型函数以接受浮点数。是否有另一个模块可能在这种情况下使用更有意义? span>
  • 当然存在一个函数,模拟观察到的数据。您正在使用它:data中的数组的加权和。我的编辑是什么意思,问问自己在优化期间的kidx更改。如果答案是“否”,那么你不需要将它传递给model,然后你可以完全避免这个问题。如果答案是“是”,那么您需要查看除curve_fit之外的另一种类型的优化过程。动态编程可能是一个合适的,但它真的取决于您的问题的细节。 span>
猜你喜欢
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
相关资源
最近更新 更多