我是一名经验丰富的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]
我现在想要的是根据
istart
和iend
所表示的间隔来缩小值数组x
。即:# 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
进行分块操作的方法,这仅允许使用常规块。我无法找到一种不需要执行Python循环的解决方案:-(
在我的实际应用中,数组要大得多,并且性能确实很重要,所以我目前使用numba.jit
。
是否有任何我遗漏的numpy函数可以做到这一点?
x
的值是否总是在[0,1)
范围内的浮点数? - Divakarx
通常都是一个更复杂的数组结构。 - Marti Nito