[Numpy-discussion] speeding up getting a subset of a data array

josef.pktd@gmai... josef.pktd@gmai...
Mon Aug 10 19:58:53 CDT 2009


On Mon, Aug 10, 2009 at 8:52 PM, Brennan
Williams<brennan.williams@visualreservoir.com> wrote:
> Hi
>
> No doubt asked many times before so apologies....
>
> I'm pulling a subset array out of a data array where I have a list of
> the indices I want (could be an array rather than a list actually - I
> have it in both).
>
> Potentially the number of points and the number of times I do this can
> get very large so any saving in time is good.
>
> So, paraphrasing what I've currently got.... say I have...
>
> subsetpointerlist=[0,1,2,5,8,15,25...]
> subsetsize=len(subsetpointerlist)
> subsetarray=zeros(subsetsize,dtype=float)
> for index,pos in enumerate(subsetpointerlist):
>  subsetarray[index]=dataarray[pos]
>
> How do I speed this up in numpy, i.e. by removing the for loop?
>
> Do I set up some sort of a subsetpointerarray as a mask and then somehow
> apply that to dataarray to get the values into subsetarray?
>
> Thanks
>
> Brennan
>
>

looks to me like

subsetarray = dataarray[subsetpointerlist]

or with type conversion

subsetarray = dataarray[subsetpointerlist].astype(float)

Josef

>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list