Skip to content Skip to sidebar Skip to footer

How To Append Numpy Arrays?

I want append numpy arrays like below A: [[1,2,3],[2,3,1],[1,4,2]] B: [[1,3,3],[3,3,1],[1,4,5]] A+B = [[[1,2,3],[2,3,1],[1,4,2]], [[1,3,3],[3,3,1],[1,4,5]]] How can I do

Solution 1:

One way would be to use np.vstack on 3D extended versions of those arrays -

np.vstack((A[None],B[None]))

Another way with np.row_stack (functionally same as np.vstack) -

np.row_stack((A[None],B[None]))

And similarly with np.concatenate -

np.concatenate((A[None],B[None])) # By default stacks along axis=0

Another way would be with np.stack and specifying the axis of stacking i.e. axis=0 or skip it as that's the default axis of stacking -

np.stack((A,B))

Post a Comment for "How To Append Numpy Arrays?"