Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: Why is numpy.array([2]).any() > 1 False?

Tags:

python

numpy

>>> import numpy
>>> numpy.array([2]) > 1
array([ True], dtype=bool)
>>> numpy.array([2]).any() > 1
False

Shouldn't any() test all elements of the array and return True?

like image 204
Hauke Avatar asked May 26 '26 05:05

Hauke


2 Answers

It does return True. But (True > 1) == False. While the first part is 2 > 1 which of course is True.

As others posted, you probably want:

(numpy.array([2])  > 1).any()
like image 77
seberg Avatar answered May 27 '26 18:05

seberg


Perhaps you are confusing it with this

>>> (numpy.array([2]) > 1).any()
True
like image 26
John La Rooy Avatar answered May 27 '26 19:05

John La Rooy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!