tags:

views:

210

answers:

2

during preparing data for numpy calculate ,i curious about way to contruct

myarray.shape => (2,18,18)

from

d1.shape => (18,18)
d2.shape => (18,18)

i try to use numpy command

hstack([[d1],[d2]])

but it looks not work!!

+3  A: 

Just doing d3 = array([d1,d2]) seems to work for me:

>>> from numpy import array
>>> # ... create d1 and d2 ...
>>> d1.shape
(18,18)
>>> d2.shape
(18,18)
>>> d3 = array([d1, d2])
>>> d3.shape
(2, 18, 18)
Daniel G
oh its work, thank Daniel :)
vernomcrp
+1  A: 

hstack and vstack do no change the number of dimensions of the arrays: they merely put them "side by side". Thus, combining 2-dimensional arrays creates a new 2-dimensional array (not a 3D one!).

You can do what Daniel suggested.

You can alternatively convert your arrays to 3D arrays before stacking them, by adding a new dimension to each array:

d3 = vstack([ d1[newaxis,...], d2[newaxis,...] ])  # shape = (2, 18, 18)

In fact, d1[newaxis,...].shape == (1, 18, 18), and you can stack both 3D arrays directly and get the new 3D array (d3) that you wanted.

EOL
:) thank EOL , now i 'll know more about vstack,hstack
vernomcrp