调整 NumPy 数组的大小和拉伸

发布于 2024-10-03 13:18:47 字数 370 浏览 3 评论 0原文

我正在使用 Python 工作,并且有一个像这样的 NumPy 数组:

[1,5,9]
[2,7,3]
[8,4,6]

如何将其拉伸到某个值像下面这样?

[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]

这些只是一些示例数组,我实际上会调整数组的几个大小,而不仅仅是这些。

我对此很陌生,我似乎无法集中精力思考我需要做什么。

I am working in Python and I have a NumPy array like this:

[1,5,9]
[2,7,3]
[8,4,6]

How do I stretch it to something like the following?

[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]

These are just some example arrays, I will actually be resizing several sizes of arrays, not just these.

I'm new at this, and I just can't seem to wrap my head around what I need to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ双果 2024-10-10 13:18:47

@KennyTM 的答案非常巧妙,并且确实适合您的情况,但作为替代方案,可能会为扩展数组提供更大的灵活性,请尝试 np.repeat

>>> a = np.array([[1, 5, 9],
              [2, 7, 3],
              [8, 4, 6]])

>>> np.repeat(a,2, axis=1)
array([[1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6]])

因此,这完成了沿一个轴的重复,要使其沿多个轴(如您可能想要的)重复,只需嵌套np.repeat 调用:

>>> np.repeat(np.repeat(a,2, axis=0), 2, axis=1)
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])

您还可以改变任何初始行或列的重复次数。例如,如果您希望除最后一行之外的每一行重复两次:

>>> np.repeat(a, [2,2,1], axis=0)
array([[1, 5, 9],
       [1, 5, 9],
       [2, 7, 3],
       [2, 7, 3],
       [8, 4, 6]])

这里,当第二个参数是列表时,它指定按行(在本例中为行,因为 axis=0 ) 对每一行重复。

@KennyTM's answer is very slick, and really works for your case but as an alternative that might offer a bit more flexibility for expanding arrays try np.repeat:

>>> a = np.array([[1, 5, 9],
              [2, 7, 3],
              [8, 4, 6]])

>>> np.repeat(a,2, axis=1)
array([[1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6]])

So, this accomplishes repeating along one axis, to get it along multiple axes (as you might want), simply nest the np.repeat calls:

>>> np.repeat(np.repeat(a,2, axis=0), 2, axis=1)
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])

You can also vary the number of repeats for any initial row or column. For example, if you wanted two repeats of each row aside from the last row:

>>> np.repeat(a, [2,2,1], axis=0)
array([[1, 5, 9],
       [1, 5, 9],
       [2, 7, 3],
       [2, 7, 3],
       [8, 4, 6]])

Here when the second argument is a list it specifies a row-wise (rows in this case because axis=0) repeats for each row.

风筝在阴天搁浅。 2024-10-10 13:18:47
>>> a = numpy.array([[1,5,9],[2,7,3],[8,4,6]])
>>> numpy.kron(a, [[1,1],[1,1]])
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])
>>> a = numpy.array([[1,5,9],[2,7,3],[8,4,6]])
>>> numpy.kron(a, [[1,1],[1,1]])
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])
柳絮泡泡 2024-10-10 13:18:47

不幸的是 numpy 不允许分数步(据我所知)。这是一个解决方法。它不像 Kenny 的解决方案那么聪明,但它利用了传统的索引:

>>> a = numpy.array([[1,5,9],[2,7,3],[8,4,6]])
>>> step = .5
>>> xstop, ystop = a.shape
>>> x = numpy.arange(0,xstop,step).astype(int)
>>> y = numpy.arange(0,ystop,step).astype(int)
>>> mg = numpy.meshgrid(x,y)
>>> b = a[mg].T
>>> b
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])

(dtlussier 的解决方案更好)

Unfortunately numpy does not allow fractional steps (as far as I am aware). Here is a workaround. It's not as clever as Kenny's solution, but it makes use of traditional indexing:

>>> a = numpy.array([[1,5,9],[2,7,3],[8,4,6]])
>>> step = .5
>>> xstop, ystop = a.shape
>>> x = numpy.arange(0,xstop,step).astype(int)
>>> y = numpy.arange(0,ystop,step).astype(int)
>>> mg = numpy.meshgrid(x,y)
>>> b = a[mg].T
>>> b
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])

(dtlussier's solution is better)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文