I am new in Python and faced with a problem of calculating element-wise mean value in 2D numpy array. I've searched in the web and didn't find the optimal algorithm for this stuff.
So, for example I have this 2D array:
array = np.arange(20).reshape(4,5)
array
Out[7]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
I want to get another 2D numpy array with the elements equal to element-wise mean values of previous matrix. Moreover, my target is to have the output matrix with the same shape as the input matrix.
For example I've select a 3*3 block of neighnourhood elements and start with the first element of matrix (zero):
Since its a 'corner' element we can not calculate the mean of 3 * 3, so I assume we can calculate mean of 2 * 2: (0+1+5+6):4=3
Then we move our 3 * 3 block to the next element:
And calculate the mean in the same way and so on for the first row of the matrix. Moving row by row, element by element:
And calculate their mean in the same way: (0+1+2+5+6+7+10+11+12):9=6
And do so on.
Could you tell me please how it is possible to create the matrix of the mean elements with the same shape as an input matrix? Shall I operate it with for loop with 2 variables? And how to deal with 'border' elements where we can not apply full 3 * 3 block of neighbour element?
Will be really appreciated for your help!
P.S. On my calculations I assume to have the final result matrix, for this case:



