NumPy | dstack method
Start your free 7-days trial now!
dstack(~) method is used to concatenate arrays along the third axis. Check the examples below for clarification.
The arrays that will concatenated along the third axis.
A Numpy array with the provided arrays concatenated along the third axis.
Concatenating 1D arrays
x = np.array([1,2,3])y = np.array([4,5,6])z = np.dstack([x,y])zarray([[[1, 4],[2, 5],[3, 6]]])
Note that you aren't limited to concatenating just two arrays at once - you can pass in as many as you wish. Also, the returned Numpy array here is three-dimensional.
Concatenating 2D arrays
x = np.array([[1,2],[3,4]])y = np.array([[5,6],[7,8]])z = np.dstack([x,y])zarray([[[1, 5],[2, 6]],[[3, 7],[4, 8]]])
Just to make it easier to process this, here's
x = [[1, 2], y = [[5, 6][3, 4]] [7, 8]]