# [Numpy-discussion] Adding a 2D with a 1D array...

Thu Sep 10 04:43:44 CDT 2009

```OK. Thanks everybody :D
But...what is happening now? When executing this code:

print ' .....  object parameters mutation .....'
print 'np.shape(offspr)', np.shape(offspr)
print 'np.shape(offspr[0])', np.shape(offspr[0])
print "np.shape(r)", np.shape(r)
print "np.shape(offspr_sigma)", np.shape(offspr_sigma)
a = offspr_sigma * np.random.normal(0, 1, shp_sigma)
print "np.shape(a)", np.shape(a)
t4 = clock()
offspr[...] = r
offspr += a[:,None]
t5 = clock() - t4
print "Pythonic time (no array creation) ==> %.8f seconds" % t5
t2 = clock()
offspr = r + a[:,None]
t3 = clock() - t2
print "Pythonic time ==> %.8f seconds" % t3
t = clock()
for i in range(lambd):
offspr[i] = r[i] + a[i]
t1 = clock() - t
print "For loop time ==> %.8f seconds" % t1

what I get is

.....  object parameters mutation .....
np.shape(offspr) (80000, 26)
np.shape(offspr[0]) (26,)
np.shape(r) (80000, 26)
np.shape(offspr_sigma) (80000,)
np.shape(a) (80000,)
Traceback (most recent call last):
<module>
main()
main
mutate_strat, tau_global, tau_params)
mutate
offspr[...] = r
TypeError: list indices must be integers

WTF?

On 9/10/09, Francesc Alted <faltet@pytables.org> wrote:
>
>  A Wednesday 09 September 2009 20:17:20 Dag Sverre Seljebotn escrigué:
>
>
> > > Your results are what I expected...but. This code is called from my
> main
>
> > > program, and what I have in there (output array already created for
> both
>
> > > cases) is:
>
> > >
>
> > > print "lambd", lambd
>
> > > print "np.shape(a)", np.shape(a)
>
> > > print "np.shape(r)", np.shape(r)
>
> > > print "np.shape(offspr)", np.shape(offspr)
>
> > > t = clock()
>
> > > for i in range(lambd):
>
> > > offspr[i] = r[i] + a[i]
>
> > > t1 = clock() - t
>
> > > print "For loop time ==> %.8f seconds" % t1
>
> > > t2 = clock()
>
> > > offspr = r + a[:,None]
>
> > > t3 = clock() - t2
>
> > > print "Pythonic time ==> %.8f seconds" % t3
>
> > >
>
> > > The results I obtain are:
>
> > >
>
> > > lambd 80000
>
> > > np.shape(a) (80000,)
>
> > > np.shape(r) (80000, 26)
>
> > > np.shape(offspr) (80000, 26)
>
> > > For loop time ==> 0.34528804 seconds
>
> > > Pythonic time ==> 0.35956192 seconds
>
> > >
>
> > > Maybe I'm not measuring properly, so, how should I do it?
>
> >
>
> > Like Luca said, you are not including the creation time of offspr in the
>
> > for-loop version. A fairer comparison would be
>
> >
>
> > offspr[...] = r + a[:, None]
>
> >
>
> > Even fairer (one less temporary copy):
>
> >
>
> > offspr[...] = r
>
> > offspr += a[:, None]
>
> >
>
> > Of course, see how the trend is for larger N as well.
>
> >
>
> > Also your timings are a bit crude (though this depends on how many times
>
> > you ran your script to check :-)). To get better measurements, use the
>
> > timeit module, or (easier) IPython and the %timeit command.
>
> Oh well, the art of benchmarking :)
>
> The timeit module allows you normally get less jitter in timings because it
> loops on doing the same operation repeatedly and get a mean. However, this
> has the drawback of filling your cache with the datasets (or part of them)
> so, in the end, your measurements with timeit does not take into account the
> time to transmit the data in main memory into the CPU caches, and that may
> be not what you want to measure.
>
> In the case of Ruben, I think what he is seeing are cache effects. Maybe if
> he does a loop, he would finally see the difference coming up (although this
> may be not what he want, of course ;-)
>
> --
>
> Francesc Alted
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090910/06a5dee5/attachment-0001.html
```