What is a pythonic way of finding maximum values and their indices for moving subarrays for numpy ndarray?

JustDoIt

I have numpy ndarrays which could be 3 or 4 dimensional. I'd like to find maximum values and their indices in a moving subarray window with specified strides.

For example, suppose I have a 4x4 2d array and my moving subarray window is 2x2 with stride 2 for simplicity:

[[ 1, 2, 3, 4], 
 [ 5, 6, 7, 8], 
 [ 9,10,11,12], 
 [13,14,15,16]].

I'd like to find

[[ 6  8],
 [14 16]]

for max values and

[(1,1), (3,1),
 (3,1), (3,3)]

for indices as output.

Is there a concise, efficient implementation for this for ndarray without using loops?

jme

Here's a solution using stride_tricks:

def make_panes(arr, window):
    arr = np.asarray(arr)
    r,c = arr.shape
    s_r, s_c = arr.strides
    w_r, w_c = window

    if c % w_c != 0 or r % w_r != 0:
        raise ValueError("Window doesn't fit array.")

    shape = (r / w_r, c / w_c, w_r, w_c)
    strides = (w_r*s_r, w_c*s_c, s_r, s_c)

    return np.lib.stride_tricks.as_strided(arr, shape, strides)

def max_in_panes(arr, window):
    w_r, w_c = window
    r, c = arr.shape
    panes = make_panes(arr, window)
    v = panes.reshape((-1, w_r * w_c))

    ix = np.argmax(v, axis=1)
    max_vals = v[np.arange(r/w_r * c/w_c), ix]

    i = np.repeat(np.arange(0,r,w_r), c/w_c)
    j = np.tile(np.arange(0, c, w_c), r/w_r)
    rel_i, rel_j = np.unravel_index(ix, window)
    max_ix = i + rel_i, j + rel_j

    return max_vals, max_ix

A demo:

>>> vals, ix = max_in_panes(x, (2,2))
>>> print vals
[[ 6  8]
 [14 16]]
>>> print ix
(array([1, 1, 3, 3]), array([1, 3, 1, 3]))

Note that this is pretty untested, and is designed to work with 2d arrays. I'll leave the generalization to n-d arrays to the reader...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding a set of indices that maps the rows of one NumPy ndarray to another

From Dev

Looping through a vector and finding a maximum sum of values with fixed number of indices

From Dev

A pythonic way of finding folder

From Dev

Ruby - Finding the Maximum Difference among subarrays

From Java

How do I get indices of N maximum values in a NumPy array?

From Dev

NumPy: How to retrieve the indices of the maximum values in a multidimensional array

From Dev

How to get indices of maximum values in numpy but with threshold value?

From Dev

Finding the maximum of the values in a file

From Dev

What is the Pythonic way of doing this?

From Dev

What is the fastest way to map group names of numpy array to indices?

From Dev

What's the Pythonic way to "repeat this until condition met -or- maximum achieved"?

From Dev

Get the indices of N highest values in an ndarray

From Dev

Find cumsum of subarrays split by indices for numpy array efficiently

From Dev

Python 2.7, finding length of numpy.ndarray

From Dev

Finding indexes of maximum values of an array

From Dev

Get indices of N maximum values in a numpy array, with random tie-breaking

From Dev

Get indices of N maximum values in a numpy array, with random tie-breaking

From Dev

Pythonic way to find maximum absolute value of list

From Dev

Finding indices given condition in numpy matrix

From Dev

hdf to ndarray in numpy - fast way

From Dev

What is the pythonic way to calculate argmax?

From Java

What is a Pythonic way for Dependency Injection?

From Dev

What is the pythonic way to calculate argmax?

From Dev

What is the pythonic way of doing nothing?

From Dev

Count of values in numpy.ndarray

From Dev

Finding the indices of the values of one list in another list

From Dev

Most efficient (and pythonic) way to count False values in 2D numpy arrays?

From Dev

A Pure Pythonic Pairwise Euclidean distance of rows of a numpy ndarray

From Dev

A Pure Pythonic Pairwise Euclidean distance of rows of a numpy ndarray

Related Related

  1. 1

    Finding a set of indices that maps the rows of one NumPy ndarray to another

  2. 2

    Looping through a vector and finding a maximum sum of values with fixed number of indices

  3. 3

    A pythonic way of finding folder

  4. 4

    Ruby - Finding the Maximum Difference among subarrays

  5. 5

    How do I get indices of N maximum values in a NumPy array?

  6. 6

    NumPy: How to retrieve the indices of the maximum values in a multidimensional array

  7. 7

    How to get indices of maximum values in numpy but with threshold value?

  8. 8

    Finding the maximum of the values in a file

  9. 9

    What is the Pythonic way of doing this?

  10. 10

    What is the fastest way to map group names of numpy array to indices?

  11. 11

    What's the Pythonic way to "repeat this until condition met -or- maximum achieved"?

  12. 12

    Get the indices of N highest values in an ndarray

  13. 13

    Find cumsum of subarrays split by indices for numpy array efficiently

  14. 14

    Python 2.7, finding length of numpy.ndarray

  15. 15

    Finding indexes of maximum values of an array

  16. 16

    Get indices of N maximum values in a numpy array, with random tie-breaking

  17. 17

    Get indices of N maximum values in a numpy array, with random tie-breaking

  18. 18

    Pythonic way to find maximum absolute value of list

  19. 19

    Finding indices given condition in numpy matrix

  20. 20

    hdf to ndarray in numpy - fast way

  21. 21

    What is the pythonic way to calculate argmax?

  22. 22

    What is a Pythonic way for Dependency Injection?

  23. 23

    What is the pythonic way to calculate argmax?

  24. 24

    What is the pythonic way of doing nothing?

  25. 25

    Count of values in numpy.ndarray

  26. 26

    Finding the indices of the values of one list in another list

  27. 27

    Most efficient (and pythonic) way to count False values in 2D numpy arrays?

  28. 28

    A Pure Pythonic Pairwise Euclidean distance of rows of a numpy ndarray

  29. 29

    A Pure Pythonic Pairwise Euclidean distance of rows of a numpy ndarray

HotTag

Archive