I need to calculate a most frequent element in a matrix based on the neighbor values and itself. I found a generic_filter function which I used to calculate what I wanted. So here is how I can do this for a 2d array
arr = np.array([
[1, 2, 4],
[5, 6, 7],
[2, 4, 4]
])
def most_frequent(arr):
def most_frequent(val):
return Counter(val).most_common(1)[0][0]
footprint = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
return ndimage.generic_filter(arr, most_frequent, footprint=footprint, mode='constant')
print most_frequent(arr)
This returns me
[[0 0 0]
[0 4 0]
[0 0 0]]
Ignore the elements on the edge. As you see the middle element is 4 because this is a most frequent element among the neighbors and the value.
The big problem is that I need to do the same thing for a 3d matrix. So for a matrix like this
arr = np.array([
[[1, 1], [2, 2], [4, 4]],
[[5, 5], [6, 6], [7, 7]],
[[2, 2], [4, 4], [4, 4]]
])
I expect to get [0, 0] everywhere and [4, 4] in the middle. This fails with RuntimeError('filter footprint array has incorrect shape.'). The worse thing that I have doubts that I can use generic_filter here because the docs say:
cval : scalar, optional Value to fill past edges of input if mode is ‘constant’.
So how can I solve my problem?
(3,3,2), right?