NumPy offers a stack of arrays. Stacking means joining the same dimensional arrays along with a new axis. Stacking can be done horizontally, vertically, column-wise, row-wise, or depth-wise:
- Horizontal stacking: In horizontal stacking, the same dimensional arrays are joined along with a horizontal axis using the hstack() and concatenate() functions. Let's see the following example:
arr1 = np.arange(1,10).reshape(3,3)
print(arr1)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
We have created one 3*3 array; it's time to create another 3*3 array:
arr2 = 2*arr1
print(arr2)
Output:
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]
After creating two arrays, we will perform horizontal stacking:
# Horizontal Stacking
arr3=np.hstack((arr1, arr2))
print(arr3)
Output:
[[ 1 2 3 2 4 6]
[ 4 5 6 8 10 12]
[ 7 8 9 14 16 18]]
In the preceding code, two arrays are stacked horizontally along the x axis. The concatenate() function can also be used to generate the horizontal stacking with axis parameter...