如何从数组数组中创建一个numpy数组?

时间:2023-01-28 12:32:25

I'm experimenting in ipython3, where I created an array of arrays:

我正在ipython3中进行实验,在那里我创建了一个数组数组:

In [105]: counts_array
Out[105]: 
array([array([ 17,  59, 320, ...,   1,   7,   0], dtype=uint32),
       array([ 30,  71, 390, ...,  12,  20,   6], dtype=uint32),
       array([  7, 145, 214, ...,   4,  12,   0], dtype=uint32),
       array([ 23, 346, 381, ...,  15,  19,   5], dtype=uint32),
       array([ 51,  78, 270, ...,   3,   0,   2], dtype=uint32),
       array([212, 149, 511, ...,  19,  31,   8], dtype=uint32)], dtype=object)

In [106]: counts_array.shape
Out[106]: (6,)

In [107]: counts_array[0].shape
Out[107]: (1590,)

I would like to obtain a plain shape=(6, 1590), dtype=uint32 array from this monster I created.

我想从我创建的这个怪物中获得一个普通的形状=(6,1590),dtype = uint32数组。

How can I do that?

我怎样才能做到这一点?

3 个解决方案

#1


4  

You can use np.vstack -

你可以使用np.vstack -

np.vstack(counts_array)

Another way with np.concatenate -

使用np.concatenate的另一种方法 -

np.concatenate(counts_array).reshape(len(counts_array),-1)

Sample run -

样品运行 -

In [23]: a
Out[23]: 
array([array([68, 92, 84, 35, 14, 71, 55, 40, 21, 41]),
       array([30, 90, 52, 64, 86, 68, 61, 85, 26, 98]),
       array([98, 64, 23, 49, 13, 17, 52, 96, 97, 19]),
       array([54, 26, 25, 22, 95, 77, 20, 73, 22, 80]),
       array([15, 84, 91, 54, 25, 21, 37, 19, 25, 25]),
       array([87, 17, 49, 74, 11, 34, 27, 23, 22, 83])], dtype=object)

In [24]: np.vstack(a)
Out[24]: 
array([[68, 92, 84, 35, 14, 71, 55, 40, 21, 41],
       [30, 90, 52, 64, 86, 68, 61, 85, 26, 98],
       [98, 64, 23, 49, 13, 17, 52, 96, 97, 19],
       [54, 26, 25, 22, 95, 77, 20, 73, 22, 80],
       [15, 84, 91, 54, 25, 21, 37, 19, 25, 25],
       [87, 17, 49, 74, 11, 34, 27, 23, 22, 83]])

#2


2  

After various experiments, it turns out that the following simple syntax just works:

经过各种实验,结果证明以下简单语法正常:

numpy.array([sub_array for sub_array in counts_array])

My first working version was unnecessary complicated:

我的第一个工作版本不必要复杂:

numpy.array([[*sub_array] for sub_array in counts_array], dtype=numpy.uint32)

#3


2  

Have you considered numpy.vstack()? I use it very often for this kind of operations.

你考虑过numpy.vstack()吗?我经常使用它来进行这种操作。

#1


4  

You can use np.vstack -

你可以使用np.vstack -

np.vstack(counts_array)

Another way with np.concatenate -

使用np.concatenate的另一种方法 -

np.concatenate(counts_array).reshape(len(counts_array),-1)

Sample run -

样品运行 -

In [23]: a
Out[23]: 
array([array([68, 92, 84, 35, 14, 71, 55, 40, 21, 41]),
       array([30, 90, 52, 64, 86, 68, 61, 85, 26, 98]),
       array([98, 64, 23, 49, 13, 17, 52, 96, 97, 19]),
       array([54, 26, 25, 22, 95, 77, 20, 73, 22, 80]),
       array([15, 84, 91, 54, 25, 21, 37, 19, 25, 25]),
       array([87, 17, 49, 74, 11, 34, 27, 23, 22, 83])], dtype=object)

In [24]: np.vstack(a)
Out[24]: 
array([[68, 92, 84, 35, 14, 71, 55, 40, 21, 41],
       [30, 90, 52, 64, 86, 68, 61, 85, 26, 98],
       [98, 64, 23, 49, 13, 17, 52, 96, 97, 19],
       [54, 26, 25, 22, 95, 77, 20, 73, 22, 80],
       [15, 84, 91, 54, 25, 21, 37, 19, 25, 25],
       [87, 17, 49, 74, 11, 34, 27, 23, 22, 83]])

#2


2  

After various experiments, it turns out that the following simple syntax just works:

经过各种实验,结果证明以下简单语法正常:

numpy.array([sub_array for sub_array in counts_array])

My first working version was unnecessary complicated:

我的第一个工作版本不必要复杂:

numpy.array([[*sub_array] for sub_array in counts_array], dtype=numpy.uint32)

#3


2  

Have you considered numpy.vstack()? I use it very often for this kind of operations.

你考虑过numpy.vstack()吗?我经常使用它来进行这种操作。