In this article, we are going to see how arraying joining works.
Joining simply means combining two or more objects. As we have seen in the previous articles, we deal with a variety of multidimensional arrays in python. In joining the contents two arrays, we use the concept of joining. In most cases, we try joining arrays with the SQL with the help of primary key, but in NumPy with the help axis. And also, with the help of functions that will be examined very soon. Below is a brief content of what we are going see in this article:
- Joining using concatenate function
- Joining using stack function
- Row stacking and column stacking
- Height stacking
Joining two or more arrays using the concatenate form
We used this function in joining two completely two completely different arrays and this operation takes place along their along axis. And if we don’t specify the axis, the compiler considers the axis as 0.
Example
import numpy as np
arr1 = np.array([[2, 4]])arr2 = np.array([[6, 8]])arr = np.concatenate((arr1, arr2), axis=1)print(arr)
NumPy instructs the catenate function to join the two in arrays with respect to the specified axis., which in our case is 1.
OUTPUT
import numpy as np
arr1 = np.array([[2, 4], [6, 8]])arr2 = np.array([[10, 12], [14, 16]])arr = np.concatenate((arr1, arr2), axis=1)print(arr)
Just like in the 1-D array, NumPy also instructs the catenate function to join the 2-D array, while respecting the specified axis, which is still one.
OUTPUT
This simply means putting one array over the other. With this method, we are going to specify a new axis in order to join two arrays. This is no difference from catenating, the only difference comes in with the existence of the new axis.
import numpy as np
arr1= np.array([2, 4, 6])arr2 = np.array([8, 10, 12])arr = np.stack((arr1, arr2), axis=1)print(arr)
Here we are trying to stack along the axis one.
In the NumPy package, we have function called hstack () that helps us to stack along rows.
Example
import numpy as np
arr1 = np.array([4, 5, 3])arr2 = np.array([2, 7, 6])arr = np.hstack((arr1, arr2))print(arr)
We are just trying to stack rows without specifying any number of axis for the row.
OUTPUT
Example
import numpy as np
arr1 = np.array(['a', 'b', 'c'])arr2 = np.array(['f', 'e', 'f'])arr = np.vstack((arr1, arr2))print(arr)
In this program, we are just stacking up the arrays along the column.
OUTPUT
Also known as depth, we can use the dstack() function to stack along arrays with respect to height.
Example
import numpy as np
arr1 = np.array(['a', 'b', 'c'])arr2 = np.array(['f', 'e', 'f'])arr = np.dstack((arr1, arr2))print(arr)
OUTPUT
Comments
Post a Comment
Please do not enter any spam link in the comment box.