Hello, I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work: In [47]:a=arange(0,10,1) In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) In [49]:b=arange(-10,0,1) In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]) In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]) In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]) but it never expands the dimensions. Do I have to do this... 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? thanks, Brian Blais -- ----------------- bblais@bryant.edu http://web.bryant.edu/~bblais
Try column_stack,
and also try the "See also" parts of the Numpy Examples List. very
handy for finding things like this.
http://www.scipy.org/Numpy_Example_List
--bb
On 3/23/07, Brian Blais
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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?
thanks,
Brian Blais
-- -----------------
bblais@bryant.edu http://web.bryant.edu/~bblais
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Thu, Mar 22, 2007 at 08:13:22PM -0400, Brian Blais wrote:
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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?
How about N.vstack((a,b)).T Cheers Stéfan
On 3/22/07, Stefan van der Walt
On Thu, Mar 22, 2007 at 08:13:22PM -0400, Brian Blais wrote:
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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?
How about
N.vstack((a,b)).T
Also mentioned here should be the use of newaxis. As in a[:,newaxis] However I never got a "good feel" for how to use it, so I can't complete the code you would need. -Sebastian Haase
Sebastian Haase wrote:
On 3/22/07, Stefan van der Walt
wrote: On Thu, Mar 22, 2007 at 08:13:22PM -0400, Brian Blais wrote:
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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? How about
N.vstack((a,b)).T
Also mentioned here should be the use of newaxis. As in a[:,newaxis]
However I never got a "good feel" for how to use it, so I can't complete the code you would need.
n [9]:c = N.concatenate((a[:,N.newaxis], b[:,N.newaxis]), axis=1) In [10]:c Out[10]: array([[ 0, -10], [ 1, -9], [ 2, -8], [ 3, -7], [ 4, -6], [ 5, -5], [ 6, -4], [ 7, -3], [ 8, -2], [ 9, -1]])
-Sebastian Haase _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On 3/23/07, Eric Firing
Sebastian Haase wrote:
On 3/22/07, Stefan van der Walt
wrote: On Thu, Mar 22, 2007 at 08:13:22PM -0400, Brian Blais wrote:
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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? How about
N.vstack((a,b)).T
Also mentioned here should be the use of newaxis. As in a[:,newaxis]
However I never got a "good feel" for how to use it, so I can't complete the code you would need.
n [9]:c = N.concatenate((a[:,N.newaxis], b[:,N.newaxis]), axis=1)
In [10]:c Out[10]: array([[ 0, -10], [ 1, -9], [ 2, -8], [ 3, -7], [ 4, -6], [ 5, -5], [ 6, -4], [ 7, -3], [ 8, -2], [ 9, -1]])
Then of course, there's r_ and c_: c = numpy.c_[a,b] c = numpy.r_[a[None],b[None]].T --bb
On 3/22/07, Bill Baxter
On 3/23/07, Eric Firing
wrote: Sebastian Haase wrote:
On 3/22/07, Stefan van der Walt
wrote: On Thu, Mar 22, 2007 at 08:13:22PM -0400, Brian Blais wrote:
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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? How about
N.vstack((a,b)).T
Also mentioned here should be the use of newaxis. As in a[:,newaxis]
However I never got a "good feel" for how to use it, so I can't complete the code you would need.
n [9]:c = N.concatenate((a[:,N.newaxis], b[:,N.newaxis]), axis=1)
In [10]:c Out[10]: array([[ 0, -10], [ 1, -9], [ 2, -8], [ 3, -7], [ 4, -6], [ 5, -5], [ 6, -4], [ 7, -3], [ 8, -2], [ 9, -1]])
Then of course, there's r_ and c_:
c = numpy.c_[a,b]
c = numpy.r_[a[None],b[None]].T
--bb So, None is the same as newaxis - right?
But what is a[None] vs. a[:,N.newaxis] ? -Sebastian
On 3/24/07, Sebastian Haase
Then of course, there's r_ and c_:
c = numpy.c_[a,b]
c = numpy.r_[a[None],b[None]].T
--bb
So, None is the same as newaxis - right?
Yes, newaxis is None. None is newaxis. Same thing. I just don't see much advantage in spelling it numpy.newaxis, since it's pretty common and not likely to ever change.
But what is a[None] vs. a[:,N.newaxis] ?
a[None] is the same as a[None,:]. It prepends the new axis, so a shape of (5,) becomes (1,5), a "row vector" a[:,None] puts the new axis after the first axis, so shape of (5,) becomes (5,1) a "column vector" a[None,:,None] puts a new axis both before and after, so (5,) becomes (1,5,1). If 'a' is higher dimensional, the same kind of thing goes. Wherever None/newaxis appears in the index, insert a new axis there in the result. Say A is shape (2,3,4), then A[:,None,:,None] is shape (2,1,3,1,4). (Same as A[:,None,:,None,:] since unspecified trailing indices are always taken to be ':') --bb
On Mar 22, 2007, at 8:13 PM, Brian Blais wrote:
Hello,
I'd like to concatenate a couple of 1D arrays to make it a 2D array, with two columns (one for each of the original 1D arrays). I thought this would work:
In [47]:a=arange(0,10,1)
In [48]:a Out[48]:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:b=arange(-10,0,1)
In [51]:b Out[51]:array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [54]:concatenate((a,b)) Out[54]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
In [55]:concatenate((a,b),axis=1) Out[55]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1])
but it never expands the dimensions. Do I have to do this...
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.) ~ $ python Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type "help", "copyright", "credits" or "license" for more information.
from numpy import * a=arange(0,10,1) a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) b=arange(-10,0,1) b array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]) array(zip(a,b)) array([[ 0, -10], [ 1, -9], [ 2, -8], [ 3, -7], [ 4, -6], [ 5, -5], [ 6, -4], [ 7, -3], [ 8, -2], [ 9, -1]])
Bob Pyle
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
participants (7)
-
Bill Baxter
-
Brian Blais
-
Eric Firing
-
Robert Pyle
-
Sebastian Haase
-
Sebastian Haase
-
Stefan van der Walt