<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Thanks guys </span></div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><div>
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">I implemented a numpy array with fancy indices and got rid of the list and the loops. The time to do the mapping improved ~10x. As a matter of fact, the number of elements in array A to be summed and mapped was different for each element in B (which was the reason I was using lists). But I solved that problem by simply adding zero elements to make a regular 3D numpy array out of the list. </span></div>
<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><div>Saurabh</div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br>
</span></div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br>
</span></div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br>
</span></div>On 25 June 2012 08:24, Stefan Behnel </span><span dir="ltr" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><<a href="mailto:stefan_ml@behnel.de" target="_blank" style="color:rgb(17,85,204)">stefan_ml@behnel.de</a>></span><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"> wrote:</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Saurabh Kabra, <a href="tel:25.06.2012%2005" value="+12506201205" target="_blank" style="color:rgb(17,85,204)">25.06.2012 05</a>:37:<br><div>> I have written a script to map a 2D numpy array(A) onto another array(B) of<br>
> different dimension. more than one element (of array A) are summed and<br>> mapped to each element of array B.  To achieve this I create a list where I<br>> store the index of array A to be mapped to array B. The list is the<br>
> dimension of array B (if one can technically say that) and each element is<br>> a list of indices to be summed. Then I parse this list with a nested loop<br>> and compute each element of array B. </div></blockquote>
<blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div>><br>> Because of the nested loop and the big arrays the process takes a minute or<br>> so. My question is: is there a more elegant and significantly faster way of<br>> doing this in python?<br><br></div>
I'm sure there's a way to do this kind of transformation more efficiently<br>in NumPy. I faintly recall that you can use one array to index into<br>another, something like that might do the trick already. In any case, using<br>
a NumPy array also for the mapping matrix sounds like a straight forward<br>thing to try.<br></blockquote><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br>
</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">I can't tell from the description of the problem what you're trying to do but for the special case of summing along one axis of a numpy array of dimension N to produce a new numpy array of dimension N-1, there is  fast builtin support in numpy:</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<div>>>> import numpy</div><div>>>> a = numpy.array([[1, 2], [3, 4]])</div><div>>>> a</div><div>array([[1, 2],</div><div>       [3, 4]])</div><div>>>> a.sum()   # sum of all elements</div>
<div>10</div><div>>>> a.sum(axis=1)  # sum of each row</div><div>array([3, 7])</div><div>>>> a.sum(axis=0)  # sum of each column</div><div>array([4, 6])</div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">If your problem is not in this form, you can use numpy's fancy indexing to convert it to this form, provided the number of elements summed from A is the same for each element of B (i.e. if each element of B is the result of summing exactly 10 elements chosen from A).</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>But you might also want to take a look at Cython. It sounds like a problem<br>where a trivial Cython implementation would seriously boost the performance.<br><br><a href="http://docs.cython.org/src/tutorial/numpy.html" target="_blank" style="color:rgb(17,85,204)">http://docs.cython.org/src/tutorial/numpy.html</a></blockquote>
<blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br><font color="#888888"><br>Stefan<br></font></blockquote>