Python numpy concatenate

The Python numpy concatenate function used to Join two or more arrays together. And it returns a concatenated ndarray as an output. The syntax of the Python numpy concatenate function is

numpy.concatenate((array1, array2,....), axis = 0)
  • array1, array2,… are the arrays that you want to concatenate. The arrays that you pass to this concatenate function must have the same shape. However, you can choose the arrays with different dimensions.
  • axis – This is an optional argument with default value as 0. Use this to specify in which way (horizontal or Vertical) concatenation should be done.

Python numpy array concatenate

In this example, we declared two numpy ndarrays. Next, we used this Python numpy concatenate function to join those two arrays.

import numpy as np
 
a = np.array([1, 2, 3])
print(a)
 
b = np.array([4, 5, 6])
print(b)
 
print('\n---Numpy concatenation---')
print(np.concatenate((a, b)))

Python numpy array concatenate output

[1 2 3]
[4 5 6]

---Numpy concatenation---
[1 2 3 4 5 6]

The Python Numpy concatenate function is not limited to join two arrays. You can use this function to concatenate more than two. Here, we are joining four different arrays using this function.

import numpy as np
a = np.array([1, 2, 3])
print(a)
 
b = np.array([4, 5, 6])
print(b)
 
c = np.array([7, 8, 9])
print(c)
 
d = np.array([10, 11, 12])
print(d)
 
print('\n---Numpy concatenation---')
print(np.concatenate((a, b, c, d)))

Numpy concatenate function on arrays output

[1 2 3]
[4 5 6]
[7 8 9]
[10 11 12]

---Numpy concatenation---
[ 1  2  3  4  5  6  7  8  9 10 11 12]

Python numpy concatenate 2D array

We are using Numpy concatenate function to join two dimensional arrays.

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print()
 
b = np.array([[7, 8, 9],[10, 11, 12]])
print(b)
 
print('\n---Numpy concatenation---')
print(np.concatenate((a, b)))
Python Numpy concatenate 2D array 1

It is another numpy example to concatenate 2D arrays. 

import numpy as np
a = np.array([[10, 20, 30, 40]])
print(a)
 
b = np.array([[50 ,60, 70, 80], [90 ,100, 110, 120]])
print(b)
 
print('\n---Numpy concatenate Array---')
print(np.concatenate((a, b)))
 
print('\n---Numpy concatenate Array---')
print(np.concatenate((b, a)))

Numpy concatenate 2D arrays output

[[10 20 30 40]]
[[ 50  60  70  80]
 [ 90 100 110 120]]

---Numpy concatenate Array---
[[ 10  20  30  40]
 [ 50  60  70  80]
 [ 90 100 110 120]]

---Numpy concatenate Array---
[[ 50  60  70  80]
 [ 90 100 110 120]
 [ 10  20  30  40]]

numpy concatenate 2D array with axis

Until now, we are using a concatenate function without an axis parameter. This time, we use this parameter value while concatenating two-dimensional arrays. Remember, If axis = 0, then the items in array b vertically appended to a. Whereas axis = 1 horizontally appends array items in b to a.

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print()
 
b = np.array([[7, 8, 9],[10, 11, 12]])
print(b)
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), axis = 0))
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), axis = 1))

concatenate numpy 2D array with axis output

[[1 2 3]
 [4 5 6]]

[[ 7  8  9]
 [10 11 12]]

---Numpy concatenation of Two Dimensional Array---
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

---Numpy concatenation of Two Dimensional Array---
[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

In Python, you don’t have to specify the axis. I mean, you can directly use the value.

import numpy as np
a = np.array([[10, 20, 30], [40, 50, 60]])
print(a)
print()
 
b = np.array([[70, 80, 90],[100, 110, 120]])
print(b)
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), 0))
 
print('\n---Numpy concatenation of Two Dimensional Array---')
print(np.concatenate((a, b), 1))

Numpy Array concatenate function output

[[10 20 30]
 [40 50 60]]

[[ 70  80  90]
 [100 110 120]]

---Numpy concatenation of Two Dimensional Array---
[[ 10  20  30]
 [ 40  50  60]
 [ 70  80  90]
 [100 110 120]]

---Numpy concatenation of Two Dimensional Array---
[[ 10  20  30  70  80  90]
 [ 40  50  60 100 110 120]]

Python Numpy concatenate 3D array

In this example, we are using Numpy concatenate function on three-dimensional arrays. First, we created two 3D random arrays using randint. Next, we used the concatenate function with different axis values.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (2, 3, 4)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (2, 3, 4)))
print(b)
 
print('\n---Numpy concatenation of Three Dimensional Array---')
print(np.concatenate((a, b), axis = -1))

Numpy concat three Dimensional arrays output

[[[5 3 6 4]
  [1 8 1 1]
  [1 9 7 2]]

 [[4 2 6 0]
  [4 3 6 8]
  [4 8 8 9]]]

[[[12 13 17 16]
  [17 14 16 13]
  [17 16 17 19]]

 [[16 18 18 16]
  [19 16 18 12]
  [18 19 18 14]]]

