【发布时间】:2017-04-05 01:33:37
【问题描述】:
我认为自己是一位经验丰富的 numpy 用户,但我无法找到以下问题的解决方案。假设有以下数组:
# sorted array of times
t = numpy.cumsum(numpy.random.random(size = 100))
# some values associated with the times
x = numpy.random.random(size=100)
# some indices into the time/data array
indices = numpy.cumsum(numpy.random.randint(low = 1, high=10,size = 20))
indices = indices[indices <90] # respect size of 100
if len(indices) % 2: # make number of indices even
indices = indices[:-1]
# select some starting and end indices
istart = indices[0::2]
iend = indices[1::2]
我现在想要的是减少值数组x,给定由istart 和iend 表示的间隔。即
# e.g. use max reduce, I'll probably also need mean and stdv
what_i_want = numpy.array([numpy.max(x[is:ie]) for is,ie in zip(istart,iend)])
我已经用谷歌搜索了很多,但我只能找到通过 stride_tricks 进行的分块操作,它只允许常规块。如果不执行 pyhthon 循环,我无法找到解决方案:-(
在我的实际应用程序中,数组更大,性能也很重要,所以我暂时使用numba.jit。
我是否缺少任何能够做到这一点的 numpy 函数?
【问题讨论】:
-
x在[0,1)中是否总是有浮动点数? -
没有。
x通常是一个复杂得多的数组结构。
标签: python arrays numpy indexing reduce