0

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.

5
  • What's an 'entry'? What's inside or outside? Commented Jul 8, 2021 at 6:19
  • An entry is an array of shape (128, 128). The array is composed of floats drawn from a normal distribution with 0 mean and unit variance. Commented Jul 8, 2021 at 6:48
  • arr>0.5 creates a boolean array with the same shape. np.sum can be used to count the number of 'True/1' values. np.sum (and similar ufunc take an axis tuple to specify summing on one or more of the dimensions. Commented Jul 8, 2021 at 6:53
  • Thank you. Could you provide a minimal example? Commented Jul 8, 2021 at 7:11
  • I have also added what I just tried. Commented Jul 8, 2021 at 7:45

1 Answer 1

2
x =  np.random.rand(32, 128, 128)
#check 80%
np.sum(x > 0.5, axis = (1, 2)) > 0.8 * 128 * 128

x > 0.5 will return True/False boolean for all values (32 * 128 * 128). After that we are summing over 1st and 2nd axis (128 * 128) to extract the total number of True values i.e. where the conditions are met for all 32 arrays and checking whether the number is more than 80%.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Could you make the example a bit more comprehensive by adding comments? Might be helpful for other readers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.