gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:20 26 4
gpt4 key购买 nike

我正在 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,)

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

我该怎么做?

最佳答案

您可以使用 np.vstack -

np.vstack(counts_array)

另一种方式 np.concatenate -

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

sample 运行-

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]])

关于python - 如何从数组数组中创建一个 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40402545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com