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?
是否可以一次重复多个维度的数组?
2 个解决方案
#1
2
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. 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
.
使用numpy的广播规则,可能有很多方法可以创建重复数据并将其投入到您想要的形状中。一种方法可以是使用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.
并且可能有十几种其他方法可以做到这一点。但是我仍然认为你的原始版本更具惯用性,因为把它扔到额外的尺寸来折叠它是很奇怪的。
#2
-1
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)
, e.g.
因为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第一行和[2,5]:5x第二行(形状:(2,*无关紧要*)因为轴= 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. 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的第一次调用产生一个2D数组,第二次调用重复列。如果你想使用np.repeat(X2,[5],axis = 0),你会得到与上面帖子中提到的相同的结果,因为你必须在np的输出上第二次调用np.repeat。重复(X2,[5],轴= 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
编辑:希望答案现在更清楚了
#1
2
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. 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
.
使用numpy的广播规则,可能有很多方法可以创建重复数据并将其投入到您想要的形状中。一种方法可以是使用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.
并且可能有十几种其他方法可以做到这一点。但是我仍然认为你的原始版本更具惯用性,因为把它扔到额外的尺寸来折叠它是很奇怪的。
#2
-1
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)
, e.g.
因为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第一行和[2,5]:5x第二行(形状:(2,*无关紧要*)因为轴= 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. 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的第一次调用产生一个2D数组,第二次调用重复列。如果你想使用np.repeat(X2,[5],axis = 0),你会得到与上面帖子中提到的相同的结果,因为你必须在np的输出上第二次调用np.repeat。重复(X2,[5],轴= 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
编辑:希望答案现在更清楚了