您的位置:首页 > 产品设计 > UI/UE

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

2016-03-21 16:07 756 查看
初次使用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
的执行结果:



查看矩阵的形状:

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

测试:



发现猜测并不对,同样报错。

查看
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)

a3cd
结果应该为
array([ False,  True,  True], dtype=bool)
),
any(1)
中的布尔值对应每元素的
值,
any()
所有元素相
的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python