简体   繁体   English

如何修复此错误 IndexError:数组的索引太多

[英]How do I fix this error IndexError: too many indices for array

I am trying to fit a piece wise function to a time series that I have, where the first part is an exponential decay and the second part is linear, I have looked up many examples, but I keep getting that error.我正在尝试将分段函数拟合到我拥有的时间序列中,其中第一部分是指数衰减,第二部分是线性的,我查了很多例子,但我一直收到那个错误。 This is a python program.这是一个python程序。

Here is my code这是我的代码

def piece_wise(t, t0, a, b, c, d):

return np.piecewise(t, [t <= t0, t > t0], [lambda t:a*np.exp(b*-t) + d, lambda t:c*t + d])

t = filtered["Time (s)"]

y = filtered["Average"]

p, e = optimize.curve_fit(piece_wise, t, y)

The filtered data frame has 2187 rows过滤后的数据框有 2187 行

This is the full error这是完整的错误

  File "/opt/anaconda3/lib/python3.8/site-packages/numpy/lib/function_base.py", line 626, in piecewise
    vals = x[condlist[k]]

IndexError: too many indices for array

Now looking at the program function_base.py, k is a loop variable looped over the number of elements in the condition list passed to np.piecewise.现在看一下程序 function_base.py,k 是一个循环变量,它在传递给 np.piecewise 的条件列表中的元素数量上循环。 What am I doing wrong here?我在这里做错了什么?

Thanks!谢谢!

First of all, before doing something complicated like passing your function to scipy.optimize, you want to check that it works.首先,在做一些复杂的事情之前,比如将你的函数传递给 scipy.optimize,你想检查它是否有效。

You can simply call it with some arbitrary data and parameters:您可以简单地使用一些任意数据和参数调用它:

t = filtered["Time (s)"]
t0 = 5; a = 0; b = 0; c = 0; d = 0;
piece_wise(t, t0, a, b, c, d)

This produces the traceback:这会产生回溯:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-24-e4aa1198c545> in <module>
----> 1 piece_wise(t, t0, a, b, c, d)

<ipython-input-16-d251be3d390b> in piece_wise(t, t0, a, b, c, d)
      1 def piece_wise(t, t0, a, b, c, d):
----> 2     return np.piecewise(t, [t <= t0, t > t0], [lambda t:a*np.exp(b*-t) + d, lambda t:c*t + d])
      3 

<__array_function__ internals> in piecewise(*args, **kwargs)

~/anaconda3/envs/torch/lib/python3.9/site-packages/numpy/lib/function_base.py in piecewise(x, condlist, funclist, *args, **kw)
    612             y[cond] = func
    613         else:
--> 614             vals = x[cond]
    615             if vals.size > 0:
    616                 y[cond] = func(vals, *args, **kw)

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

This confirms that the problem is nothing to do with scipy.optimize.这证实了问题与 scipy.optimize 无关。

Also, it tells us that the error occurred on this line:此外,它告诉我们错误发生在这一行:

return np.piecewise(t, [t <= t0, t > t0], [lambda t:a*np.exp(b*-t) + d, lambda t:c*t + d])

Let's break it down...让我们分解它...

f1, f2 = lambda t:a*np.exp(b*-t) + d, lambda t:c*t + d
f1(0)
f2(0)

No errors, so these two functions are ok...没有错误,所以这两个功能都可以...

Let's look at the documentation for np.piecewise我们看一下 np.piecewise 的文档

piecewise(x, condlist, funclist, *args, **kw)
    Evaluate a piecewise-defined function.
    
    Given a set of conditions and corresponding functions, evaluate each
    function on the input data wherever its condition is true.
    
    Parameters
    ----------
    x : ndarray or scalar
        The input domain.
    condlist : list of bool arrays or bool scalars
        ....
    funclist : list of callables, f(x,*args,**kw), or scalars
        ....

Ah look!啊看! It says x should be an ndarray or scalar.它说 x 应该是一个 ndarray 或标量。

We are passing a Pandas Series.我们正在传递 Pandas 系列。

Try this:尝试这个:

t = filtered["Time (s)"].to_numpy()
t0 = 5; a = 0; b = 0; c = 0; d = 0;
piece_wise(t, t0, a, b, c, d)

Out: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Now it seems to work.现在它似乎起作用了。 I think this might be your problem.我想这可能是你的问题。

Hopefully I have helped you with a means to debug such problems in future.希望我在将来帮助您调试此类问题。 Always breakdown problems into smaller pieces and test each piece separately.始终将问题分解为更小的部分并分别测试每个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM