8

Say I have a 1-dimensional numpy array with shape (5,):

a = np.array(range(0,5))

And I want to transform it two a 2-dimensional array by duplicating the array above 3 times, so that the shape will be (5,3), for example:

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

How would I do that? I know that with lists, you can use list.copy() to create a copy, but I don't want to convert my array to a list first.

1
  • numpy has a vstack function that will do what you're after Commented Nov 26, 2018 at 12:57

2 Answers 2

14

With numpy.tile.

>>> a = np.arange(5)
>>> np.tile(a, (3, 1))
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
Sign up to request clarification or add additional context in comments.

Comments

5

You can use * operator on list.

import numpy as np
arr = np.array(3*[range(0,5)])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.