测试数组的最后 n 行是否全为零

Test that the last nth rows of an array are all zeros

我有这个数组:

A = np.array([[[ 1.8, -3.1, -3.5,  2.2],
        [ 1.5, -6.6,  1.1,  1.1],
        [ 8.9,  4.8, -1.2,  3.6],
        [ 1.3, -7.4,  7.4,  1. ],
        [ 6.3,  0. ,  0. ,  3. ],
        [ 6.3,  0. , -6.3,  0. ],
        [ 6.3, -6.3,  6.3,  3.3],
        [ 0. ,  0. ,  0. ,  0. ],
        [ 0. ,  0. ,  0. ,  0. ],
        [ 0. ,  0. ,  0. ,  0. ]]])

所以我想检查这个数组的最后 3 行是否全为零:

counter = 0
if A[last-3-rows==0]:
    counter += 1

您可以使用 np.all 来检查具有此索引的最后三行:

>>> np.all(A[:, -3:] == 0)
# or alternatively
# >>> (A[:, -3:] == 0).all()
True

如果你希望它是一个整数:

>>> np.all(A[:, -3:] == 0).astype(int)
1