I have a multi-dimensional NumPy array with the shape (32, 128, 128). For each entry in this array (which is of shape (128, 128), I would like to check if 80% of the values present inside it are greater than a threshold, say 0.5.
Currently, I am doing something like so:
for entry in entries: # entries: (32, 128, 128)
raveled = np.ravel(entry) # entry: (128, 128)
total_sum = (raveled > 0.5).sum()
proportion = total_sum/len(raveled)
if proportion > 0.8:
...
I cannot seem to figure out an efficient way to do this. Any help would be appreciated.
arr>0.5creates a boolean array with the same shape.np.sumcan be used to count the number of 'True/1' values.np.sum(and similarufunctake anaxistuple to specify summing on one or more of the dimensions.