numpy.all轴参数不正常? [英] numpy.all axis parameter misbehavior?

查看:102
本文介绍了numpy.all轴参数不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下数组.

a = np.array([[0, 5, 0, 5],
              [0, 9, 0, 9]])
>>>a.shape 
Out[72]: (2, 4)

>>>np.all(a,axis=0)
Out[69]: 
array([False,  True, False,  True], dtype=bool)

>>>np.all(a,axis=1)
Out[70]: 
array([False, False], dtype=bool)

因为轴0表示2D数组中的第一个轴(行),

Because axis 0 means the first axis(row-wise) in 2D array,

我希望在给出np.all(a,axis=0)时,它每行检查所有元素是否为True.

I expected when np.all(a,axis=0) is given, it checks whether all element is True or not, per every row.

但是似乎要检查每列,因为它会将输出显示为4个元素,例如array([False, True, False, True], dtype=bool).

But it seems like checking per column cause it gives output as 4 elements like array([False, True, False, True], dtype=bool).

我对np.all的功能有什么误解?

What am I misunderstanding about np.all functioning?

推荐答案

axis=0表示将元素沿沿轴与在一起,因此a[0, 0]a[1, 0]a[1, 1]等进行与"运算.指定的轴被折叠.

axis=0 means to AND the elements together along axis 0, so a[0, 0] gets ANDed with a[1, 0], a[0, 1] gets ANDed with a[1, 1], etc. The axis specified gets collapsed.

您可能会认为需要np.all(a[0])np.all(a[1])等,通过沿轴0进行索引并在每个子数组上执行np.all来选择子数组.那是相反的.会折叠除指定轴以外的所有轴.

You're probably thinking that it takes np.all(a[0]), np.all(a[1]), etc., selecting subarrays by indexing along axis 0 and performing np.all on each subarray. That's the opposite of how it works; that would collapse every axis but the one specified.

对于2D数组,一种约定比另一种约定没有太多优势,但是对于3D及更高版本,NumPy选择的约定更加有用.

With 2D arrays, there isn't much advantage for one convention over the other, but with 3D and higher, NumPy's chosen convention is much more useful.

这篇关于numpy.all轴参数不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