5

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.

Tim
  • 187
  • 1
  • 3
  • 9

2 Answers2

10

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]])
timgeb
  • 76,762
  • 20
  • 123
  • 145
2

You can use * operator on list.

import numpy as np
arr = np.array(3*[range(0,5)])
Oli
  • 1,313
  • 14
  • 31