---Numpy concatenation of Three Dimensional Array---
[[[ 5  3  6  4 12 13 17 16]
  [ 1  8  1  1 17 14 16 13]
  [ 1  9  7  2 17 16 17 19]]

 [[ 4  2  6  0 16 18 18 16]
  [ 4  3  6  8 19 16 18 12]
  [ 4  8  8  9 18 19 18 14]]]
>>> 

Another example to concatenate the three dimensional Numpy Array.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (2, 3, 4)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (2, 3, 4)))
print(b)
 
print('\n---Numpy concatenation of Three Dimensional Array---')
print(np.concatenate((a, b), axis = -1))

concat Numpy 3D array output

[[[9 4 1 6]
  [1 9 4 5]
  [8 5 1 3]]

 [[9 3 3 3]
  [5 0 6 6]
  [5 9 7 4]]]

[[[16 17 12 12]
  [12 19 19 14]
  [11 11 11 14]]

 [[15 17 13 17]
  [17 13 17 19]
  [18 12 15 11]]]

---Numpy concatenation of Three Dimensional Array---
[[[ 9  4  1  6 16 17 12 12]
  [ 1  9  4  5 12 19 19 14]
  [ 8  5  1  3 11 11 11 14]]

 [[ 9  3  3  3 15 17 13 17]
  [ 5  0  6  6 17 13 17 19]
  [ 5  9  7  4 18 12 15 11]]]

Python numpy concatenate Different sizes

Until now, we are working with the same size of arrays (joining the same size arrays). Let me join different size arrays using this concatenate function.

import numpy as np
x = np.array([1, 2, 3])
print(x)
 
y = np.array([4, 5])
print(y)
 
print('\n---Numpy concatenation---')
print(np.concatenate((x, y)))
print()
 
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print()
 
b = np.array([[7, 8, 9],[10, 11, 12], [13, 14, 15]])
print(b)
 
print('\n---Numpy concatenation of two Dimensional Array---')
print(np.concatenate((a, b), axis = 0))
 
print('\n---Numpy concatenation of two Dimensional Array---')
print(np.concatenate((b, a.T), axis = 1))

numpy concatenate Different array sizes output

[1 2 3]
[4 5]

---Numpy concatenation---
[1 2 3 4 5]

[[1 2 3]
 [4 5 6]]

[[ 7  8  9]
 [10 11 12]
 [13 14 15]]

---Numpy concatenation of two Dimensional Array---
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]
 [13 14 15]]

---Numpy concatenation of two Dimensional Array---
[[ 7  8  9  1  4]
 [10 11 12  2  5]
 [13 14 15  3  6]]

Python numpy hstack

The Python numpy hstack function horizontally appends the array items, which is similar to axis = 1.

import numpy as np
a = np.array([10, 20, 30, 40])
print(a)
 
b = np.array([50 ,60, 70, 80])
print(b)
 
print('\n---Numpy hstack on one Dimensional Array---')
print(np.hstack((a, b)))

Numpy Array hstack output

[10 20 30 40]
[50 60 70 80]

---Numpy hstack on one Dimensional Array---
[10 20 30 40 50 60 70 80]

Let me use this numpy hstack function to concatenate two-dimensional arrays.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (3, 3)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (3, 3)))
print(b)
 
print('\n---Numpy hstack on Two Dimensional Array---')
print(np.hstack((a, b)))

numpy hstack function to concat two dimensional arrays output

[[3 6 0]
 [4 3 5]
 [3 8 7]]

[[11 12 11]
 [12 13 17]
 [19 11 17]]

---Numpy hstack on Two Dimensional Array---
[[ 3  6  0 11 12 11]
 [ 4  3  5 12 13 17]
 [ 3  8  7 19 11 17]]

Python numpy vstack

The Python numpy vstack function vertically appends the array items, which is similar to axis = 0.

import numpy as np
a = np.array([10, 20, 30, 40])
print(a)
 
b = np.array([50 ,60, 70, 80])
print(b)
 
print('\n---Numpy vstack on one Dimensional Array---')
print(np.vstack((a, b)))

numpy vstack function to concat arrays output

[10 20 30 40]
[50 60 70 80]

---Numpy vstack on one Dimensional Array---
[[10 20 30 40]
 [50 60 70 80]]

We are using the numpy vstack function to concatenate two-dimensional arrays.

import numpy as np
a = np.array(np.random.randint(0, 10, size = (3, 3)))
print(a)
print()
 
b = np.array(np.random.randint(11, 20, size = (3, 3)))
print(b)
 
print('\n---Numpy vstack on Two Dimensional Array---')
print(np.vstack((a, b)))

numpy vstack function to concat two dimensional arrays output

[[8 7 1]
 [3 4 5]
 [8 7 6]]

[[11 17 13]
 [14 12 14]
 [18 17 18]]

---Numpy vstack on Two Dimensional Array---
[[ 8  7  1]
 [ 3  4  5]
 [ 8  7  6]
 [11 17 13]
 [14 12 14]
 [18 17 18]]