0
import numpy as np
Bees = open("BeesData.txt", "r")
Bees = Bees.read()
Bees = Bees.split( )
for i in range(0, len(Bees)): 
    Bees[i] = int(Bees[i])
Bees = np.reshape(Bees,(25,25))
def suma5x5(x,y):
    suma=0

This is the BeesData file:

https://drive.google.com/file/d/1aWcLZq2MuGENavoTnCfokXr1Nnyygz23/view?usp=sharing![enter image description here](https://i.stack.imgur.com/G9u5P.png)

2 Answers2

1

I took this as kind of a learning exercise for myself. Not a numpy user really.

TLDR:

search_area = np.arange(25).reshape(5,5)
search_kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])

results = convolve2d(search_area, search_kernel, mode='same')

max_index = np.argmax(results, axis=None)
max_location = np.unravel_index(max_index, results.shape)
print(max_location)

Assuming that 5 adjacent means: up, down, left, right, center, then you can find the value using a convolution.

Assume that we want to find the sum of every 3x3 block, only for the values marked as 1's:

[[0, 1, 0],
 [1, 1, 1],
 [0, 1, 0]]

This shape can be used as your kernel for a convolution. It will be used to sum up every value in a 3x3 square by multiplying their corresponding values by 1. e.g for

[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]]

You would get 1x0 + 2x1 + 3x0 + 4x1 + 5x1 + 6x1 + 7x0 + 8x1 + 9x0 = 25

scipy has a method for this called convolve2d.

import numpy as np
from scipy.signal import convolve2d

search_area = np.arange(36).reshape(6,6)
search_kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])

results = convolve2d(search_area, search_kernel)

print(search_area)
print(results)

This outputs:

[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]]
[[  0   0   1   2   3   4   5   0]
 [  0   7  10  14  18  22  20   5]
 [  6  25  35  40  45  50  43  11]
 [ 12  49  65  70  75  80  67  17]
 [ 18  73  95 100 105 110  91  23]
 [ 24  97 125 130 135 140 115  29]
 [ 30  85 118 122 126 130  98  35]
 [  0  30  31  32  33  34  35   0]]

Because we included the edges as part of the convolution, you'll see that the result size is now 8x8 instead of the original 6x6. For values it couldn't find because they go off the edge of the array, the method assumed a value of zero.

To discard the edges you can use the same mode, which make it drop these edges from the results:

results = convolve2d(search_area, search_kernel, mode='same')

print(search_area)
print(results)
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]
 [24 25 26 27 28 29]
 [30 31 32 33 34 35]]
[[  7  10  14  18  22  20]
 [ 25  35  40  45  50  43]
 [ 49  65  70  75  80  67]
 [ 73  95 100 105 110  91]
 [ 97 125 130 135 140 115]
 [ 85 118 122 126 130  98]]

Now to find the location with the most bees, you can use argmax to get the index of the largest value, and unravel_index to get this as a location in the original shape.

max_index = np.argmax(results, axis=None)
max_location = np.unravel_index(max_index, results.shape)

print(max_location)
(4, 4)
flakes
  • 21,558
  • 8
  • 41
  • 88
0

Assuming window size of 5x5 from your last 2 lines of code:

def suma5x5(x,y):
    suma=0

tried a simple basic python + numpy operation code.

arr = np.array([[2, 3, 7, 4, 6, 2, 9],
                [6, 6, 9, 8, 7, 4, 3],
                [3, 4, 8, 3, 8, 9, 7],
                [7, 8, 3, 6, 6, 3, 4],
                [4, 2, 1, 8, 3, 4, 6],
                [3, 2, 4, 1, 9, 8, 3],
                [0, 1, 3, 9, 2, 1, 4]])
w_size = 5
res = max(sum(sum(a)) for a in (arr[row:row+w_size, col:col+w_size] for row in range(arr.shape[0]-w_size+1) for col in range(arr.shape[1]-w_size+1)))

print(res)  # 138
Joonyoung Park
  • 474
  • 3
  • 6
  • I was thinking like making a matrix 5x5 inside of the 25x25 with the starting point at [0][0], sum all the items and then make a loop to move the 5x5 matrix until the right end of the matrix reach 25 then move the matrix down and keep it going like that – LUIS A HERNANDEZ-PADILLA Oct 21 '20 at 14:59
  • That's what I did; some comments on the code above: `arr[row:row+w_size, col:col+w_size]` gives 5x5 2d array since `w_size` is 5. `row` and `col` loops are for moving window. `sum(sum(a))` would sum up 25 numbers in 5x5 2d array. `max` is for picking out the greatest number. – Joonyoung Park Oct 22 '20 at 04:01