[Numpy-discussion] Complex slicing and take
Keith Goodman
kwgoodman@gmail....
Tue Dec 29 10:25:31 CST 2009
On Tue, Dec 29, 2009 at 7:58 AM, Eric Emsellem
<emsellem@obs.univ-lyon1.fr> wrote:
> Hi (sorry if you receive this twice, but I did not see the first post appear)
>
> I have a nagging problem which I think could be solved nicely with numpy
> indexing but I cannot find the solution, except by invoking a stupid
> loop. I would like to do this with some numpy item.
>
> Problem
> ========
> I have a 2D array which is let's say 1000 x 100.
> This represents 100 1D spectra, each one containing 1000 datapoints. For
> example:
>
> startarray = random((1000,100))
>
> Assuming I have a list of indices, for example:
>
> ind = [1,2,5,6,1,2]
>
> and a corresponding list of integer shifts let's say between -100 and 100:
>
> shift = [10,20,34,-10,22,-20]
>
> I would like to do the following sum
>
> result = stararray[100+shift[0]:-100+shift[0],ind[0]]
> + stararray[100+shift[1]:-100+shift[0],ind[0]]
> + ...
>
> Basically: I would like to sum some extracted 1D line after they have been
> shifted by some amount. Note that I would like to be able to use each line
> several times (with different shifts).
>
> At the moment, I am using "take" to extract from this array some of these
> spectra, so let's start from scratch here:
>
> import numpy as num
> startarray = random((1000,100))
> take_sample = [1,2,5,6,1,2]
> temp = num.take(startarray,take_sample,axis=1)
Would it help to make temp a 1000x4 array instead of 1000x6? Could you
do that by changing take_sample to [1,2,5,6] and multiplying columns 1
and 2 by a factor of 2? That would slow down the construction of temp
but speed up the addition (and slicing?) in the loop below.
>
> # and to do the sum after shifting I need a loop
>
> shift = [10,20,34,-10,22,-20]
> result = num.zeros(900) # shorter than initial because of the shift
> for i in range(len(shift)) :
> result += temp[100+shift[i]:-100+shift[1]]
This looks fast to me. The slicing doesn't make a copy nor does the
addition. I've read that cython does fast indexing but I don't know if
that applies to slicing as well. I assume that shift[1] is a typo and
should be shift[i].
>
> Is there a way to do this without invoking a loop? Is there also a FAST
> solution which makes the shifts at the same time than the "take" (that would
> then prevent the use of the "temp" array)?
>
> Of course the arrays I am using are much BIGGER than these, so I really need an
> efficient way to do all this.
>
> thanks!
>
> Eric
>
>
> --
>
>
> --------------------------------------------------------------------------
> Ce message a été envoyé depuis le webmail IMP (Internet Messaging Program)
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list