[Tutor] index of elements in numpy array
oscar.j.benjamin at gmail.com
Fri Sep 14 14:38:18 CEST 2012
On 14 September 2012 11:05, eryksun <eryksun at gmail.com> wrote:
> On Fri, Sep 14, 2012 at 4:36 AM, Bala subramanian
> <bala.biophysics at gmail.com> wrote:
> > 10 # loop over each row
> > 11 for i1, d1 in enumerate(b):
> > 12 # each x in d1 - value in z corresponding to index of x in d1
> > 13 d1=[x-z[d1.index(x)] for x in d1]
> > If d1 is a simple list, i can fetch the index of its element as
> > d1.index(x). So i would like to know how can achieve the same with
> > numpy array.
Whether you use a list or a numpy array, iterating over the elements and
then trying to get the index of each value from the value itself is
inefficient. It's also leads to problems when the array/list contains
duplicate values. I think the way to replace your line 13 would be to use:
d1 = [x - z[n] for n, x in enumerate(d1)]
There is another problem though which is that you're assigning to d1 which
is the same name that you've used for your loop variable in the outer loop.
This means that you're throwing away the values you compute. Are you hoping
that by assigning to d1, the values would get stored in b?
Perhaps what you need to do is:
b[i1, :] = [x - z[n] for n, x in enumerate(d1)]
This way the values will get stored in b. If you actually want them to be
stored in another array, say c, then create that array before the loop with
c = np.zeros_like(b)
If this is what you're trying to do, though you would be better off just
using Peter's suggestion:
c = b - np.array(z[:b.shape])
so that you don't need a loop at all.
> In general you don't want to loop over and enumerate a NumPy array.
> That's a lot slower than using vectorized operations (just like in
> MATLAB) and broadcasting. See Peter Otten's answer.
> For an 'index' operation you have a couple of options. To begin with,
> a test returns a bool array:
> >>> d = np.array([1,2,1,2,1])
> >>> d == 1
> array([ True, False, True, False, True], dtype=bool)
> You can use this bool array to index the source array:
> >>> d[d == 1]
> array([1, 1, 1])
> >>> d[d == 1] = 3
> >>> d
> array([3, 2, 3, 2, 3])
> To get the indexes, use "nonzero" or "where":
> >>> np.nonzero(d == 2)
> (array([1, 3]),)
> >>> np.where(d == 2)
> (array([1, 3]),)
The suggestions above from eryksun are closer to what actually happens when
you use the index function on a list, but you should consider whether or
not that is really what you want to do.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor