ndarray.any()--ValueError: 'axis' entry is out of bounds
来源:互联网 发布:专业网络零售商的优势 编辑:程序博客网 时间:2024/04/30 04:49
初次使用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).shapeOut[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 unlessaxis
is notNone
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 ofa
).
Seedoc.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 originalarr
.Returns
any : bool or ndarray
A new boolean orndarray
is returned unlessout
is specified,
in which case a reference toout
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]: TrueIn [43]: test == 1Out[43]: array([ True, False, False], dtype=bool)In [44]: 1 in testOut[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()
为所有元素相或
的值。
- ndarray.any()--ValueError: 'axis' entry is out of bounds
- ValueError: y_i value out of bounds
- 【IndexError: index 1 is out of bounds for axis 0 with size 1】
- TypeError: value is out of bounds
- MNC用自己的数据运行得到如下错误:IndexError: index 4 is out of bounds for axis 1 with size 4
- TFS Administration Error : Index 14834 is out of bounds
- Python Numpy.ndarray ValueError:assignment destination is read-only
- ArgumentException: Index out of bounds
- Illegal redeclaration of module <glbl>.&Element index 2 into memp is out of bounds
- Return code of 127 is out of bounds - plugin may be missing
- unity各种报错之:Mesh.colors is out of bounds. The supplied array needs ...
- weiphp 2.0:Your requested sheet index: 1 is out of bounds. The actual number of sheets is 0
- flexlib dataGrid out of bounds 解决方法
- Range or index out of bounds
- 数组溢出 Array out of bounds
- raised exception "Index out of Bounds"
- Range {2147483648, 4294967295} out of bounds;
- CWE -- Out-of-bounds Write 例子
- Activity和Service的生命周期(图)
- CGContextRef
- Spring的SimpleAliasRegistry类解析
- Excel快捷键
- MySQL数据库更改用户密码
- ndarray.any()--ValueError: 'axis' entry is out of bounds
- 函数调用
- 线性表(双向链表)
- 文章标题
- 欢迎使用CSDN-markdown编辑器
- shell-4-数组
- Leetcode: 168. Excel Sheet Column Title(JAVA)
- An Example of Perceptron Learning: Logic Functions
- Eclipse中自动生成versionName的一种方法