What am I missing about concatenate?
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 Chris.Barker@noaa.gov
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
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 Chris.Barker@noaa.gov
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
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) 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?
concatenate is not meant to do that. Try putting your arrays in a list and building an array from that list. a1 = array([1,2]) a2 = array([3,4]) print array([a1, a2]) /bin/bash: q: command not found -- Alexandre Fayolle LOGILAB, Paris (France) Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations Développement logiciel sur mesure: http://www.logilab.fr/services Informatique scientifique: http://www.logilab.fr/science
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
participants (5)
-
Alan G Isaac -
Alexandre Fayolle -
Christopher Barker -
Robert Kern -
Tim Hochberg