In this post, we are going to learn NumPy concatenate 2D array in Python with examples that include how to NumPy concatenate 2D array,1D array, an array of different dimensional.
numpy.conatenate() function
The NumPy library array concatenates() function concatenates two numpy arrays of the same shape either row-wise or column-wise.This function by default concatenate arrays row-wise (axis=0).
Syntax
numpy.concatenate((a1, a2, ...an), axis=0, out=None, dtype=None)
Parameters
- a1..an : The sequence of array_like object.The sequence must be of same shape
- axis :The axis along which array is joined.It is optional parameter.
- axis=none: The input array is flatten before use.
- axis =1: The array is join column-wise
- axis = 0 : The array is joined row-wise.It is defulat value of axis.
- Out : optional The output ndarray if provided output is placed in this array.The shape must be correct.
- dtype : optional if provided the output array of this type.
1. Concatenate two or mutiple arrays 1D NumPy arrays.
In this example, we are concatenating two or multiple numpy arrays using concatenate function. To use this function we need to import the NumPy library using “import numpy as np”
import numpy as np
nparr1 = np.array([13, 14, 15, 18, 20])
nparr2 = np.array([22, 32, 33, 34, 36])
nparr3 = np.array([5, 10, 12, 13, 14])
nparr4 = np.array([2, 4, 6, 8, 10])
joined_nparr = np.concatenate( (nparr1, nparr2) )
print('concatenate two arrays:',joined_nparr)
muti_nparr = np.concatenate( (nparr1, nparr2) )
print('concatenate mutiple arrays:',muti_nparr)
Output
concatenate two arrays: [13 14 15 18 20 22 32 33 34 36]
concatenate mutiple arrays: [13 14 15 18 20 22 32 33 34 36]
2. Concatenate 1D array to 2D Numpy array
In this example, we are concatenating a 1-dimensional numpy array to a 2-dimensional numpy array with setting axis=1 column-wise in concatenate function.
import numpy as np
nparr1 = np.array([13, 14, 15, 18, 20])
nparr2 = np.array([22, 32, 33, 34, 36])
joinnparr = np.stack((nparr1,nparr2),axis=1)
print(joinnparr)
Output
[[13 22]
[14 32]
[15 33]
[18 34]
[20 36]]
3. Concatenate 2D Numpy array axis column wise
In this python program example, we are concatenating 2 dimensional NumPy array column-wise by setting the axis =1.
import numpy as np
nparr1 = np.array([[13, 14, 15],
[26, 12, 23],
[24, 25, 30]])
nparr2 = np.array([ [11, 54, 9],
[3, 6, 7],
[4, 8, 62]])
# Concatenate 2D np Arrays column wise
joined_nparr = np.concatenate( (nparr1, nparr2), axis=1 )
print('concatenate column-wise:\n',joined_nparr)
Output
concatenate column-wise:
[[13 14 15 11 54 9]
[26 12 23 3 6 7]
[24 25 30 4 8 62]]
4. Concatenate 2D Numpy array axis row-wise
In this python program example, we are concatenating 2 dimesenioal NumPy arrays row-wise by setting the axis =0.
import numpy as np
nparr1 = np.array([[13, 14, 15],
[26, 12, 23],
[24, 25, 30]])
nparr2 = np.array([ [11, 54, 9],
[3, 6, 7],
[4, 8, 62]])
# Concatenate 2D np Arrays row-wise
join_nparr_rowwise = np.concatenate( (nparr1, nparr2), axis=0 )
print('\n concatenate row-wise:',join_nparr_rowwise)
Output
concatenate row-wise: [[13 14 15]
[26 12 23]
[24 25 30]
[11 54 9]
[ 3 6 7]
[ 4 8 62]]
5. Concatenate NumPy arrays of different dimensions
Sometimes we have to concatenate numpy arrays of different dimensional either row-wise or column-wise.To concatenate different dimensional numpy arrays column-wise we have to specify the array to concatenate function with T attribute. Let us understand below python program
Python Program to concatenate NumPy arrays of different dimensional
import numpy as np
nparr1 = np.array([[13, 14, 15],
[26, 12, 23]
])
nparr2 = np.array([ [11, 54, 9],
[3, 6, 7],
[4, 8, 62]])
# Concatenate 2D np Arrays row-wise
nparr_rowise = np.concatenate( (nparr1, nparr2), axis=0 )
print('\n concatenate 2d array of different size row-wise:',nparr_rowise )
nparr_colwise = np.concatenate( (nparr1.T, nparr2), axis=1 )
print('\n concatenate 2d arrayof different size column-wise:',nparr_colwise)
Output
concatenate 2d array of different size row-wise: [[13 14 15]
[26 12 23]
[11 54 9]
[ 3 6 7]
[ 4 8 62]]
concatenate 2d arrayof different size column-wise
[[ 7 8 9]
[10 11 12]
[13 14 15]]