Suppose I have a numpy array of shape (1,4,5)
,
arr = np.array([[[ 0, 0, 0, 3, 0],
[ 0, 0, 2, 3, 2],
[ 0, 0, 0, 0, 0],
[ 2, 1, 0, 0, 0]]])
And I would like to find the most frequent non-zero value in the array across a specific axis, and only returns zero if there are no other non-zero values.
Let's say I'm looking at axis=2, I would like to get something like [[3,2,0,2]]
from this array (For the last row either 1 or 2 would be fine). Is there a good way to implement this?
I've tried the solution in this following question (Link) , but I am unsure how to modify it so that it excludes a specific value.Thanks again!