[SciPy-User] Indexing question

Michael Lerner mglerner at gmail.com
Mon Aug 10 20:15:13 EDT 2009


Dav and Josef-

Thanks!

I had looked at the indexing docs, but I apparently missed the relevant
section for multidimensional indexing. I see it now.

FWIW, your explanation, coupled with the example, is crystal clear.

I was also surprised by the double-counting oddity that you mention. I had
to rephrase my problem a bit in order to deal with it.

Thanks again,

-michael

On Mon, Aug 10, 2009 at 7:05 PM, <josef.pktd at gmail.com> wrote:

> On Mon, Aug 10, 2009 at 6:50 PM, Dav Clark<dav at alum.mit.edu> wrote:
> > You should give help(nipy.docs.indexing) a read... there you will find
> > that this is the way to do what you're trying to do:
> >
> > a = zeros([3,3,3])
> > a[[0, 1], 0, 0] += 1
> >
> > # Or, if you want to do more typing
> > b = [array([0, 1]), array([0, 0]), array([0, 0])]
> > a[b] += 1
> >
> > Basically, the nth dimension of a should be indexed by the nth entry/
> > entries in a python sequence b.  In your code, you are passing in a
> > single array, which gets interpreted as a (very fancy) index into the
> > first dimension of a, so you get the 0th and 1st slab of your data
> > (the former repeated many times).  Your b is equivalent to
> > array([0,1]) for assignment (though not for reference - it gives a
> > different shape).  This actually points out an obscure feature, which
> > is that even if you have entries that are doubly (or n-ably)
> > referenced in your indexing, each element in the lvalue will only get
> > operated on once.  Thus, while I might have expected the following to
> > yield a = [2, 0], it actually yields a = [1, 0]:
> >
> > a = array([0, 0])
> > a[a] += 1 # a[0] incremented twice?  No!
> >
> > In other words, we don't need to worry about double-counting in our
> > lvalues, which I guess is OK.  It'd be a bit more intuitive for _me_
> > the other way 'round.  Any logic behind that, or perhaps just
> > implementational ease?
> >
> > As an additional idea... you could get numpy to tell you how it would
> > like to refer to a set of indices by using the where function, as in:
> >
> > where(desireda)
> >
> > Final tip - if you already have code constructing your 'b', you could
> > coerce it to a list with a[list(b)]
> >
> > Cheers,
> > Dav
> >
> >
> > On Aug 10, 2009, at 1:38 PM, Michael Lerner wrote:
> >
> >> Hi,
> >>
> >> If I have a 1-dimensional array and I want to increment some values,
> >> I can do this:
> >>
> >> In [1]: a = array([0,0,0,0,0,0,0,0])
> >>
> >> In [2]: b = array([1,2,4])
> >>
> >> In [3]: a[b] += 1
> >>
> >> In [4]: a
> >> Out[4]: array([0, 1, 1, 0, 1, 0, 0, 0])
> >>
> >> I actually have a 3-dimensional array and a list of cells within it
> >> that I'd like to increment. I can't quite seem to get the syntax
> >> right, e.g.
> >>
> >> In [5]: a = zeros([3,3,3])
> >>
> >> In [6]: b = array([[0,0,0],[1,0,0]])
> >>
> >> In [7]: a[b] += 1
> >>
> >> In [8]: a
> >> Out[8]:
> >> array([[[ 1.,  1.,  1.],
> >>         [ 1.,  1.,  1.],
> >>         [ 1.,  1.,  1.]],
> >>
> >>        [[ 1.,  1.,  1.],
> >>         [ 1.,  1.,  1.],
> >>         [ 1.,  1.,  1.]],
> >>
> >>        [[ 0.,  0.,  0.],
> >>         [ 0.,  0.,  0.],
> >>         [ 0.,  0.,  0.]]])
> >>
> >> whereas I'd like to end up with
> >>
> >> In [10]: desireda
> >> Out[10]:
> >> array([[[ 1.,  0.,  0.],
> >>         [ 0.,  0.,  0.],
> >>         [ 0.,  0.,  0.]],
> >>
> >>        [[ 1.,  0.,  0.],
> >>         [ 0.,  0.,  0.],
> >>         [ 0.,  0.,  0.]],
> >>
> >>        [[ 0.,  0.,  0.],
> >>         [ 0.,  0.,  0.],
> >>         [ 0.,  0.,  0.]]])
> >>
> >> I think speed matters too. In the worst case, I'll have an array
> >> with dimensions (400,400,30) and a list of around a million indices
> >> that I'd like to increment.
> >>
> >> Thanks,
> >>
> >> -Michael
>
> and an example as illustration
>
> Josef
>
> >>> a = np.zeros([3,3,3])
> >>> b = np.array([[0,0,0],[1,0,0]])
> >>> a[b[:,0],b[:,1],b[:,2]] += 1
> >>> a
> array([[[ 1.,  0.,  0.],
>        [ 0.,  0.,  0.],
>        [ 0.,  0.,  0.]],
>
>       [[ 1.,  0.,  0.],
>        [ 0.,  0.,  0.],
>        [ 0.,  0.,  0.]],
>
>       [[ 0.,  0.,  0.],
>        [ 0.,  0.,  0.],
>        [ 0.,  0.,  0.]]])
> >>> bz = zip([0,0,0],[1,0,0])
> >>> a[bz] += 1
> >>> a
> array([[[ 2.,  0.,  0.],
>        [ 0.,  0.,  0.],
>         [ 0.,  0.,  0.]],
>
>       [[ 2.,  0.,  0.],
>        [ 0.,  0.,  0.],
>        [ 0.,  0.,  0.]],
>
>       [[ 0.,  0.,  0.],
>        [ 0.,  0.,  0.],
>        [ 0.,  0.,  0.]]])
>
> >>
> >> --
> >> Michael Lerner, Ph.D.
> >> IRTA Postdoctoral Fellow
> >> Laboratory of Computational Biology NIH/NHLBI
> >> 5635 Fishers Lane, Room T909, MSC 9314
> >> Rockville, MD 20852 (UPS/FedEx/Reality)
> >> Bethesda MD 20892-9314 (USPS)
> >> _______________________________________________
> >> SciPy-User mailing list
> >> SciPy-User at scipy.org
> >> http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User at scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



-- 
Michael Lerner, Ph.D.
IRTA Postdoctoral Fellow
Laboratory of Computational Biology NIH/NHLBI
5635 Fishers Lane, Room T909, MSC 9314
Rockville, MD 20852 (UPS/FedEx/Reality)
Bethesda MD 20892-9314 (USPS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090810/6de7c643/attachment.html>


More information about the SciPy-User mailing list