0

I am trying to remove sub-arrays from a multidimensional numpy array using a condition. In this exampe I want to remove all sub-arrays which include the value 999. Here is one of my failed attempts:

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

for i in range(0,len(a)):
    if 999 in a[i]:
        np.delete(a, i, 0)

The result I want is:

array([[1,2,3], [1,2,3]])

This is just a smaler exaple, which should help me understand a lager issue, which loos like that:

# win_list_hyper.shape -> (1449168, 233)
# win_list_multi.shape -> (1449168, 12, 5, 5)

win_list_hyper = np.where(win_list_hyper <= 0, -3.40282e+38, win_list_hyper)
win_list_multi = np.where(win_list_multi <= 0, -3.40282e+38, win_list_multi)


# fail!:
for i in range(0,len(win_list_multi)):
    
    if -3.40282e+38 in win_list_multi[i] or -3.40282e+38 in win_list_hyper[i]:
        
        np.delete(win_list_multi, i, 0)
        np.delete(win_list_hyper, i, 0)

(btw. if you know how to make this more efficient, please let me know aswell!)

2 Answers 2

2

Your 1st attempt fails, since np.delete does not operate inplace (i.e. it does not modify the array, it returns a new one). Also, deleting elements from an array while iterating over it is usually not a good idea (unless you know what you are doing).

You can just use np.where as follows:

inds = np.where(a == 999)  # get indices where value equals 999
np.delete(a, inds[0], axis=0)   # delete along first dimension

Result:

array([[[1, 2, 3],
        [1, 2, 3]]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. I had to realize that my example wasn't good. I coudn't translate your solution to my data that easily. Never the less you helped me a lot. Your solution gave me the Idea to mask my arrays. I hope my solution doesn't mess with my data (e.g. shuffles it).
0

Jussi Nurminen solution works fine for my example, but I had to realize that my example wasn't good. I coudn't translate the given solution to my data that easily. Never the less Jussi Nurminen solution helped me a lot, because it gave me the Idea to mask my arrays. I hope my solution doesn't mess with my data (e.g. shuffles it). For those who are interestet...

... This is my solution for my (bad) example:

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

a_mask = []

for i in range(0,len(a)):
    if 999 in a[i]:
        x = 0
    else: x = 1
    
    a_mask.append(x)
 
a_mask = np.asarray(a_mask)
    
inds = np.where(a_mask == 0)
        
b = np.delete(a, inds, axis=0) 

... And this is how it looks like translatet to my data:


# win_list_multi.shape -> (1449168, 12, 5, 5)
# win_list_hyper.shape -> (1449168, 233


win_list_multi = np.where(win_list_multi <= 0, -1, win_list_multi)

win_list_hyper = np.where(win_list_hyper <= 0, -1, win_list_hyper)


win_list_multi_mask = []

for i in range(0,len(win_list_multi)):
    if -1 in win_list_multi[i]:
        x = 0
    else: x = 1
    
    win_list_multi_mask.append(x)

win_list_multi_mask = np.asarray(win_list_multi_mask)



win_list_hyper_mask = []

for i in range(0,len(win_list_hyper)):
    if -1 in win_list_hyper[i]:
        x = 0
    else: x = 1
    
    win_list_hyper_mask.append(x)

win_list_hyper_mask = np.asarray(win_list_hyper_mask)



inds = np.where((win_list_multi_mask == 0) | (win_list_hyper_mask == 0))


win_list_multi_nd = np.delete(win_list_multi, inds, axis=0) 
win_list_hyper_nd = np.delete(win_list_hyper, inds, axis=0) 

# win_list_multi_nd.shape -> (9679, 12, 5, 5)
# win_list_hyper_nd.shape -> (9679, 233)

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.