Slicing a numpy array and getting the "complement"

5 messages
Open this post in threaded view
|

Slicing a numpy array and getting the "complement"

 Given a slice, such as s_[..., :-2:], is it possible to take the complement of this slice?  Specifically, s_[..., ::-2].  I have a series of 2D arrays that I need to split into two subarrays via slicing where the members of the second array are all the members leftover from the slice.  The problem is that the slice itself will vary, and could be anything such as s_[..., 1:4:] or s_[..., 1:-4:], etc, so I'm wondering if there's a straightforward idiom or routine in Numpy that would facilitate taking the complement of a slice?  I've looked around the docs, and have not had much luck. Thanks! Orest _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

Re: Slicing a numpy array and getting the "complement"

 2008/5/19 Orest Kozyar <[hidden email]>: > Given a slice, such as s_[..., :-2:], is it possible to take the > complement of this slice?  Specifically, s_[..., ::-2].  I have a > series of 2D arrays that I need to split into two subarrays via > slicing where the members of the second array are all the members > leftover from the slice.  The problem is that the slice itself will > vary, and could be anything such as s_[..., 1:4:] or s_[..., 1:-4:], > etc, so I'm wondering if there's a straightforward idiom or routine in > Numpy that would facilitate taking the complement of a slice?  I've > looked around the docs, and have not had much luck. If you are using boolean indexing, of course complements are easy (just use ~). But if you want slice indexing, so that you get views, sometimes the complement cannot be expressed as a slice: for example: A = np.arange(10) A[2:4] The complement of A[2:4] is np.concatenate((A[:2],A[4:])). Things become even more complicated if you start skipping elements. If you don't mind fancy indexing, you can convert your index arrays into boolean form: complement = A==A complement[idx] = False Anne _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

Re: Slicing a numpy array and getting the "complement"

 In reply to this post by orest.kozyar On Mon, May 19, 2008 at 9:34 AM, Orest Kozyar <[hidden email]> wrote: > Given a slice, such as s_[..., :-2:], is it possible to take the > complement of this slice?  Specifically, s_[..., ::-2]. Hmm, that doesn't look like the complement. Did you mean s_[..., -2:] and s_[..., :-2]? > I have a > series of 2D arrays that I need to split into two subarrays via > slicing where the members of the second array are all the members > leftover from the slice.  The problem is that the slice itself will > vary, and could be anything such as s_[..., 1:4:] or s_[..., 1:-4:], > etc, so I'm wondering if there's a straightforward idiom or routine in > Numpy that would facilitate taking the complement of a slice?  I've > looked around the docs, and have not had much luck. In general, for any given slice, there may not be a slice giving the complement. For example, the complement of arange(6)[1:4] should be array([0,4,5]), but there is no slice which can make that. Things get even more difficult with start:stop:step slices let alone simultaneous multidimensional slices. Can you be more specific as to exactly the variety of slices you need to support? -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  -- Umberto Eco _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion