![](/img/trans.png)
[英]adding values in a 2d array provided that the first value is greater than 5
[英]Index of smallest neighbouring cell greater than a value in 2D array
我有一個numpy數組
a = np.array([[0,4,3,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,3,5,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,8,3,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,2,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,2,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,4,3,4,4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,4,3,4,4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]])
我正在尋找value= 2
( numpy.where(a==value)
)但大於value
的單元格最小鄰居的索引。 我還需要找到最小鄰居的相應單元格的索引= value
。
在這種情況下,結果( value = 2
)應為:
相鄰單元的索引: [0,2]
和[4,3]
相應單元格的索引: [1,2]
和[3,3]
如果問題不太清楚,請您道歉。
這是我到目前為止的內容:
import numpy as np
value = 2
neighbors = np.zeros(4, dtype = np.float)
fdx = np.flatnonzero(a== value)
locations = fdx // a.shape[1], fdx % a.shape[1]
maximums = []
for item in zip(*locations):
i, j = item[0], item[1]
neighbors[0], neighbors[1], neighbors[2],neighbors[3] = [a[i-1,j], a[i+1,j], a[i,j-1], a[i,j+1]]
maximums.append(min(neighbors[neighbors> value]))
print np.where(a==min(maximums))
prints: (array([0, 4]), array([2, 3]))
這是非常慢的,並且仍然不知道如何找到相應單元格的索引。 任何與我的解決方案完全不同的解決方案也將被接受。
您可以使用scipy.ndimage.morphology.binary_dilation
找到鄰居
import numpy as np
from scipy.ndimage import morphology
a = np.array([[0,4,3,9,9,9],
[4,4,2,2,2,9],
[4,2,2,9,2,6],
[4,2,2,2,6,8],
[4,4,4,3,4,4]])
k = 2
# make mask
eq = a==k
# find greater neighbors (as mask)
nbs = morphology.binary_dilation(eq) & (a>k)
# translate to index
minidx = np.argwhere(nbs & (a == np.min(a[nbs])))
# now find neighbors' neighbors
# pad original mask
m,n = a.shape
eqp = np.zeros((m+2, n+2), bool)
eqp[1:-1,1:-1] = eq
# generate offset vectors for the four major directions (up, down, left, right)
# corrected for padding
offsp = np.array([(0,1),(2,1),(1,0),(1,2)])
# without padding correction
offs = offsp - 1#
# for each minimum, find all (1-4) reference neighbors
refidx = [i + offs[eqp[tuple((i+offsp).T)]] for i in minidx]
print(minidx)
print(refidx)
# [[0 2]
# [4 3]]
# [array([[1, 2]]), array([[3, 3]])]
不知道這有多快(如果有的話),但是它會找到相應的單元。
import numpy as np
a = np.array([[0,4,3,9,9,9],
[4,4,2,2,2,9],
[4,2,2,9,2,6],
[4,2,2,2,6,8],
[4,4,4,3,4,4]])
value = 2
idx = np.where(a == value)
idx = zip(idx[0],idx[1])
running_min = np.inf
corresponding_idx = []#corresponding cells
nb_min_list = []#location of neighbors
nb_min_idx = []
for i in idx:
nb_idx = [(i[0]+1,i[1]),(i[0]-1,i[1]),(i[0],i[1]+1),(i[0],i[1]-1)]#note no check for out of bounds.
nb_idx = [nb for nb in nb_idx if nb[0] >= 0 and nb[0] < a.shape[0] and nb[1] >= 0 and nb[1] < a.shape[1]]#test for edges
try:
nb_min = min([a[nb] for nb in nb_idx if a[nb] > value])
corresponding_idx.append(i)
nb_min_list.append(nb_min)
nb_min_idx.append([nb for nb in nb_idx if a[nb] == nb_min])
except:
pass
nb_min_loc = np.where(nb_min_list == min(nb_min_list))[0]
corresponding_cells = []
min_nbs = []
for nb in nb_min_loc:
corresponding_cells.append(corresponding_idx[nb])
min_nbs.append(nb_min_idx[nb])
print(corresponding_cells)#[(1, 2), (3, 3)]
print(min_nbs)#[[(0, 2)], [(4, 3)]]
使用上一個答案中的 window_nd
def min_search(a, val = 2):
a_view = window_nd(np.pad(a, ((1, 1),(1, 1)), 'constant',
constant_values = np.inf), 3)
min_val = np.where(a_view[a == val] <= val, np.inf, a_view[a == val]).min()
neig_mask = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype = bool)
rev_mask = np.logical_and(np.any(np.logical_and(a_view == min_val,
neig_mask), axis = (-1, -2)), a == val)
min_mask = np.logical_and(np.any(np.logical_and(a_view == val,
neig_mask), axis = (-1, -2)), a == min_val)
return np.nonzero(min_mask), np.nonzero(rev_mask)
這是做什么的:
a
帶襯墊val
(因此加窗的形狀是(*a.shape, 3, 3)
val
為中心的窗口中找到大於val
的最小值(填充允許邊緣的插入),並將其分配給min_val
neig_mask
將neigbors限制在基本方向上 neig_mask
位置具有min_val
且在中央具有val
窗口,分配給rev_mask
val
在neig_mask
位置和min_val
在中央,分配給min_mask
返回np.nonzero
的min_mask
和rev_mask
,它們是可被用作一個索引元組a
min_search(a) Out: ((array([0, 4], dtype=int32), array([2, 3], dtype=int32)), (array([1, 3], dtype=int32), array([2, 3], dtype=int32)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.