[SciPy-User] re[SciPy-user] moving for loops...

Zachary Pincus zachary.pincus at yale.edu
Fri May 21 11:46:58 EDT 2010


> Thanks that works...
>
> So the way to do it is with np.arange(tsteps)[:,None], that was the  
> step I
> was struggling with, so this forms a 2D array which replaces the the  
> two for
> loops? Do I have that right?

If tsteps is just the size of the array in that dimension, you can  
use :, as before:
data[:,5,index,0]
which will be quicker and more straightforward.

If you want to index with multiple list-of-indices along different  
axes, then Josef's point about broadcasting is a good one (and the  
answer to the question I'd asked, actually...)

Given:
a = numpy.arange(100).reshape((10,10))
Then:
a[numpy.array([0,4,2])[:,numpy.newaxis], [1,2]]
or equivalently:
a[[[0],[4],[2]], [1,2]]

yields:
array([[ 1,  2],
        [41, 42],
        [21, 22]])

That is, the 0th, 4th, and 2nd rows, and the 1st and 2nd columns of a.

Thanks Josef!

Zach


> A lot quicker...!
>
> Martin
>
>
> josef.pktd wrote:
>>
>> On Fri, May 21, 2010 at 8:59 AM, mdekauwe <mdekauwe at gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> I am trying to extract data from a 4D array and store it in a 2D  
>>> array,
>>> but
>>> avoid my current usage of the for loops for speed, as in reality the
>>> arrays
>>> sizes are quite big. Could someone also try and explain the  
>>> solution as
>>> well
>>> if they have a spare moment as I am still finding it quite  
>>> difficult to
>>> get
>>> over the habit of using loops (C convert for my sins). I get that  
>>> one
>>> could
>>> precompute the indices's i and j i.e.
>>>
>>> i = np.arange(tsteps)
>>> j = np.arange(numpts)
>>>
>>> but just can't get my head round how i then use them...
>>>
>>> Thanks,
>>> Martin
>>>
>>> import numpy as np
>>>
>>> numpts=10
>>> tsteps = 12
>>> vari = 22
>>>
>>> data = np.random.random((tsteps, vari, numpts, 1))
>>> new_data = np.zeros((tsteps, numpts), dtype=np.float32)
>>> index = np.arange(numpts)
>>>
>>> for i in xrange(tsteps):
>>>   for j in xrange(numpts):
>>>       new_data[i,j] = data[i,5,index[j],0]
>>
>> The index arrays need to be broadcastable against each other.
>>
>> I think this should do it
>>
>> new_data = data[np.arange(tsteps)[:,None], 5, np.arange(numpts), 0]
>>
>> Josef
>>>
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/removing-for-loops...-tp28633477p28633477.html
>>> Sent from the Scipy-User mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> 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
>>
>>
>
> -- 
> View this message in context: http://old.nabble.com/removing-for-loops...-tp28633477p28634924.html
> Sent from the Scipy-User mailing list archive at Nabble.com.
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user




More information about the SciPy-User mailing list