NumPy Array Concatenation

In this tutorial you will learn:

  • What is array concatenation and its uses?
  • NumPy array 1D array concatenation
  • NumPy array 2D array concatenation
  • NumPy array 3D array concatenation

NumPy Array Concatenation

During machine learning many programmers come across a situation where they have to join in or concatenate the arrays to either show result or further manipulate it. Concatenation is related to the action of linking things together in a series, just as the joining functions (outer join, inner join etc) in SQL databases. The main difference is that in NumPy array concatenation, we join the array on basis of indexes while in SQL databases the joining is carried out on the basis of keys.

Concatenation using concatenate()

The concatenate function provides the simplest form of index to index joining, it takes in the three parameters, first parameter is the first array, second parameter is the second array while the third parameter is an optional parameter that indicates the axis of joining, if not defined explicitly the value of axis is taken as 0.

In this example I will demonstrate the joining of two 1D arrays. Here you can clearly observe that I have not defined the axis, so joining two 1 D array will convert it into a 1D array containing all the elements of first array and second array in a single row.

Concatenate 1D Arrays

  1. import numpy as np
  2. first_arr = np.array(['a','b','c'])
  3. print('First 1D array being concatenated:', first_arr)
  4. second_arr = np.array(['d','e','f'])
  5. print('Second 1D array being concatenated:', second_arr)
  6. my_arr = np.concatenate((first_arr, second_arr))
  7. print('Result of Array after concatenation:', my_arr)

Concatenate 2D arrays

In this example, I will concatenate two 2D arrays without defining the axis, the 2D arrays used have dimension of 2(rows) x 5(columns), their concatenation will result in an array having dimension of 4 x 5

  1. import numpy as np
  2. first_arr = np.array([['s','o','u','r','c'],['o','d','e','s','t']])
  3. print('First 2D array being concatenated: \n', first_arr)
  4. second_arr = np.array([['t','e','r','t','u'],['t','o','r','i','a']])
  5. print('Second 2D array being concatenated: \n', second_arr)
  6. my_arr = np.concatenate((first_arr, second_arr))
  7. print('Result of Array after concatenation: \n', my_arr)

This example is same as above except I have defined the value of axis equal to one, therefore the dimension of concatenated array is 2 x 10

  1. import numpy as np
  2. first_arr = np.array([['s','o','u','r','c'],['o','d','e','s','t']])
  3. print('First 2D array being concatenated: \n', first_arr)
  4. second_arr = np.array([['t','e','r','t','u'],['t','o','r','i','a']])
  5. print('Second 2D array being concatenated: \n', second_arr)
  6. my_arr = np.concatenate((first_arr, second_arr), axis=1)
  7. print('Result of Array after concatenation(axis = 1): \n', my_arr)

Concatenate 3D arrays

In this example, we will concatenate two 3D arrays while each array is having dimension of 1 x 3 x 5. First I will concatenate this array while not defining the axis and it will result into new concatenated array having dimension of 2 x 3 x 5.

  1. import numpy as np
  2. first_arr = np.array([[['s','o','u','r','c'],['o','d','e','s','t'],['t','e','r','t','u']]])
  3. print('First 3D array being concatenated: \n', first_arr)
  4. second_arr = np.array([[['t','o','r','i','a'],['l','s','l','e','a'],['r','n','i','n','g']]])
  5. print('Second 3D array being concatenated: \n', second_arr)
  6. my_arr = np.concatenate((first_arr, second_arr))
  7. print('Result of Array after concatenation(axis = 0): \n', my_arr)

In this example, we will concatenate two 3D arrays, each having dimension of 1 x 3 x 5, and the value of axis used in this example is 1. So it will result in new concatenated array having dimensions of 1 x 6 x 5.

  1. import numpy as np
  2. first_arr = np.array([[['s','o','u','r','c'],['o','d','e','s','t'],['t','e','r','t','u']]])
  3. print('First 3D array being concatenated: \n', first_arr)
  4. second_arr = np.array([[['t','o','r','i','a'],['l','s','l','e','a'],['r','n','i','n','g']]])
  5. print('Second 3D array being concatenated: \n', second_arr)
  6. my_arr = np.concatenate((first_arr, second_arr), axis=1)
  7. print('Result of Array after concatenation(axis = 1): \n', my_arr)

In this example we will again be concatenating two 3D arrays having dimension of 1 x 3 x 5 while keeping the value of axis equal to 2. The concatenation will result in a new array having dimensions of 1 x 3 x 10

  1. import numpy as np
  2. first_arr = np.array([[['s','o','u','r','c'],['o','d','e','s','t'],['t','e','r','t','u']]])
  3. print('First 3D array being concatenated: \n', first_arr)
  4. second_arr = np.array([[['t','o','r','i','a'],['l','s','l','e','a'],['r','n','i','n','g']]])
  5. print('Second 3D array being concatenated: \n', second_arr)
  6. my_arr = np.concatenate((first_arr, second_arr), axis=2)
  7. print('Result of Array after concatenation(axis = 2): \n',my_arr)

Add new comment