python - Deleting elements from numpy array with iteration -


what fastest method delete elements numpy array while retreiving initial positions. following code not return elements should:

list = [] pos,i in enumerate(array):     if < some_condition:         list.append(pos)  #this loop fails  _ in list:     array = np.delete(array, _) 

it feels you're going inefficiently. should using more builtin numpy capabilities -- e.g. np.where, or boolean indexing. using np.delete in loop going kill performance gains using numpy...

for example (with boolean indexing):

keep = np.ones(array.shape, dtype=bool) pos, val in enumerate(array):     if val < some_condition:         keep[pos] = false array = array[keep] 

of course, possibly simplified (and generalized) further:

array = array[array >= some_condition] 

edit

you've stated in comments need same mask operate on other arrays -- that's not problem. can keep handle on mask , use other arrays:

mask = array >= some_condition array = array[mask] other_array = other_array[mask] ... 

additionally (and perhaps reason original code isn't working), delete first index array in loop, of other items shift 1 index left, you're not deleting same items "tagged" on initial pass.

as example, lets original array [a, b, c, d, e] , on original pass, tagged elements @ indexes [0, 2] deletion (a, c)... on first pass through delete loop, you'd remove item @ index 0 -- make array:

[b, c, d, e] 

now on second iteration of delete loop, you're going delete item @ index 2 in new array:

[b, c, e] 

but look, instead of removing c wanted, removed d! oh snap!

to fix that, write loop on reversed(list), still won't result in fast operation.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -