Remove 2d slice from 3d numpy array

Viewed 1048

I need to remove the last arrays from a 3D numpy cube. I have:

a = np.array(
[[[1,2,3],
 [4,5,6],
 [7,8,9]],

[[9,8,7],
 [6,5,4],
 [3,2,1]],

[[0,0,0],
 [0,0,0],
 [0,0,0]],

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

How do I remove the arrays with zero sub-arrays like at the bottom side of the cube, using np.delete?

(I cannot simply remove all zero values, because there will be zeros in the data on the top side)

5 Answers

For a 3D cube, you might check all against the last two axes

a = np.asarray(a)
a[~(a==0).all((2,1))]

array([[[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]]])

If you know where they are already, the easiest thing to do is slice them off:

a[:-2]

Results in:

array([[[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]]])

Here's one way to remove trailing all zeros slices, as mentioned in the question that we want to keep the all zeros slices in the data on the top side -

a[:-(a==0).all((1,2))[::-1].argmin()]

Sample run -

In [80]: a
Out[80]: 
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

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

In [81]: a[:-(a==0).all((1,2))[::-1].argmin()]
Out[81]: 
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]]])

Hope this helps,

a_new=[] #Create a empty list
for item in a:
        if not (np.count_nonzero(item) == 0): #check if inner matrix is empty or not
            a_new.append(item) #appending to inner matrix to the list

a_new=np.array(a_new) #creating numpy matrix with removed zero elements

Output:

array([[[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]]])

Use any and select :)

a=np.array([[[1,2,3],
 [4,5,6],
 [7,8,9]],

[[9,8,7],
 [6,5,4],
 [3,2,1]],

[[0,0,0],
 [0,0,0],
 [0,0,0]],

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