我有一个二维的numpy数组,可以分成64个盒子(类似于棋盘)。 目标是编写一个函数,返回每个盒子中最大值的位置和值。例如:
FindRefs(array) --> [(argmaxX00, argmaxY00, Max00), ...,(argmaxX63, argmaxY63, Max63)]
其中argmaxXnn和argmaxYnn是整个数组的索引(而不是盒子的索引),Maxnn是每个盒子中的最大值。换句话说,
Maxnn = array[argmaxYnn,argmaxYnn]
我尝试了显而易见的“嵌套for”解决方案:
def FindRefs(array):
Height, Width = array.shape
plumx = []
plumy = []
lum = []
w = int(Width/8)
h = int(Height/8)
for n in range(0,8): # recorrer boxes
x0 = n*w
x1 = (n+1)*w
for m in range(0,8):
y0 = m*h
y1 = (m+1)*h
subflatind = a[y0:y1,x0:x1].argmax() # flatten index of box
y, x = np.unravel_index(subflatind, (h, w))
X = x0 + x
Y = y0 + y
lum.append(a[Y,X])
plumx.append(X)
plumy.append(Y)
refs = []
for pt in range(0,len(plumx)):
ptx = plumx[pt]
pty = plumy[pt]
refs.append((ptx,pty,lum[pt]))
return refs
这个版本虽然能够工作,但是既不优雅也不高效。 因此,我尝试了更符合Python风格的版本:
def FindRefs(a):
box = [(n*w,m*h) for n in range(0,8) for m in range(0,8)]
flatinds = [a[b[1]:h+b[1],b[0]:w+b[0]].argmax() for b in box]
unravels = np.unravel_index(flatinds, (h, w))
ur = [(unravels[1][n],unravels[0][n]) for n in range(0,len(box))]
absinds = [map(sum,zip(box[n],ur[n])) for n in range(0,len(box))]
refs = [(absinds[n][0],absinds[n][1],a[absinds[n][1],absinds[n][0]]) for n in range(0,len(box))]
return refs
它可以正常运行,但令我惊讶的是,它并没有比以前的版本更有效率!
问题是:有没有更聪明的方法来完成这个任务?
请注意,效率很重要,因为我有许多大型数组需要处理。
欢迎任何线索。 :)