You can use the Python NumPy concatenate() function to join a sequence of arrays along an axis (row/column). This function is used to join two or more arrays of the same shape along a specified axis. In this article, I will explain NumPy concatenate() function syntax and usage with examples.
1. Quick Examples of Python NumPy Concatenate()
If you are in a hurry, below are some quick examples of how to use Python NumPy concatenate() function.
# Below are the quick examples
# Example 1: Use np.concatenate() function
con = np.concatenate((arr, arr1))
print(con)
# Example 2: Use Joining the two arrays along axis 0
con = np.concatenate((arr, arr1), axis = 0)
print(con)
# Example 3: Use Joining the two arrays along axis 1
con = np.concatenate((arr, arr1), axis = 1)
print(con)
# Example 4: Use Joining the two arrays along axis=None
con = np.concatenate((arr, arr1), axis = None)
print(con)
2. NumPy concatenate() Syntax
Following is the syntax of the concatenate() function.
# numpy.concatenate() syntax
numpy.concatenate((arr, arr1, ...), axis=0, out=None)
arr,arr1
: a sequence of array_like. The arrays must have the same shape, except in the dimension corresponding to the axis (the first, by default).axis
: The axis along which the arrays will be joined. If the axis is None, arrays are flattened before use. Default is 0.out
: If provided, the destination to place the result.Returns
: The concatenated array.
3. Use numpy.concatenate() Method
Use numpy.concatenate()
to put the content of two or more arrays into a single array. This function takes several arguments along with the NumPy arrays to concatenate and returns a Numpy array ndarray. If the axis is not passed; it is taken as 0.
import numpy as np
arr = np.array([[4, 6], [9, 13]])
arr1 = np.array([[8, 3], [12, 19]])
# Use np.concatenate() function
con = np.concatenate((arr, arr1))
print(con)
Yields below output.
# Output:
[[ 4 6]
[ 9 13]
[ 8 3]
[12 19]]
4. Use Joining the Two Arrays along Axis = 0
You can use join two NumPy arrays row-wise by specifying axis=0
. Now the resulting array is a wide matrix with more columns than rows; follow the below example, 4 rows, and 2 columns.
import numpy as np
# Create NumPy arrays
arr = np.array([[4, 6], [9, 13]])
arr1 = np.array([[8, 3], [12, 19]])
# Use Joining the two arrays along axis 0
con = np.concatenate((arr, arr1), axis = 0)
print(con)
Yields the same output as above.
5. Use Joining the Two Arrays along Axis = 1
You can also join two NumPy arrays column-wise by specifying axis=1
. Now the resulting array is a wide matrix with more columns than rows.
import numpy as np
# Create NumPy arrays
arr = np.array([[4, 6], [9, 13]])
arr1 = np.array([[8, 3], [12, 19]])
# Use Joining the two arrays along axis 1
con = np.concatenate((arr, arr1), axis = 1)
print(con)
Yields below output.
# Output:
[[ 4 6 8 3]
[ 9 13 12 19]]
6. Use numpy.concatenate() with Axis=None
import numpy as np
# Create NumPy arrays
arr = np.array([[4, 6], [9, 13]])
arr1 = np.array([[8, 3], [12, 19]])
# Use Joining the two arrays along axis=None
con = np.concatenate((arr, arr1), axis = None)
print(con)
Yields below output.
# Output:
[ 4 6 9 13 8 3 12 19]
7. Conclusion
In this article, I have explained how to use NumPy concatenate() function to join two or more arrays into a single Numpy array with examples.
Happy Learning!!
Related Articles
- How to concatinate Array ?
- How to append NumPy array ?
- NumPy concatenate() Function
- How to sort elements of NumPy ?
- How to create NumPy array in different ways ?
- Python NumPy hstack Function
- Python NumPy Interpolate Function
- Python NumPy Reverse Array