Re: deleting certain entries in numpy array

Discussion in 'Python' started by Robert Kern, Jul 1, 2009.

  1. Robert Kern

    Robert Kern Guest

    On 2009-07-01 09:51, Sebastian Schabe wrote:
    > Hello everybody,
    >
    > I'm new to python and numpy and have a little/special problem:


    You will want to ask numpy questions on the numpy mailing list.

    http://www.scipy.org/Mailing_Lists

    > I have an numpy array which is in fact a gray scale image mask, e.g.:
    >
    > mask =
    > array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 255, 255, 255, 0, 0, 255, 0],
    > [ 0, 0, 255, 255, 255, 0, 0, 255, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    > [ 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >
    > and I have another array of (y, x) positions in that mask (first two
    > values of each row):
    >
    > pos =
    > array([[ 3., 2., 0., 0.],
    > [ 3., 4., 0., 0.],
    > [ 5., 2., 0., 0.],
    > [ 5., 4., 0., 0.],
    > [ 6., 2., 0., 0.],
    > [ 6., 7., 0., 0.],
    > [ 0., 0., 0., 0.],
    > [ 8., 8., 0., 0.]])
    >
    > and now I only want to keep all lines from 2nd array pos with those
    > indices that are nonzero in the mask, i.e. line 3-6 (pos[2]-pos[5]).
    >
    > F.e. line 4 in pos has the values (5, 4) and mask[5][4] is nonzero, so I
    > want to keep it. While line 2 (pos[1]) has the values (4, 6) and
    > mask[4][6] is zero, so shall be discarded.
    >
    > I want to avoid a for loop (if possible!!!) cause I think (but don't
    > know) numpy array are handled in another way. I think numpy.delete is
    > the right function for discarding the values, but I don't know how to
    > build the indices.


    First, convert the pos array to integers, and just the columns with indices in them:

    ipos = pos[:,:2].astype(int)

    Now check the values in the mask corresponding to these positions:

    mask_values = mask[ipos[:,0], ipos[:,1]]

    Now extract the rows from the original pos array where mask_values is nonzero:

    result = pos[mask_values != 0]

    --
    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
     
    Robert Kern, Jul 1, 2009
    #1
    1. Advertising

  2. Robert Kern

    Robert Kern Guest

    On 2009-07-02 04:40, Sebastian Schabe wrote:
    > Robert Kern schrieb:


    > > You will want to ask numpy questions on the numpy mailing list.
    > >
    > > http://www.scipy.org/Mailing_Lists
    > >

    >
    > I ever thought news-groups are the right way for questions like this.
    > And the mailing list page just confuses me, but I'm trying to get used
    > to it.


    You may want to try GMane, then:

    http://dir.gmane.org/gmane.comp.python.numeric.general

    It's how I read this list, for instance.

    --
    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
     
    Robert Kern, Jul 2, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    394
    Travis E. Oliphant
    Mar 1, 2006
  2. Duncan Smith
    Replies:
    3
    Views:
    450
    Duncan Smith
    Apr 25, 2007
  3. Replies:
    2
    Views:
    519
    Robert Kern
    Nov 13, 2007
  4. W. eWatson
    Replies:
    2
    Views:
    1,048
    W. eWatson
    Nov 23, 2009
  5. Don Bruder
    Replies:
    3
    Views:
    1,018
    spikeysnack
    Aug 3, 2010
Loading...

Share This Page