From: Christopher B. <Chr...@no...> - 2006-06-01 18:32:16
|
I want to take two (2,) arrays and put them together into one (2,2) array. I thought one of these would work: >>> N.concatenate(((1,2),(3,4)),0) array([1, 2, 3, 4]) >>> N.concatenate(((1,2),(3,4)),1) array([1, 2, 3, 4]) Is this the best I can do? >>> N.concatenate(((1,2),(3,4))).reshape(2,2) array([[1, 2], [3, 4]]) Is it because the arrays I'm putting together are rank-1? >>> N.__version__ '0.9.6' -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Robert K. <rob...@gm...> - 2006-06-01 18:42:22
|
Christopher Barker wrote: > I want to take two (2,) arrays and put them together into one (2,2) > array. I thought one of these would work: > >>>> N.concatenate(((1,2),(3,4)),0) > array([1, 2, 3, 4]) >>>> N.concatenate(((1,2),(3,4)),1) > array([1, 2, 3, 4]) > > Is this the best I can do? > >>>> N.concatenate(((1,2),(3,4))).reshape(2,2) > array([[1, 2], > [3, 4]]) > > Is it because the arrays I'm putting together are rank-1? Yes. Look at vstack() (and also its friends hstack(), dstack() and column_stack() for completeness). -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |
From: Christopher B. <Chr...@no...> - 2006-06-01 19:13:47
|
Thanks all, Robert Kern wrote: > Look at vstack() (and also its friends hstack(), dstack() and column_stack() for > completeness). I like this, but need to keep Numeric/numarray compatibility for the moment -- I think, I've just sent out a query to my users. Tim Hochberg wrote: > If you are using real arrays, use newaxis: > > >>> a > array([0, 1, 2]) > >>> b > array([3, 4, 5]) > >>> concatenate([a[newaxis], b[newaxis]], 0) > array([[0, 1, 2], > [3, 4, 5]]) I like this, but again, not in Numeric -- I really need to dump that as soon as I can! > hate newaxis, wrap the arrays in [] to give them an extra dimension. > This tends to look nicer, but I suspect has poorer performance than > above (haven't timed it though): > > >>> concatenate([[a], [b]], 0) > array([[0, 1, 2], > [3, 4, 5]]) Lovely. much cleaner. By they way, wouldn't wrapping in a tuple, be slightly better, performance-wise (I know, probably negligible, but I always feel that I should use a tuple when I don't need mutability) -thanks, -chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Tim H. <tim...@co...> - 2006-06-01 19:58:50
|
Christopher Barker wrote: > Thanks all, > > > Robert Kern wrote: > >> Look at vstack() (and also its friends hstack(), dstack() and >> column_stack() for >> completeness). > > > I like this, but need to keep Numeric/numarray compatibility for the > moment -- I think, I've just sent out a query to my users. > > > > Tim Hochberg wrote: > >> If you are using real arrays, use newaxis: >> >> >>> a >> array([0, 1, 2]) >> >>> b >> array([3, 4, 5]) >> >>> concatenate([a[newaxis], b[newaxis]], 0) >> array([[0, 1, 2], >> [3, 4, 5]]) > > > I like this, but again, not in Numeric -- I really need to dump that > as soon as I can! In Numeric, you can use NewAxis instead for the same effect. > >> hate newaxis, wrap the arrays in [] to give them an extra dimension. >> This tends to look nicer, but I suspect has poorer performance than >> above (haven't timed it though): >> >> >>> concatenate([[a], [b]], 0) >> array([[0, 1, 2], >> [3, 4, 5]]) > > > Lovely. much cleaner. > > By they way, wouldn't wrapping in a tuple, be slightly better, > performance-wise (I know, probably negligible, but I always feel that > I should use a tuple when I don't need mutability) I doubt it would make a signifigant difference and the square brackets are much easier to read IMO. Your mileage may vary. -tim |
From: Alexandre F. <ale...@lo...> - 2006-06-01 18:44:47
|
On Thu, Jun 01, 2006 at 11:32:06AM -0700, Christopher Barker wrote: > I want to take two (2,) arrays and put them together into one (2,2)=20 > array. I thought one of these would work: >=20 > >>> N.concatenate(((1,2),(3,4)),0) > array([1, 2, 3, 4]) > >>> N.concatenate(((1,2),(3,4)),1) > array([1, 2, 3, 4]) >=20 > Is this the best I can do? >=20 > >>> N.concatenate(((1,2),(3,4))).reshape(2,2) > array([[1, 2], > [3, 4]]) >=20 > Is it because the arrays I'm putting together are rank-1? concatenate is not meant to do that. Try putting your arrays in a list and building an array from that list.=20 a1 =3D array([1,2]) a2 =3D array([3,4]) print array([a1, a2]) /bin/bash: q: command not found --=20 Alexandre Fayolle LOGILAB, Paris (France) Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations D=E9veloppement logiciel sur mesure: http://www.logilab.fr/services Informatique scientifique: http://www.logilab.fr/science |
From: Alan G I. <ai...@am...> - 2006-06-01 18:44:54
|
On Thu, 01 Jun 2006, Christopher Barker apparently wrote:=20 > Is this the best I can do?=20 > >>> N.concatenate(((1,2),(3,4))).reshape(2,2)=20 > array([[1, 2],=20 > [3, 4]])=20 >>> import numpy as N >>> N.vstack([(1,2),(3,4)]) array([[1, 2], [3, 4]]) hth, Alan Isaac |
From: Tim H. <tim...@co...> - 2006-06-01 18:46:58
|
Christopher Barker wrote: > I want to take two (2,) arrays and put them together into one (2,2) > array. I thought one of these would work: > > >>> N.concatenate(((1,2),(3,4)),0) > array([1, 2, 3, 4]) > >>> N.concatenate(((1,2),(3,4)),1) > array([1, 2, 3, 4]) > > Is this the best I can do? > > >>> N.concatenate(((1,2),(3,4))).reshape(2,2) > array([[1, 2], > [3, 4]]) > > Is it because the arrays I'm putting together are rank-1? Yes. You need to add a dimension somehow. There are (at least) two ways to do this. If you are using real arrays, use newaxis: >>> a array([0, 1, 2]) >>> b array([3, 4, 5]) >>> concatenate([a[newaxis], b[newaxis]], 0) array([[0, 1, 2], [3, 4, 5]]) Alternatively, if you don't know that 'a' and 'b' are arrays or you just hate newaxis, wrap the arrays in [] to give them an extra dimension. This tends to look nicer, but I suspect has poorer performance than above (haven't timed it though): >>> concatenate([[a], [b]], 0) array([[0, 1, 2], [3, 4, 5]]) -tim > > >>> N.__version__ > '0.9.6' > > -Chris > > > > > |