简体   繁体   English

一次在多个维度上重复NumPy数组?

[英]Repeat a NumPy array in multiple dimensions at once?

np.repeat(np.repeat([[1, 2, 3]], 3, axis=0), 3, axis=1)

works as expected and produces 按预期工作并产生

array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3]])

However, 然而,

np.repeat([[1, 2, 3]], [3, 3])

and

np.repeat([[1, 2, 3]], [3, 3], axis=0)

produce errors. 产生错误。

Is it possible to repeat an array in multiple dimensions at once? 是否可以一次在多个维度上repeat数组?

First off, I think the original method you propose is totally fine. 首先,我认为您建议的原始方法完全可以。 It's readable, it makes sense, and it's not very slow. 它是可读的,有道理,并且不是很慢。

You could use the repeat method instead of function which reads a bit more nicely: 您可以使用repeat 方法代替函数,该函数的读取效果更好:

>>> x.repeat(3, 1).repeat(3, 0)
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3]])

With numpy's broadcasting rules, there's likely dozens of ways to create the repeated data and throw it around into the shape you want, too. 使用numpy的广播规则,可能会有数十种方法来创建重复的数据并将其放置到所需的形状中。 One approach could be to use np.broadcast_to() and repeat the data in D+1 dimensions, where D is the dimension you need, and then collapse it down to D . 一种方法是使用np.broadcast_to()并以D+1维重复数据,其中D是您需要的维,然后将其折叠为D

For example: 例如:

>>> x = np.array([[1, 2, 3]])
>>> np.broadcast_to(x.T, (3, 3, 3)).reshape((3, 9))
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3]])

And without reshaping (so that you don't need to know the final length): 而且无需重塑(这样您就不必知道最终长度):

>>> np.hstack(np.broadcast_to(x, (3, 3, 3)).T)
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3]])

And there's likely a dozen other ways to do this. 可能还有许多其他方法可以做到这一点。 But I still think your original version is more idiomatic, as throwing it into extra dimensions to collapse it down is weird. 但是我仍然认为您的原始版本更惯用,因为将其投入额外的维度以使其折叠起来很奇怪。

It isn't possible, see repeat . 这是不可能的,请参见重复 But you are using a array with the shape (1,3) , so you have to use: 但是您正在使用形状为(1,3)的数组,因此必须使用:

np.repeat(X, [2], axis=0)

because np.repeat(X, [2,2], axis=0) needs shape (2,3) , eg 因为np.repeat(X, [2,2], axis=0)需要形状(2,3) ,例如

X = np.array([[1, 2, 3], [5, 6, 7]])
np.repeat(X, [2, 5], axis=0)

the output looks like: 输出如下:

[[1 2 3]
 [1 2 3]
 [5 6 7]
 [5 6 7]
 [5 6 7]
 [5 6 7]]

This means [2,5] stands for [ 2 , 5]: 2x first row and [2, 5 ]: 5x second row (shape: (2, *doesn't matter*) because axis=0 means you want to repeat the rows. Therefore you first have to generate an array with the dimensions (3, *) , and then produce the next array. 这意味着[2,5]表示[2,5]: 2x first row[2,5]: 5x second row (形状: (2, *doesn't matter*)因为axis=0意味着要重复因此,您首先必须生成一个尺寸为(3, *)的数组,然后生成下一个数组。

If you want to repeat your array: 如果要重复数组:

np.repeat(X2, [5], axis=0)

produces: 生产:

[[1 2 3]
 [1 2 3]
 [1 2 3]
 [1 2 3]
 [1 2 3]]

because you have only a 1-dimensional array. 因为您只有一维数组。

The first call of np.repeat produces a 2D-array, the second call duplicates the columns. 第一次调用np.repeat产生2D数组,第二次调用复制列。 If you want to use np.repeat(X2, [5], axis=0) you get the same result as you have mentioned in your post above, because you have to call np.repeat a second time on the output of np.repeat(X2, [5], axis=0) . 如果要使用np.repeat(X2, [5], axis=0)则会得到与您在上面的文章中提到的结果相同的结果,因为您必须在np.repeat(X2, [5], axis=0)的输出np.repeat二次调用np.repeat np.repeat(X2, [5], axis=0)

In my opinion your use of np.repeat is the easiest and best way to achieve your output. 我认为您对np.repeat的使用是实现输出的最简单,最佳方法。


Edit: Hopefully the answer is now more clearly 编辑:希望答案现在更加清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM