Hamza
Hamza

Reputation: 6045

How to check if the number of ones is greater than the number of zeroes in a numpy array without traversing whole array?

Given a Boolean numpy nd-array, how can I found if the total number of ones is greater than the total numbe of zeros in the array without traversing the whole array with nested for loops. I meant a function in-line with any() and all(). Say max_bool() which works as follows:

def max_bool(array):
    return array.ones => array.zeros

Traversing is not an option as the dimensions of arrays, I intended to work with have diverse unpredictible dimensions and can be too large. I am not concerned about the exact number of ones & zeros either. Just that if array has more ones or zeros, even if the number of ones is just one greater than the number of zeros. Any help?

Upvotes: 0

Views: 265

Answers (1)

Daniel F
Daniel F

Reputation: 14399

Simplest way I can think of:

def max_bool(array):
    return array.mean() >= .5

Upvotes: 3

Related Questions