Stacking and Concatenating Numpy Arrays
Stacking ndarrays
Program
import numpy as np
ary1 = np.array([1,2,3,4])
ary2 = np.array([5,6,7,8])
ary3 = np.array([9,10,11,12])
print("Arrays stacked row-wise(axis = 0):")
print(np.stack((ary1,ary2,ary3),axis=0))
print("Arrays stacked column-wise(axis = 1):")
print(np.stack((ary1,ary2,ary3),axis=1))
ary1 = np.array([1,2,3,4])
ary2 = np.array([5,6,7,8])
ary3 = np.array([9,10,11,12])
print("Arrays stacked row-wise(axis = 0):")
print(np.stack((ary1,ary2,ary3),axis=0))
print("Arrays stacked column-wise(axis = 1):")
print(np.stack((ary1,ary2,ary3),axis=1))
Output
Arrays stacked row-wise(axis = 0):
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Arrays stacked column-wise(axis = 1):
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Arrays stacked column-wise(axis = 1):
[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]
Concatenating ndarrays
Program
import numpy as np
ary1 = np.array([[1,2],[3,4]])
ary2 = np.array([[5,6],[7,8]])
ary3 = np.array([[9,10],[11,12]])
print("Arrays concatenating(axis = None):")
print(np.concatenate((ary1,ary2,ary3),axis=None))
print("Arrays concatenating(axis = 0):")
print(np.concatenate((ary1,ary2,ary3),axis=0))
print("Arrays concatenating(axis = 1):")
print(np.concatenate((ary1,ary2,ary3),axis=1))
ary1 = np.array([[1,2],[3,4]])
ary2 = np.array([[5,6],[7,8]])
ary3 = np.array([[9,10],[11,12]])
print("Arrays concatenating(axis = None):")
print(np.concatenate((ary1,ary2,ary3),axis=None))
print("Arrays concatenating(axis = 0):")
print(np.concatenate((ary1,ary2,ary3),axis=0))
print("Arrays concatenating(axis = 1):")
print(np.concatenate((ary1,ary2,ary3),axis=1))
Output
Arrays concatenating(axis = None):
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Arrays concatenating(axis = 0):
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
Arrays concatenating(axis = 1):
[[ 1 2 5 6 9 10]
[ 3 4 7 8 11 12]]
[ 1 2 3 4 5 6 7 8 9 10 11 12]
Arrays concatenating(axis = 0):
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
Arrays concatenating(axis = 1):
[[ 1 2 5 6 9 10]
[ 3 4 7 8 11 12]]