2

Im trying to delete specific rows in my numpy array that following certain conditions.

This is an example:

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

I want to able to delete all rows, where specific columns are zero, this array could be a lot bigger. In this example, if first two element are zero, or if last two elements are zero, the rows will be deleted.

It could be any combination, no only first element or last ones.

This should be the final:

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

For example If I try:

a[:,0:2] == 0

After reading:

But they don't seem to apply to my case, or probably I'm not understanding something here as nothing works my case.

This gives me all rows there the first two cases are zero, True, True

array([[False, False],
   [ True,  True],
   [ True, False],
   [False,  True],
   [ True,  True],
   [False,  True]])

and for the last two columns being zero, the last row should be deleted too. So at the end I will only be left with 2 rows.

 a[:,3:5] == 0

 array([[ True, False],
       [False, False],
       [False, False],
       [ True, False],
       [ True, False],
       [ True,  True]])

Im trying something like this, but I don't understand now how to tell it to only give me the rows that follow the condition, although this only :

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

      array([[ True,  True, False, False, False],
             [False, False,  True,  True, False],
             [False, False, False, False, False],
             [False, False, False, False, False],
             [False, False,  True,  True, False],
             [False, False, False, False, False]])


 (a[((a[:,0])& (a[:,1])) ] == 0).all(axis=1)

and this shows everything as False

could you please guide me a bit? thank you

Just adding in the question, that the case it wont always be the first 2 or the last 2. If my matrix has 35 columns, it could be the column 6th to 10th, and then column 20th and 25th. An user will be able to decide which columns they want to get deleted.

0

3 Answers 3

2

Try this

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

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

a[~(idx0 | idx1)]

The first two steps select the indices of the rows that match your filtering criteria. Then do an or (|) operation, and the not (~) operation to obtain the final indices you want.

Sign up to request clarification or add additional context in comments.

Comments

0

If I understood correctly you could do something like this:

import numpy as np

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

left = np.count_nonzero(a[:, :2], axis=1) != 0
a = a[left]

right = np.count_nonzero(a[:, -2:], axis=1) != 0
a = a[right]

print(a)

Output

[[1 1 0 0 1]
 [0 1 0 1 1]
 [1 0 1 0 1]]

Or, a shorter version:

left = np.count_nonzero(a[:, :2], axis=1) != 0
right = np.count_nonzero(a[:, -2:], axis=1) != 0
a = a[(left & right)]

3 Comments

Thank you, but that is very specific for only the first two element that are zero. So, if I have a very long list, [0,1,0,0,0,1,0,0,0,1,1,1], it could be that maybe the case number 1, 3 and 5,6 being zero.
@Learner What will be other cases, note this also considers the last two.
So, if I have a very long list, [0,1,0,0,0,1,0,0,0,1,1,1], it could be that maybe the case number 1, 3 and 5,6 being zero, so it could be any random set of cases, that maybe the user can decide at an specific time
0

Use the following mask:

[np.any(a[:,:2], axis=1) & np.any(a[:,:-2], axis=1)]

if you want to create a filtered view:

a[np.any(a[:,:2], axis=1) & np.any(a[:,:-2], axis=1)]

if you want to create a new array:

np.delete(a,np.where(~(np.any(a[:,:2], axis=1) & np.any(a[:,:-2], axis=1))), axis=0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.