[Numpy-discussion] concatenating 1-D arrays to 2D
Stefan van der Walt
stefan at sun.ac.za
Fri Mar 23 12:23:53 EDT 2007
On Fri, Mar 23, 2007 at 11:09:03AM -0400, Robert Pyle wrote:
> > In [65]:concatenate((a.reshape(10,1),b.reshape(10,1)),axis=1)
> > Out[65]:
> > array([[ 0, -10],
> > [ 1, -9],
> > [ 2, -8],
> > [ 3, -7],
> > [ 4, -6],
> > [ 5, -5],
> > [ 6, -4],
> > [ 7, -3],
> > [ 8, -2],
> > [ 9, -1]])
> >
> >
> > ?
> >
> > I thought there would be an easier way. Did I overlook something?
>
> What's wrong with zip? Or did *I* miss the point? (I'm just getting
> the hang of numpy.)
If you use 'zip' you don't make use of numpy's fast array mechanisms.
I attach some code you can run as a benchmark. From my ipython
session:
In [1]: run vsbench.py
In [2]: timeit using_vstack(x,y)
1000 loops, best of 3: 997 µs per loop
In [3]: timeit using_zip(x,y)
10 loops, best of 3: 503 ms per loop
In [4]: timeit using_custom_iteration(x,y)
1000 loops, best of 3: 1.64 ms per loop
Cheers
Stéfan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vsbench.py
Type: text/x-python
Size: 492 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070323/1e6d839d/attachment.py>
More information about the NumPy-Discussion
mailing list