Issue
I'll use a simple 2D array with shape (4,4) as an example:
array([[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]])
I want to convert this to a 3D array, so that the values a duplicated along the z-axis, as such:
So that the resulting array has a shape (4,4,3)
It seems really simple, but I can't seem to think of any way to do this.
Edit: I tried np.tile
from the answers below, however I would like the output to be this:
array([[[0, 0, 0],
[2, 2, 2],
[6, 6, 6],
[3, 3, 3]],
[[3, 3, 3],
[7, 7, 7],
[3, 3, 3],
[9, 9, 9]],
[[0, 0, 0],
[8, 8, 8],
[3, 3, 3],
[4, 4, 4]],
[[4, 4, 4],
[6, 6, 6],
[2, 2, 2],
[1, 1, 1]]])
I tried changing which axis is duplicated and reshaping, although it doesn't work.
Solution
You can use numpy.tile
for this
>>> import numpy as np
>>> data = np.array([[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]])
>>> np.tile(data, (3,1,1))
array([[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]],
[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]],
[[0, 2, 6, 3],
[3, 7, 3, 9],
[0, 8, 3, 4],
[4, 6, 2, 1]]])
Answered By - Cory Kramer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.