Contruct 3d array in numpy from existing 2d array

前端 未结 3 752
终归单人心
终归单人心 2021-02-18 23:57

During preparing data for NumPy calculate. I am curious about way to construct:

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

from:

d1.shape          


        
3条回答
  •  滥情空心
    2021-02-19 00:30

    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 (directly use numpy.array([d1, d2])).

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

    d3 = numpy.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.

提交回复
热议问题