简体   繁体   中英

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?

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:

>>> 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 .

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:

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

because np.repeat(X, [2,2], axis=0) needs shape (2,3) , eg

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.

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) .

In my opinion your use of np.repeat is the easiest and best way to achieve your output.


Edit: Hopefully the answer is now more clearly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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