重复 NumPy 数组而不复制数据?

发布于 2024-10-30 03:23:44 字数 115 浏览 5 评论 0原文

我想创建一个 1D NumPy 数组,该数组由另一个 1D 数组的 1000 次连续重复组成,而不需要复制数据 1000 次。

是否可以?

如果有帮助,我打算将这两个数组视为不可变的。

I'd like to create a 1D NumPy array that would consist of 1000 back-to-back repetitions of another 1D array, without replicating the data 1000 times.

Is it possible?

If it helps, I intend to treat both arrays as immutable.

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

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

发布评论

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

评论(5

初熏 2024-11-06 03:23:44

你不能这样做; NumPy 数组必须沿每个维度具有一致的步幅,而您的步幅大多数时候需要朝一个方向走,但有时会向后跳。

您可以获得的最接近的是 1000 行二维数组,其中每一行都是第一个数组的视图,或者 flatiter 对象,其行为有点像一维数组。 (Flatiters 支持迭代和索引,但您无法获取它们的视图;所有索引都会生成副本。)

设置:

import numpy as np
a = np.arange(10)

2D 视图:

b = np.lib.stride_tricks.as_strided(a, (1000, a.size), (0, a.itemsize))

Flatiter 对象:

c = b.flat

You can't do this; a NumPy array must have a consistent stride along each dimension, while your strides would need to go one way most of the time but sometimes jump backwards.

The closest you can get is either a 1000-row 2D array where every row is a view of your first array, or a flatiter object, which behaves kind of like a 1D array. (flatiters support iteration and indexing, but you can't take views of them; all indexing makes a copy.)

Setup:

import numpy as np
a = np.arange(10)

2D view:

b = np.lib.stride_tricks.as_strided(a, (1000, a.size), (0, a.itemsize))

flatiter object:

c = b.flat
北方。的韩爷 2024-11-06 03:23:44

broadcast_to 已在 numpy 1.10 中添加,这可以让您以更少的努力有效地重复数组。

复制已接受答案的样式:

import numpy as np
arr = np.arange(10)
repeated = np.broadcast_to(arr, (1000, arr.size))

broadcast_to was added in numpy 1.10, which allows you effectively repeat an array with a little less effort.

Copying the style of the accepted answer:

import numpy as np
arr = np.arange(10)
repeated = np.broadcast_to(arr, (1000, arr.size))
红ご颜醉 2024-11-06 03:23:44

我不是 100% 确定“不复制数据 1000 次”是什么意思。如果您正在寻找一种 numpy 方法来从 a 一次性构建 b (而不是循环),您可以使用:

a = np.arange(1000)
b = np.tile(a,1000)

否则,我会执行以下操作:

a = np.arange(1000)
ii = [700,2000,10000] # The indices you want of the tiled array
b = a[np.mod(ii,a.size)]

<在这种情况下,code>b 不是 a 的视图,因为它有精美的索引(它创建了一个副本),但至少它返回一个 numpy 数组并且不会创建 1000 *内存中的 1000x1 数组,仅包含您想要的元素。

至于它们是不可变的(请参阅 不可变的 numpy 数组?),您需要单独切换每个标志,因为副本不保留标志设置。

I'm not 100% sure what you mean by 'not replicating the data 1000 times'. If you are looking for a numpy method to build b from a in one fell swoop (rather than looping), you can use:

a = np.arange(1000)
b = np.tile(a,1000)

Otherwise, I would do something like:

a = np.arange(1000)
ii = [700,2000,10000] # The indices you want of the tiled array
b = a[np.mod(ii,a.size)]

b is not a view of a in this case because of the fancy indexing (it makes a copy), but at least it returns a numpy array and doesn't create the 1000*1000x1 array in memory and just contains the elements you want.

As far as them being immutable (see Immutable numpy array?), you would need to switch the flag for each separately since copies don't retain the flag setting.

握住你手 2024-11-06 03:23:44

我并不认为这是最优雅的解决方案,因为您必须欺骗 numpy 创建对象数组(请参阅带有注释的行)

from numpy import array

n = 3

a = array([1,2])
a.setflags(write=False)
t = [a]*n + [array([1])] # Append spurious array that is not len(a)
r = array(t,dtype=object)
r.setflags(write=False)

assert id(a) == id(t[1]) == id(r[1])

I do not claim that this is the most elegant solution, because you have to fool numpy into creating an array of objects (see the line with the comment)

from numpy import array

n = 3

a = array([1,2])
a.setflags(write=False)
t = [a]*n + [array([1])] # Append spurious array that is not len(a)
r = array(t,dtype=object)
r.setflags(write=False)

assert id(a) == id(t[1]) == id(r[1])
夜灵血窟げ 2024-11-06 03:23:44

这行得通吗:

import numpy
a = numpy.array([1, 2, 3, 4])
b = numpy.ones((1000, a.shape[0]))
b *= a
b = b.flatten()

Would this work:

import numpy
a = numpy.array([1, 2, 3, 4])
b = numpy.ones((1000, a.shape[0]))
b *= a
b = b.flatten()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文