python 如何将行和列添加到 NUMPY 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1909994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:19:35  来源:igfitidea点击:

How do I add rows and columns to a NUMPY array?

pythonarraysnumpyreshape

提问by Thomas Browne

Hello I have a 1000 data series with 1500 points in each.

您好,我有一个 1000 个数据系列,每个系列有 1500 个点。

They form a (1000x1500) size Numpy array created using np.zeros((1500, 1000)) and then filled with the data.

它们形成一个 (1000x1500) 大小的 Numpy 数组,使用 np.zeros((1500, 1000)) 创建,然后填充数据。

Now what if I want the array to grow to say 1600 x 1100? Do I have to add arrays using hstack and vstack or is there a better way?

现在如果我想让数组增长到 1600 x 1100 怎么办?我必须使用 hstack 和 vstack 添加数组还是有更好的方法?

I would want the data already in the 1000x1500 piece of the array not to be changed, only blank data (zeros) added to the bottom and right, basically.

我希望已经在 1000x1500 数组中的数据不被更改,基本上只在底部和右侧添加空白数据(零)。

Thanks.

谢谢。

采纳答案by Eric O Lebigot

If you want zeroes in the added elements, my_array.resize((1600, 1000))should work. Note that this differs from numpy.resize(my_array, (1600, 1000)), in which previous lines are duplicated, which is probably not what you want.

如果您想在添加的元素中使用零,my_array.resize((1600, 1000))应该可以。请注意,这不同于numpy.resize(my_array, (1600, 1000)),其中重复前几行,这可能不是您想要的。

Otherwise (for instance if you want to avoid initializing elements to zero, which could be unnecessary), you can indeed use hstackand vstackto add an array containing the new elements; numpy.concatenate()(see pydoc numpy.concatenate) should work too (it is just more general, as far as I understand).

否则(例如,如果您想避免将元素初始化为零,这可能是不必要的),您确实可以使用 hstackvstack添加一个包含新元素的数组;numpy.concatenate()(请参阅 pydoc numpy.concatenate)也应该有效(据我所知,它只是更通用)。

In either case, I would guess that a new memory block has to be allocated in order to extend the array, and that all these methods take about the same time.

在任何一种情况下,我都会猜测必须分配一个新的内存块才能扩展数组,并且所有这些方法都需要大约相同的时间。

回答by doug

This should do what you want (ie, using 3x3 array and 4x4 array to represent the two arrays in the OP)

这应该做你想做的(,使用 3x3 数组和 4x4 数组来表示 OP 中的两个数组)

>>> import numpy as NP
>>> a = NP.random.randint(0, 10, 9).reshape(3, 3)
>>> a
>>> array([[1, 2, 2],
           [7, 0, 7],
           [0, 3, 0]])

>>> b = NP.zeros((4, 4))

mappinga on to b:

a映射到 b:

>>> b[:3,:3] = a

>>> b
    array([[ 1.,  2.,  2.,  0.],
           [ 7.,  0.,  7.,  0.],
           [ 0.,  3.,  0.,  0.],
           [ 0.,  0.,  0.,  0.]])

回答by dwf

No matter what, you'll be stuck reallocating a chunk of memory, so it doesn't really matter if you use arr.resize(), np.concatenate, hstack/vstack, etc. Note that if you're accumulating a lot of data sequentially, Python lists are usually more efficient.

无论什么时候,你会被卡住重新分配的内存块,所以它并不真正的问题,如果你使用arr.resize()np.concatenatehstack/vstack等需要注意的是,如果你连续积累了大量的数据,Python的名单通常更有效。

回答by Simon

You should use reshape()and/or resize()depending on your precise requirement.

您应该根据您的具体要求使用reshape()和/或resize()

If you want chapter and verse from the authors you are probably better off posting on the numpy discussion board.

如果你想要作者的章节和诗句,你最好在 numpy 讨论板上发帖。