3

Here is my problem:

I use numpy any() function to check if my array is empty or not.

a = numpy.array([1., 2., 3.])
a.any()
#True


a = numpy.array([0., 0., 0.])
a.any()
#False

I would think that, given that 0. is a float, the any() function from numpy would return True. How can I make this happen?

What's the reason behind the fact that zeros are not considered as actual values by numpy?

I use python 2.6

3
  • They are considered "actual values", they're just actual values that evaluate False. Commented Aug 4, 2014 at 9:40
  • 1
    @jonrsharpe: Be careful; they don't actually evaluate to False, but they do evaluate to a false value. That's why the community has the terms "truthy" and "falsy", to avoid that confusion. Well, that, and to stubbornly remind everyone that someone in the Python community came up with "truthiness" before Stephen Colbert, dammit. :) Commented Aug 4, 2014 at 9:47
  • @abarnert good point, thanks Commented Aug 4, 2014 at 9:53

2 Answers 2

3

What you are observing is actually expected: any() means "is there any element whose boolean value is true in this array?". Since the boolean value of 0. is false (non-zero numbers are true), it is normal that a.any() is false when the array only contains zeroes.

You can check the boolean value of any Python object with bool().

If you need to know if your array has any element, then you can test a.size (0 for no elements).

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

Comments

2

What's the reason behind the fact that zeros are not considered as actual values by numpy?

It's a general principle in Python that "falsey" means False, None, a numeric zero*, or an empty collection. See Truth Value Testing in the documentation for the exact rules.** Different languages have different rules for what counts as truthy vs. falsy***; these are Python's.

And NumPy follows that general principle. So, zeros are considered as actual values, but they're actual false values.

So, an array full of numeric zero values does not have any truthy members, therefore calling any on it will return False.


* Note that in some cases, a value that rounds to 0.0 isn't exactly zero, in which case it may be, confusingly, true. Just one more way floating point rounding errors suck… If you really need to check that the values are non-zero, check whether they're within some appropriate epsilon of zero, rather than checking exact values. NumPy has a number of useful helpers here.

** I left out the rule that custom types can decide which values are truthy or falsy by defining a __bool__ method, or various fallbacks which depend on your exact Python version. That's how things work under the hood. But for the designer of such a class, her class should try to follow the general principle; whatever it means for her values which are "zero" or "empty" or "false" or "nonexistent", that's the rule that her __bool__ method should apply.

*** In C-family languages, it's generally zeros and NULL pointers that are falsy. In Lisp-family languages, it's only the empty list or closely-related values. In Ruby and Swift, it's just false and nil. And so on. Any rule will be counter-intuitive in some cases; as long as the language and its ecosystem are consistent, that's as good as you can hope for. (If you have to use a language that isn't consistent, like PHP or JavaScript, you'll have to keep the docs handy…)

3 Comments

Ok. I did not know that. It seemed counter-intuitive to me that a float value would be returned as False (especially in the case of an array, in which getting zeros as a results is very possible), but if that's the way it is, fair enough!
@WilliamAbma: Every language has its own rules for truthiness, and every rule you come up with will be counterintuitive in some cases but not others; as long as a language (and its stdlib and popular third-party libraries) is consistent, that's really all you can ask for.
@EOL: Not almost every language. Lisp and most of its derivatives, Ruby, Swift, etc. do not treat 0 as false. It's mainly just C-derived (and some other Algol-derived) languages, Python, and Perl that treat 0 as false.

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.