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

初次使用python中any()函数,any用于测试数组(ndarray)中是否存在一个或多个True(用于非布尔型数组时,所有非零元素会被当作True)。
与此相关的,sum()用于统计布尔型数组中True值的个数[布尔值会被强制转换为1(True)和0(False)],all()检查数组中所有值是否都是True

《Python for Data Analysis》中一个例子(后续会说明这个例子的问题):

hits30 = (np.abs(walks) >= 30).any(1)

np.abs(walks) >= 30可以得到一个布尔型数组,表示距离是否大于等于30
执行之后会报错:’axis’ entry is out of bounds
查看np.abs(walks) >= 30的执行结果:
ndarray.any()--ValueError: 'axis' entry is out of bounds_第1张图片
查看矩阵的形状:

In [34]: (np.abs(walk) >= 30).shape
Out[34]: (1000,)

此时,以为any(1)中的1是指True,但是如果只需要查看得到的布尔型数组中是否有True的话,用(np.abs(walks) >= 30).any()就够了:

In [37]: (np.abs(walk) >= 30).any()
Out[37]: True

那么,any(1)到底是用来做什么的呢?
猜测: 1对应ndarray中是否有元素1
测试:
ndarray.any()--ValueError: 'axis' entry is out of bounds_第2张图片
发现猜测并不对,同样报错。

查看any()的详细说明:

Signature: any(a, axis=None, out=None, keepdims=False)
Docstring:
Test whether any array element along a given axis evaluates to True.
Returns single boolean unless axis is not None

Parameters

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.

.. versionadded:: 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.

Returns

any : bool or ndarray
A new boolean or ndarray is returned unless out is specified,
in which case a reference to out is returned.

以上可以看到a对应np.abs(walks) >= 30得到的布尔型数组,则any(1)括号中的数字对应axis,从我们查看该数组的shape可以看到,它是一个一维数组(1000,),则此时要么不指定axis(即选择所有元素),要么指定axis=0,即any(0):

In [40]: (np.abs(walk) >= 30).any(0)
Out[40]: True

也就是说……………………书里面应该写错了(╯°□°)╯︵ ┻━┻
呵呵……好歹又涨知识了


那么问题又来了,如果要用any()判断一个ndarray数组中是否存在某个元素应该怎么做呢?

In [41]: test = np.array([1, 2, 3])

In [42]: np.any(test==1)
Out[42]: True

In [43]: test == 1
Out[43]: array([ True, False, False], dtype=bool)

In [44]: 1 in test
Out[44]: True

可以看到test == 1就会生成一个布尔型数组,然后将其作为any()函数的参数,用来判断其中是否存在True,也就是test中有没有元素等于1。
这么绕弯。。。还不如直接用1 in test

那么又一个问题来了,多维数组怎么使用any()呢?
(n维数组axis取值范围0~n)。
以二维数组为例,看看会发生什么:

In [46]: test2 = np.array([[1, 2, 3], [0, 0, 0]])

In [47]: test2.any(0)
Out[47]: array([ True,  True,  True], dtype=bool)

In [48]: test2.any(1)
Out[48]: array([ True, False], dtype=bool)

In [52]: test3 = np.array([[0, 1, 2], [1, 0, 2]])

In [53]: test3.any(0)
Out[53]: array([ True,  True,  True], dtype=bool)

In [54]: test3.any(1)
Out[54]: array([ True,  True], dtype=bool)

因为是二维数组,axis的取值可以为0或1。
从test2和test3结果的对比可知,any(0)中的布尔值对应一元素相的值(如果是对应第一行,则test3.any(0)的结果应该为array([ False, True, True], dtype=bool)),any(1)中的布尔值对应每元素的值,any()所有元素相的值。

你可能感兴趣的:(python)