Ricevind
Ricevind

Reputation: 351

ndarray.any() error: ValueError: 'axis' entry is out of bounds

I run in to the following problem:

Running this code

 a = np.array([1,2,3])
 a.any(2)

gives me the error: ValueError: 'axis' entry is out of bounds

It looks like the method any() receives an axis argument which is too big. When I try to specify the axis argument I receive:

a.any(2, axis=1)
--->
TypeError: _any() got multiple values for argument 'axis'

Like there was axis argument set twice.

I'm using Pyzo2014a ver.3.5 with Python 3.4.3 and numpy 1.10.1

Upvotes: 1

Views: 1339

Answers (4)

EdChum
EdChum

Reputation: 394031

I think you want:

(a == 2).any()

The method signature for any states:

a : array_like Input array or object that can be converted to an array.

axis : None or int or tuple of ints, optional Axis or axes along which a logical OR reduction is performed. The default (axis = None) is to perform a logical OR over all the dimensions of the input array. axis may be negative, in which case it counts from the last to the first axis. New in version 1.7.0. If this is a tuple of ints, a reduction is performed on multiple axes, instead of a single axis or all the axes as before.

out : ndarray, optional Alternate output array in which to place the result. It must have the same shape as the expected output and its type is preserved (e.g., if it is of type float, then it will remain so, returning 1.0 for True and 0.0 for False, regardless of the type of a). See doc.ufuncs (Section “Output arguments”) for details.

keepdims : bool, optional If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.

So you're confusing the axis param where with the value you're trying to test for hence the errors:

In [208]:
a = np.array([1,2,3])
(a==2).any()

Out[208]:
True

Upvotes: 0

tmdavison
tmdavison

Reputation: 69116

a.any() tests whether any array element along a given axis evaluates to True. To test if 2 is in a, you could use

np.any(a==2)

or

(a==2).any()

or just

2 in a

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107287

You are passing a wrong axis to any.Note that the fist argument of any is a axis of the array,and since a is a one dimensional array you can just pass 0 as it's axis:

>>> a.any(0)
True

numpy.any(a, axis=None, out=None, keepdims=False)[source]

If you'd a 2D array you could pass 0 and 1 :

>>> a = np.array([[1,2,3],[0,0,0]])
>>> a.any(0)
array([ True,  True,  True], dtype=bool)
>>> a.any(1)
array([ True, False], dtype=bool)

Upvotes: 0

Nader Hisham
Nader Hisham

Reputation: 5414

 a = np.array([1,2,3])
 a.any(2)
ValueError: 'axis' entry is out of bounds

you got this error because you have only one axis which is 0 you don't have an axis with the value

the second error

a.any(2, axis=1)
--->
TypeError: _any() got multiple values for argument 'axis'

because axis is the first argument for the method any so here you're supplying 2 as the axis and again you're assigning 1 to axis parameter

Upvotes: 0

Related Questions