Q:

Construct 3 dimensions array from two dimensions array using tile

I have an array arr = np.array([0.0, 0.0, 1.0])
I want to contract array of following shape promo_class.shape which is (2,3,3)
I want to create repeated array of shape (2,3,3)
array([[[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]],
       [[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]]])

Any idea how to do that with  np.tile function ?

A:

Simply use np.broadcast_to for a view -
In [142]: arr = np.array([0.0, 0.0, 1.0])

In [144]: np.broadcast_to(arr,(2,3,3))
Out[144]: 
array([[[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]],

       [[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]]])

Why should we use view?
Because being a view, it has no extra memory overhead and hence virtually free on runtime -
In [148]: arr = np.random.rand(300)

In [149]: %timeit np.broadcast_to(arr,(200,300,300))
100000 loops, best of 3: 3.13 µs per loop

If you need an output with its own memory space, append with .copy().

If you are devoted to np.tile -
In [174]: np.tile(arr,(2,3,1))
Out[174]: 
array([[[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]],

       [[0., 0., 1.],
        [0., 0., 1.],
        [0., 0., 1.]]])

