本文介绍了关于np.all与轴的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试np.all的用法,测试数组a

I tried to test the usage of np.all, the test array a is

 a=array([[[  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0],
        [  0,   0,   0]],

       [[  0,   0, 255],
        [255, 255, 255],
        [  0,   0,   0],
        [255,   0,   0]]])

b = [255,0,255]
c = np.all(a==b,axis=1)

我知道了

c= array([[False,  True, False],
   [False, False, False]], dtype=bool)

我不知道如何通过运行np.all(a==b,axis=1)获得c中的TRUE.

I don't understand how was TRUE in c obtained from running np.all(a==b,axis=1).

推荐答案

由于您正在呼叫时,将在第一个维度(即所有列(从零开始))上执行逻辑AND.

Since you are calling np.all() with axis=1, a logical AND will be performed over the first dimension i.e. all the columns (numbering starts from zero).

您的数组是:

a = np.array([[[0,   0,   0],
            [0,   0,   0],
            [0,   0,   0],
            [0,   0,   0]],

           [[0,   0, 255],
            [255, 0, 255],
            [0,   0,   0],
            [255,   255,   0]]])

因此,a的第一列(即[0, 0, 0, 0])和b的第一元素(即255)将进行与"运算,结果为False.所有操作如下:

So, the first column of a i.e. [0, 0, 0, 0] and the first element of b i.e. 255 will go through an AND operation, giving the result False. All operations are given below:

[0, 0, 0, 0] & 255 => False
[0, 0, 0, 0] & 0 => True
[0, 0, 0, 0] & 255 => False

[0, 255, 0, 255] & 255 => False
[0, 255, 0, 0] & 0 => False
[255, 255, 0, 0] & 255 => False

这将得出以下最终结果:

This will give the end result of:

[[False  True False]
 [False False False]]

因为您没有传递keepdims=True参数,所以结果列表的形状为[2, 3],即来自[2, 4, 3][1, 1, 3]的形状(请参见 NumPy广播规则),则该操作在index=1上执行.否则,结果将为[2, 1, 3]形状.

Since, you are not passing the keepdims=True parameter, the resulting list is of shape [2, 3] i.e. from [2, 4, 3] and [1, 1, 3] (see NumPy broadcasting rules), the operation is performed on index=1. Otherwise, the result would be of shape [2, 1, 3].

这篇关于关于np.all与轴的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:42