<br><br><div class="gmail_quote">On 25 June 2012 08:24, Stefan Behnel <span dir="ltr"><<a href="mailto:stefan_ml@behnel.de" target="_blank">stefan_ml@behnel.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Saurabh Kabra, <a href="tel:25.06.2012%2005" value="+12506201205">25.06.2012 05</a>:37:<br>
<div><div class="h5">> 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></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
><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></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><br></div><div>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><br></div><div><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><br></div><div>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><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc 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">http://docs.cython.org/src/tutorial/numpy.html</a></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<span class="HOEnZb"><font color="#888888"><br>
Stefan<br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>