Is there a similar function to scipy.ndimage's uniform_filter or convolve (similar problem with Numpy Two-Dimensional Moving Average), but the average is calculated using only the values in the input array (disregard count of fill values for corner and edge cells). A similar function wherein the fill value can be set to np.nan and the resulting average is calculated as np.nanmean?
My initial code loops over the array to get the neighbors and calculate the resulting mean, but this method takes too long. I have tried both uniform_filter or convolve, but the results are not what I needed since the resulting corner and edge values are too low (due to filling the edges with 0).
For example, if I have the array:
a = np.ones((4,5))
Calculating the mean value from a moving 3x3 array should also result to:
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
uniform_filter gives:
uniform_filter(a, size=3, mode='constant', cval=0.0)
array([[0.44444444, 0.66666667, 0.66666667, 0.66666667, 0.44444444],
[0.66666667, 1. , 1. , 1. , 0.66666667],
[0.66666667, 1. , 1. , 1. , 0.66666667],
[0.44444444, 0.66666667, 0.66666667, 0.66666667, 0.44444444]])
I have tried setting cval=np.nan but the resulting values of the cell edges are nan.
For another array, b
array([[1., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
I want to obtain an array, using a 3x3 averaging-window, as
array([[0.25 , 0.16666667, 0. ],
[0.16666667, 0.11111111, 0. ],
[0. , 0. , 0. ]])
The values are calculated as shown in this illustration