4

I have a NumPy array like:

a = np.array([1,2,3,4,0,0,5,6,7,8,0,0,9,10,11,12])

What is the most effective way to select all values except values (in my example is 0) at some positions?

So I need to get an array:

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

I know how to skip the one nth value with [::n] construction but is it possible to skip several values using the similar syntax?

Thank you for any help!

2
  • Do you want to skip number at certain positions (regardless of its value), or you want to skip number with certain value? Commented Dec 1, 2014 at 10:10
  • I would like to skip numbers ar certain positions. Thanks! Commented Dec 1, 2014 at 10:14

4 Answers 4

4

You probably want np.delete:

>>> np.delete(a, [4, 5, 10, 11])
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The only problem that I see in np.delete is that it's necessary to create a copy of the array. It can be not efficient. Also the inverted index constructing ([x for x in range(len(a)) if x not in idx]) is not so efficient because of loop or iterator. The size of array in my case can be several millions values which are separated by several symbols --> it's not efficient to check with "if" operator every values. In any case thanks for your help!
You can only get views (i.e no copy) if the subarray can be indexed with a slice, a[start:stop:step], which isn't your case. A copy will be made regardless, np.delete is the fastest way to produce it.
2

I see two options:

  1. If you want to get an index vector that you can use on multiple arrays:

    import numpy as np
    
    #your input
    a = np.array([1,2,3,4,0,0,5,6,7,8,0,0,9,10,11,12])
    #indices of elements that you want to remove (given)
    idx = [4,5,10,11]
    #get the inverted indices
    idx_inv = [x for x in range(len(a)) if x not in idx]
    a[idx_inv]
    

    This output:

    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
    
  2. Use np.delete:

    import numpy as np
    
    #your input
    a = np.array([1,2,3,4,0,0,5,6,7,8,0,0,9,10,11,12])
    #indices of elements that you want to remove (given)
    idx = [4,5,10,11]
    
    np.delete(a,idx)
    

    This outputs:

    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
    

1 Comment

Please try to avoid python loops when relating to numpy arrays. idx_inv can be computed using np.in1d much more efficiently.
1

Using boolean or mask index array:

>>> a = np.array([1,2,3,4,0,0,5,6,7,8,0,0,9,10,11,12])
>>> a[a != 0]
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

3 Comments

Thank you!But I need to skip the values at some positions. In my example it's "0" (just for example) but it can be any values. So it's not possible to use the boolean or mask array directly like a[a!=0].
@vitmol, Change the value as you want. For example: a[(a != 4) & (a != 0)] or a[a < 3]
Falsetru you proposed to find an index of skipped elements based on value of the element. It's very straightforward and works fine for other situation. In my situation I know which values I would like to skip. (e.g. skip 4,5,10,11 etc). But it's base on position, not based on value. I only need a fast solution to perform such slicing. Thank you for your help!
1

You can use Boolean array indexing:

import numpy as np
a = np.array([1,2,3,4,0,0,5,6,7,8,0,0,9,10,11,12])
print a[a != 0]
# Output: [ 1  2  3  4  5  6  7  8  9 10 11 12]

and you can change a != 0 to other conditions which result in a Boolean array.

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.