numpy - Finding the lowest neighboring index in a Python array -
I have a problem in which we should write a function, when input of 2D array will be given offset in rows and for each index Return of the lowest price in the column of neighboring index; For the array of offsets in columns and for each array of each array, for an array example, if the lowest neighboring cell of an index is below one line and is in a column on the right, then offset is 1,1; If the smallest neighbor cell is on the left, the offset is 0, -1; If this is the smallest of its neighboring cells, then the offset is 0,0.
Because I could not find the fast and correct way to do this, I wrote a loop that would be repeated through each index and see that from [i, j] to the surrounding indexes for a point Compared to all other indexes. Aall ():
def findLowNhbr ("terrain"): "" "Two 2D-Arrays are made of offset (row and column) with neighboring area, in which The minimum eleven "rowOffset = np.zeros_like (terrain) colOffset = np.zeros_like (terrain) is in the range (LAN (terrain)): if I == 0: line offset [i] = 0 Call offset [i] = 0 alif I == (lane (terrain) -1): line offset [i] = 0 calloffet [i] = 0 and: j (LAN (terrain [i]) in range: If j = = 0 or J = = lane (terrain [i]) - 1: line offset [:, j] = 0 call offset [: , J] = 0 alif (area [i-1: i + 2, j-1: j + 2] & gt; = area [i-1, j-1]). All (): line offset [i, j] = -1 ColOffset [i, j] = -1 elif (area [i-1: i + 2, j-1: j + 2] & gt; = area [i , J-1]). All (): line offset [I, j] = 0 colOffset [i, j] = -1 elif (area [i-1: i + 2, j-1: j + 2] & gt; = area [i + 1, j-1]). All (): line offset [i, j] = 1 calloffet [i, j] = -1 elif (area [i-1: i + 2, j-1: j + 2] & gt; = i- 1, J]). All (): line offset [i, j] = -1 coloffset [i, j] = 0 elif (area [i-1: i + 2, j-1: j + 2] & gt; = area [i + 1, j]). All (): line offset [i, j] = 1 calloffet [i, j] = 0 alif (area [i-1: i + 2, j-1: j + 2] & gt; = terrain [i -1, j + 1]). All (): line offset [i, j] = -1 calloffet [i, j] = 1 alif (area [i-1: i + 2, j-1: j + 2] & gt; = area [i, J]). All (): line offset [i, j] = 0 colOffset [i, j] = 1 elif (area [i-1: i + 2, j-1: j + 2] & gt; = area [i + 1 , J + 1]). All (): line offset [i, j] = 1 colo offset [i, j] = 1 and: line offset [i, j] = 0 ColOffset [i, j] = 0 return line offset, call offset < / Pre> It takes a long time to run, but it runs. I can not imagine that I am actually making this the most effective way possible; Any input?
This should be done more or less in a vectorized manner, ignoring some issues on the boundaries for that moment Which you can trim the input array with repeated values around the edges and import the tray
np np.random.seed (0) terrain = np.random . Rand (10,10) offset = [(i, j) range for the range (-1,2) g (-1,2)] stacked = np.dstack (np.roll (np.roll (terrain) ), I, axis = 0), j, axis = 1) for i, in offsets j) offset_index = np.argmin (stacked, axis = 2) output = np.array (offsets) [offset_index] Explanation - Stack all offset in an NxMx9 array
- Find the indicator of the minimum element on it Last axis
axis = 2 - In this array of offset vectors, this indicator is the result of the index offset in the last line. By yoga
The cleaner way of getting one more possibly all the initial offset is:
Comments
Post a Comment