在Numpy中数组中的元素明智的比较

时间:2022-04-14 21:42:09

I have the following array of shape(5,2,3), which is a collection of 2 * 3 arrays.

我有如下的形状数组(5,2,3),它是2 * 3数组的集合。

a = array([[[ 0,  2,  0],
    [ 3,  1,  1]],

   [[ 1,  1,  0],
    [ 2,  2,  1]],

   [[ 0,  1,  0],
    [ 3,  2,  1]],

   [[-1,  2,  0],
    [ 4,  1,  1]],

   [[ 1,  0,  0],
    [ 2,  3,  1]]])

1) How can I check if there exists a 2 * 3 array in this array of arrays where at least one element is negative in it?

1)如何检查数组中是否存在至少一个元素为负的2 * 3数组?

#which is this:
[[-1,  2,  0],
[ 4,  1,  1]]

2) After that how can I remove the above found 2 * 3 array from a?

2)在那之后,我如何删除上面的2 * 3阵列从a?

A vectorized implementation is much appreciated but looping is fine too.

矢量化的实现非常受欢迎,但是循环也是可以的。

2 个解决方案

#1


2  

You could do -

你能做的,

a[~(a<0).any(axis=(1,2))]

Or the equivalent with .all() and thus avoid the inverting -

或与.all()等价,从而避免了逆-

a[(a>=0).all(axis=(1,2))]

Sample run -

样本运行-

In [35]: a
Out[35]: 
array([[[ 0,  2,  0],
        [ 3,  1,  1]],

       [[ 1,  1,  0],
        [ 2,  2,  1]],

       [[ 0,  1,  0],
        [ 3,  2,  1]],

       [[-1,  2,  0],
        [ 4,  1,  1]],

       [[ 1,  0,  0],
        [ 2,  3,  1]]])

In [36]: a[~(a<0).any(axis=(1,2))]
Out[36]: 
array([[[0, 2, 0],
        [3, 1, 1]],

       [[1, 1, 0],
        [2, 2, 1]],

       [[0, 1, 0],
        [3, 2, 1]],

       [[1, 0, 0],
        [2, 3, 1]]])

#2


2  

Use any:

使用任何:

In [10]: np.any(a<0,axis=-1)
Out[10]: 
array([[False, False],
       [False, False],
       [False, False],
       [ True, False],
       [False, False]], dtype=bool)

Or more complete, if you want the corresponding index for (2,3) array:

或者更完整,如果你想要(2,3)数组的对应索引:

In [22]: np.where(np.any(a<0,axis=-1).any(axis=-1))
Out[22]: (array([3]),)
# Or as mentioned in comment you can pass a tuple to `any` np.where(np.any(a<0,axis=(1, 2)))

You can also get the array with a simple indexing:

您还可以通过简单的索引获得数组:

In [27]: a[np.any(a<0, axis=(1, 2))]
Out[27]: 
array([[[-1,  2,  0],
        [ 4,  1,  1]]])

#1


2  

You could do -

你能做的,

a[~(a<0).any(axis=(1,2))]

Or the equivalent with .all() and thus avoid the inverting -

或与.all()等价,从而避免了逆-

a[(a>=0).all(axis=(1,2))]

Sample run -

样本运行-

In [35]: a
Out[35]: 
array([[[ 0,  2,  0],
        [ 3,  1,  1]],

       [[ 1,  1,  0],
        [ 2,  2,  1]],

       [[ 0,  1,  0],
        [ 3,  2,  1]],

       [[-1,  2,  0],
        [ 4,  1,  1]],

       [[ 1,  0,  0],
        [ 2,  3,  1]]])

In [36]: a[~(a<0).any(axis=(1,2))]
Out[36]: 
array([[[0, 2, 0],
        [3, 1, 1]],

       [[1, 1, 0],
        [2, 2, 1]],

       [[0, 1, 0],
        [3, 2, 1]],

       [[1, 0, 0],
        [2, 3, 1]]])

#2


2  

Use any:

使用任何:

In [10]: np.any(a<0,axis=-1)
Out[10]: 
array([[False, False],
       [False, False],
       [False, False],
       [ True, False],
       [False, False]], dtype=bool)

Or more complete, if you want the corresponding index for (2,3) array:

或者更完整,如果你想要(2,3)数组的对应索引:

In [22]: np.where(np.any(a<0,axis=-1).any(axis=-1))
Out[22]: (array([3]),)
# Or as mentioned in comment you can pass a tuple to `any` np.where(np.any(a<0,axis=(1, 2)))

You can also get the array with a simple indexing:

您还可以通过简单的索引获得数组:

In [27]: a[np.any(a<0, axis=(1, 2))]
Out[27]: 
array([[[-1,  2,  0],
        [ 4,  1,  1]]])