Q:

Finding highest values "zone" in a 2d matrix in Python

I have a 2d matrix in Python like this (a 10 rows/20 columns list I use to later do an imshow):
[[-20.17 -12.88 -20.7  -25.69 -21.69 -34.22 -32.65 -31.74 -36.36 -37.65
  -41.42 -41.14 -44.01 -43.19 -41.85 -39.25 -40.15 -41.31 -39.73 -28.66]
 [ 14.18  53.86  70.03  64.39  72.37  39.95  30.44  28.14  20.77  17.98
   25.74  25.66  27.56  37.61  42.39  42.39  35.79  41.65  41.65  41.84]
 [ 33.71  68.35  69.39  66.7   59.99  40.08  40.08  40.8   26.19  19.82
   19.82  18.07  20.32  19.51  24.77  22.81  21.45  21.45  21.45  23.7 ]
 [103.72  55.11  32.3   29.47  16.53  15.54   9.4    8.11   5.06   5.06
   13.07  13.07  12.99  13.47  13.47  13.47  12.92  12.92  14.27  20.63]
 [ 59.02  18.6   37.53  24.5   13.01  34.35   8.16  13.66  12.57   8.11
    8.11   8.11   8.11   8.11   8.11   5.66   5.66   5.66   5.66   7.41]
 [ 52.69  14.17   7.25  -5.79   3.19  -1.75  -2.43  -3.98  -4.92  -6.68
   -6.68  -6.98  -6.98  -8.89  -8.89  -9.15  -9.15  -9.15  -9.15  -9.15]
 [ 29.24  10.78   0.6   -3.15 -12.55   3.04  -1.68  -1.68  -1.41  -6.15
   -6.15  -6.15 -10.59 -10.59 -10.59 -10.59 -10.59  -9.62 -10.29 -10.29]
 [  6.6    0.11   2.42   0.21  -5.68 -10.84 -10.84 -13.6  -16.12 -14.41
  -15.28 -15.28 -15.28 -18.3   -5.55 -13.16 -13.16 -13.16 -13.16 -14.15]
 [  3.67 -11.69  -6.99 -16.75 -19.31 -20.28 -21.5  -21.5  -34.02 -37.16
  -25.51 -25.51 -26.36 -26.36 -26.36 -26.36 -29.38 -29.38 -29.59 -29.38]
 [ 31.36  -2.87   0.34  -8.06 -12.14 -22.7  -24.39 -25.51 -26.36 -27.37
  -29.38 -31.54 -31.54 -31.54 -32.41 -33.26 -33.26 -15.54 -15.54 -15.54]]

I'm trying to find a way to detect the "zone" of this matrix that contains the highest density of high values in it. It means it might not contain the highest single value of the whole list, obviously.
I suppose to do so I should define how big this zone is, so let's say it should be 2x2 (so I want to find what is the 'square' of 2x2 items containing the highest values).
I always think I have a logical solution to do so, but then I always fail following the logic of how it could work!
Anyone has a suggestion I could start from?

A:

I know there might be some easier ways to do so, but this is the easiest for me. I've created the following function to perform this task which takes two arguments:

arr: a 2D numpy array.
zone_size: the size of the square zone.

And the function goes like so:
def get_heighest_zone(arr, zone_size):
    max_sum = float("-inf")
    row_idx, col_idx = 0, 0
    for row in range(arr.shape[0]-zone_size):
        for col in range(arr.shape[1]-zone_size):
            curr_sum =  np.sum(arr[row:row+zone_size, col:col+zone_size])
            if curr_sum > max_sum:
                row_idx, col_idx = row, col
                max_sum = curr_sum
    return arr[row_idx:row_idx+zone_size, col_idx:col_idx+zone_size]

Assuming arr is the numpy array posted in your question, applying this function over different zone_sizes will return these values:
>>> get_heighest_zone(arr, 2)
[[70.03 64.39]
 [69.39 66.7 ]]

>>> get_heighest_zone(arr, 3)
[[53.86 70.03 64.39]
 [68.35 69.39 66.7 ]
 [55.11 32.3  29.47]]

>>> get_heighest_zone(arr, 4)
[[ 14.18  53.86  70.03  64.39]
 [ 33.71  68.35  69.39  66.7 ]
 [103.72  55.11  32.3   29.47]
 [ 59.02  18.6   37.53  24.5 ]]

If the zone_size doesn't have to be square, then you will need to modify a little bit in the code. Also, you should assert that zone_size is less than the array size.
Hopefully, this is what you was looking for!

