Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.all with integer arguments returns an integer

Tags:

python

numpy

Why do this happen?

>>> map(numpy.all, range(-2, 3))
[-2, -1, 0, 1, 2]

Is it intentional or is the integer just falling through a crack?

Does it have to do with:

>>> map(numpy.all, [False, True])
[False, True]

I'm running Numpy 1.8.0.dev-74b08b3 and Python 2.7.3

like image 763
SlimJim Avatar asked May 07 '13 18:05

SlimJim


People also ask

What does NumPy all do?

The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True. Parameters : array :[array_like]Input array or object whose elements, we need to test.

How do you check if all values in a NumPy array are true?

If we have a Numpy array with boolean, True or False data, we can use np. all() to check if all of the elements are True .

What is the default data type of NumPy array?

The default data type: float_ . The 24 built-in array scalar type objects all convert to an associated data-type object.

Can NumPy array have different data types?

Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.


1 Answers

Using map(numpy.all, range(-2,3)) is actually creating a list with:

[numpy.all(-2), numpy.all(-1), numpy.all(0), numpy.all(1), numpy.all(2)]

giving

[-2, -1, 0, 1, 2]

If you did map(lambda x: numpy.all([x]), range(-2,3)), it would do:

[numpy.all([-2]), numpy.all([-1]), numpy.all([0]), numpy.all([1]), numpy.all([2])]

giving

[True, True, False, True, True]

As posted by @Mark Dickinson, there is a known issue with numpy.all in which it returns the input value instead of True or False for some inputs. In your second example map(numpy.all, [False, True]) does exactly as before, returning the input value.

like image 52
Saullo G. P. Castro Avatar answered Oct 21 '22 09:10

Saullo G. P. Castro