Updated May 20, 2023
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 display. The concatenate function in Python allows the user to merge two arrays, either by column or row. 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. Concatenating two separate regions that are not of the same form requires first reshaping them with the reshape function to convert a single-dimensional array into a two-dimensional array, allowing the final concatenated array to be a 3×3 array with three rows and three columns.
Syntax and Parameters
The syntax for application of NumPy concatenate.
Following is the syntax used to utilize the numpy concatenate() while writing codes in the Python programming language:
numpy.concatenate((a1, a2, ...), axis)
Parameters:
Following are 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 must be of the same shape (i.e., dimension) except for the consideration of the dimension that corresponds to the specified axis. In case the user has not specified the axis, 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 the user has specified no out argument.
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, takes 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, an error is displayed because of the axis of each of These areas is 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 vertical or horizontal diversity happens on the data.
If the shapes of the areas to be concatenated disagree, an error message will be displayed when the system displays the output. In such cases, the reshape function can be used to generate an erasure of the same shape for the required result and shape, allowing arrays of varying shapes and dimensions to be merged. The concatenate function can also be used to merge two arrays column by column. If the axis is set to 1, the resulting array is column-wise merged, resulting in a broad matrix with more integers 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().
Examples of NumPy Concatenate
An example displaying the use 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 shapes:
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 essential in use, as it readily makes the merging and listing of various air possible without a large code that helps in doing the same. In essence, it helps to reduce the verbosity of the code, leading to improved program execution speed and efficiency.
Recommended Articles
We hope that this EDUCBA information on “NumPy Concatenate” was beneficial to you. You can view EDUCBA’s recommended articles for more information.