Introduction of NumPy Concatenate
Numpy.concatenate() function is used in the Python coding language to join two different arrays or more than two arrays into a single array. The concatenate function present in Python allows the user to merge two different arrays either by their column or by the rows. The function is capable of taking two or more arrays that have the shape and it merges these arrays into a single array. The default value for the row wise concatenation of arrays considers the axis as zero. For concatenating two different areas that are not of the same shape they first have to be reshaped using the reshape function to convert a single dimensional array into a two dimensional allowing the final concatenated array to be a 3×3 array, with three rows and three columns.
Syntax and Parameters
Syntax for application of NumPy concatenate
Following is the syntax which used to utilize the numpy concatenate() while writing codes in the Python programming language:
numpy.concatenate((a1, a2, ...), axis)
Parameters:
Following is the parameters used for the numpy.concatenate * () function written in the Python programming language:
- a1, a2, …sequence of array_like: The arrays that have been entered by the user that must be of the same shape (i.e., dimension) except for the consideration of the dimension that is corresponding to the specified axis. In case the axis has not been specified by the user, the default value for the axis remains as the first axis,
- Axis: int, optional: The parameter is responsible for representing the axis along which the error is would be joined with the other arrays. Arrays are flattened before being used if the parameter axis has been specified as None. The default value for the parameter is 0
- Out: ndarray, optional: The parameter enables the program to show the exact destination where the concatenated resultant array has to be placed. This parameter is not an unnecessary addition and can be skipped while writing the command. It is necessary that the shape of the array need to be correct (i.e., matching the shape or dimension of the concatenated resultant that could be returned, in case no out argument has been specified by the user.
Returns:
- Res ndarray: The concatenated resultant array which is formed after the merging of the various arrays that have been specified by the user
How does NumPy Concatenate Work?
Here we discuss the working of NumPy concatenate:
When we use the NumPy function concatenate, the system by default take the areas that have been mentioned by the user and starts them on the first axis. In case the shape of the array is not the same, and error is display because of the axis of each of These areas being different due to which a structural collaboration is not possible. In such a case, either the axis of the array has to be defined as a different axis so that a vertical or horizontal diversity happens on the data.
In case the shape of the areas that are to be concatenated are different there would be an error being displayed when the output is showed by the system. For such instances, the reshape function can be used to create an erase of the same shape for the desired result and shape so that arrays with different shape and dimensions can also be merged. The concatenate function can also the use to merge two arrays in a column wise manner. If the axis is specified as 1 the resultant array is subjected to be merged in a column wise manner producing a wide matrix that has more numbers than the number of columns.
An alternative function that can be used instead of using concatenate is using vstack(), which enables the stacking of arrays in a vertical sequence or row wise manner, producing the default output from the concatenate(). When we want to arrange various single dimensional arrays (more than two) we can use the concatenated function to create a list that contains all the elements that are contained in the various arrays that are being merged.
Examples of NumPy Concatenate
An example displaying the used of numpy.concatenate() in python:
Example #1
Displaying concatenation of arrays with the same shape:
Code:
# Python program explaining the use of NumPy.concatenate function
import numpy as np1
A1 = np1.random.random((2,2))*10 -5
A1 = A1.astype(int)
print("The elements entered in the array A1 along with dimensions:")
print(A1)
B1 = np1.random.random((2,1))*10 -5
B1 = B1.astype(int)
print("The elements entered in the array B1 along with dimensions:")
print(B1)
C1 = np1.random.random ( (1, 2))*10 -5
C1 = C1.astype(int)
print("The elements entered in the array C1 along with dimensions:")
print(C1)
print("The resultant merged array A1 and C1 is:")
np1.concatenate((A1, C1))
Output:
Example #2
Displaying concatenation of arrays with different shape:
Code:
# Python program explaining the use of NumPy.concatenate function import numpy as np1
import numpy as np1
A1 = np1.random.random((2,2))*10-5
A1 = A1.astype (int)
print("The elements entered in the array A1 along with dimensions:")
print(A1)
B1 = np1.random.random((2,1))*10 -5
B1 = B1.astype(int)
print ("The elements entered in the array B1 along with dimensions:")
print (B1)
print ("The resultant merged array A1 and B1 is:")
np1.concatenate((A1, B1),axis=1)
Output:
Conclusion
The NumPy.concatenate() function is very essential in use, as it readily make the merging and listing of various air are possible without out a large code that helps in doing the same. Moreover, it provides the versatility of changing the access from where the merge is being done so that the resultant mud array can be produced both vertically and horizontally. In a general essence, it helps in reducing the verbosity of the code which enhances the turnaround speed for the program that is being run.
Recommended Articles
This is a guide to NumPy Concatenate. Here we also discuss the introduction and how does numpy concatenate work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses