8
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?

8
  • Just to be clear, you want a shorter way to produce the output of your first program, right? Commented Sep 29, 2017 at 12:07
  • @HyperNeutrino Yes, a single invocation of np.repeat preferably. You'd think that one of the other examples should work. Commented Sep 29, 2017 at 12:09
  • Is it a performance requirement that you are looking to do so or just shorter code or something else? Commented Sep 29, 2017 at 12:38
  • @Divakar it felt like maybe I'm not using repeat correctly, or the API is warty. Commented Sep 29, 2017 at 12:40
  • 2
    I can produce that result several other ways (one repeat with reshape and transpose, kron, etc), but this double repeat is hands down the speed winner. Commented Sep 29, 2017 at 17:50

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Broadcasting also requires a lot more thought about where the repeated elements will end up compared to just repeating multiple times.
-1

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), e.g.

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

9 Comments

That is really cool, I had no idea you could do that with repeat. Also OP is using an array of shape (1, 3), not (1, 4). This isn't answering OPs question though; it needs to repeat over multiple axes at once.
I don't understand the answer. You say "it's possible", but your example is different from mine: both I and O.
np.repeat([[1, 2, 3]], [3], axis=0) does not produce the desired output
@MaxB: I'm sorry my answer is not completly clear, just give me a few seconds to improve it
@MaxB: np.repeat([[1, 2, 3]], [5], axis=0) will do the trick, but only repeats the rows, not the columns.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.