Heh,<br><br>This is fun. Two more variations with 1000 reps instead of 100 for better timing:<br><br><span style="font-family: courier new,monospace;">def numpy_nmean_conv_nl_tweak1(list,n):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp; b = numpy.ones(n,dtype=float)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp; a = numpy.convolve(list,b,mode=&quot;full&quot;)
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp; a[:n] /= numpy.arange(1, n + 1)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
&nbsp;&nbsp; a[n:] /= n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp; return a[:len(list)]</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">def numpy_nmean_conv_nl_tweak2(list,n):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp; b = numpy.ones(n,dtype=float)
</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp; a = numpy.convolve(list,b,mode=&quot;full&quot;)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
&nbsp;&nbsp; a[:n] /= numpy.arange(1, n + 1)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp; a[n:] *= 1.0/n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
&nbsp;&nbsp; return a[:len(list)]</span><br><br>Which gives<br><br><span style="font-family: courier new,monospace;">numpy convolve took: 2.630000 sec.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
numpy convolve noloop took: 0.320000 sec.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">numpy convolve noloop tweak1 took: 0.250000 sec.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">numpy convolve noloop tweak2 took: 0.240000 sec.</span><br><br>Chuck<br><br><div><span class="gmail_quote">On 8/2/06, <b class="gmail_sendername">Phil Ruggera</b> &lt;<a href="mailto:pruggera@gmail.com">
pruggera@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">A variation of the proposed convolve routine is very fast:
<br><br>regular python took: 1.150214 sec.<br>numpy mean slice took: 2.427513 sec.<br>numpy convolve took: 0.546854 sec.<br>numpy convolve noloop took: 0.058611 sec.<br><br>Code:<br><br># mean of n values within an array<br>
import numpy, time<br>def nmean(list,n):<br>&nbsp;&nbsp;&nbsp;&nbsp;a = []<br>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(1,len(list)+1):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start = i-n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;divisor = n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if start &lt; 0:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;divisor = i<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.append(sum(list[start:i])/divisor)<br>&nbsp;&nbsp;&nbsp;&nbsp;return a<br><br>t = [1.0*i for i in range(1400)]<br>start = time.clock()<br>for x in range(100):<br>&nbsp;&nbsp;&nbsp;&nbsp;reg = nmean(t,50)<br>print &quot;regular python took: %f sec.&quot;%(
time.clock() - start)<br><br>def numpy_nmean(list,n):<br>&nbsp;&nbsp;&nbsp;&nbsp;a = numpy.empty(len(list),dtype=float)<br>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(1,len(list)+1):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start = i-n<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if start &lt; 0:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i-1] = list[start:i].mean(0)
<br>&nbsp;&nbsp;&nbsp;&nbsp;return a<br><br>t = numpy.arange(0,1400,dtype=float)<br>start = time.clock()<br>for x in range(100):<br>&nbsp;&nbsp;&nbsp;&nbsp;npm = numpy_nmean(t,50)<br>print &quot;numpy mean slice took: %f sec.&quot;%(time.clock() - start)<br><br>
def numpy_nmean_conv(list,n):<br>&nbsp;&nbsp;&nbsp;&nbsp;b = numpy.ones(n,dtype=float)<br>&nbsp;&nbsp;&nbsp;&nbsp;a = numpy.convolve(list,b,mode=&quot;full&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(0,len(list)):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i &lt; n :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i] /= i + 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else :
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i] /= n<br>&nbsp;&nbsp;&nbsp;&nbsp;return a[:len(list)]<br><br>t = numpy.arange(0,1400,dtype=float)<br>start = time.clock()<br>for x in range(100):<br>&nbsp;&nbsp;&nbsp;&nbsp;npc = numpy_nmean_conv(t,50)<br>print &quot;numpy convolve took: %f sec.&quot;%(
time.clock() - start)<br><br>def numpy_nmean_conv_nl(list,n):<br>&nbsp;&nbsp;&nbsp;&nbsp;b = numpy.ones(n,dtype=float)<br>&nbsp;&nbsp;&nbsp;&nbsp;a = numpy.convolve(list,b,mode=&quot;full&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;for i in range(n):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i] /= i + 1<br>&nbsp;&nbsp;&nbsp;&nbsp;a[n:] /= n
<br>&nbsp;&nbsp;&nbsp;&nbsp;return a[:len(list)]<br><br>t = numpy.arange(0,1400,dtype=float)<br>start = time.clock()<br>for x in range(100):<br>&nbsp;&nbsp;&nbsp;&nbsp;npn = numpy_nmean_conv_nl(t,50)<br>print &quot;numpy convolve noloop took: %f sec.&quot;%(time.clock
() - start)<br><br>numpy.testing.assert_equal(reg,npm)<br>numpy.testing.assert_equal(reg,npc)<br>numpy.testing.assert_equal(reg,npn)<br><br>On 7/29/06, David Grant &lt;<a href="mailto:davidgrant@gmail.com">davidgrant@gmail.com
</a>&gt; wrote:<br>&gt;<br>&gt;<br>&gt;<br>&gt; On 7/29/06, Charles R Harris &lt;<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>&gt; wrote:<br>&gt; &gt;<br>&gt; &gt; Hmmm,<br>&gt; &gt;<br>&gt; &gt; I rewrote the subroutine a bit.
<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; def numpy_nmean(list,n):<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; a = numpy.empty(len(list),dtype=float)<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; b = numpy.cumsum(list)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; for i in range(0,len(list)):<br>
&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i &lt; n :<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a[i] = b[i]/(i+1)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else :<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a[i] = (b[i] - b[i-n])/(i+1)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; return a<br>&gt; &gt;<br>&gt; &gt; and got<br>&gt; &gt;
<br>&gt; &gt; regular python took: 0.750000 sec.<br>&gt; &gt; numpy took: 0.380000 sec.<br>&gt;<br>&gt;<br>&gt; I got rid of the for loop entirely. Usually this is the thing to do, at<br>&gt; least this will always give speedups in Matlab and also in my limited
<br>&gt; experience with Numpy/Numeric:<br>&gt;<br>&gt;&nbsp;&nbsp;def numpy_nmean2(list,n):<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; a = numpy.empty(len(list),dtype=float)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; b = numpy.cumsum(list)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = concatenate((b[n:],b[:n]))<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; a[:n] = b[:n]/(i+1)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[n:] = (b[n:] - c[n:])/(i+1)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; return a<br>&gt;<br>&gt; I got no noticeable speedup from doing this which I thought was pretty<br>&gt; amazing. I even profiled all the functions, the original, the one written by
<br>&gt; Charles, and mine, using hotspot just to make sure nothing funny was going<br>&gt; on. I guess plain old Python can be better than you'd expect in certain<br>&gt; situtations.<br>&gt;<br>&gt; --<br>&gt; David Grant
<br><br>-------------------------------------------------------------------------<br>Take Surveys. Earn Cash. Influence the Future of IT<br>Join SourceForge.net's Techsay panel and you'll get the chance to share your<br>opinions on IT &amp; business topics through brief surveys -- and earn cash
<br><a href="http://www.techsay.com/default.php?page=join.php&amp;p=sourceforge&amp;CID=DEVDEV">http://www.techsay.com/default.php?page=join.php&amp;p=sourceforge&amp;CID=DEVDEV</a><br>_______________________________________________
<br>Numpy-discussion mailing list<br><a href="mailto:Numpy-discussion@lists.sourceforge.net">Numpy-discussion@lists.sourceforge.net</a><br><a href="https://lists.sourceforge.net/lists/listinfo/numpy-discussion">https://lists.sourceforge.net/lists/listinfo/numpy-discussion
</a><br></blockquote></div><br>