In this article, we will dsicuss how to join a sequence of numpy arrays along any given axis using concatenate() function.

Table of Contents:

Overview of numpy.concatenate()

Numpy library in python provides a function to concatenate two or more arrays along a given axis.

numpy.concatenate((a1, a2, …), axis=0, out=None)

Arguments:

  • a1, a2,…: A Sequence of array_like like objects
    • The arrays in sequence must be of shape same shape.
  • axis: int, optional | Default value is 0.
    • The axis along which we want the arrays to be joined.
      • If axis is None, then all arrays are flattened and the..
      • If axis is 0, then all arrays are joined row wise.
      • If axis is 1, then all arrays are joined column wise.
  • out: ndarray, optional
    • If provided, place the result in the our array. The shape of our must match with expected result value of concatenate() function.

Returns:

  • Returns a new ndarray i.e. a Numpy array containing the concatenated values from all input arrays.

Examples of numpy.concatenate()

Concatenate two 1D Numpy Arrays

Suppose we have two 1D NumPy Arrays and we want to join them one after another and create a merged array. For that we need to create a sequence of both the arrays and pass them to the numpy.concatenate() function. For example,

import numpy as np

# A Numpy Array of integers
first = np.array([1, 1, 1, 1, 1])
# Another Numpy Array of integers
second = np.array([2, 2, 2, 2, 2])

# Concatenate two arrays to create a merged array
merged_arr = np.concatenate( (first, second) )

print(merged_arr)

Output:

[1 1 1 1 1 2 2 2 2 2]

We created a tuple of 2 arrays and passed that to the concatenate() function. It returned a new merged array containing the contents of both the arrays.

Concatenate multiple 1D Numpy Arrays

To join multiple 1D Numpy Arrays, we can create a sequence of all these arrays and pass that sequence to concatenate() function. For example, let’s see how to join three numpy arrays to create a single merged array,

import numpy as np

# Create three Numpy Arrays of integers
first =  np.array([1, 1, 1, 1, 1])
second = np.array([2, 2, 2, 2, 2])
third =  np.array([3, 3, 3, 3, 3])

# Concatenate three arrays to create a merged array
merged_arr = np.concatenate( (first, second, third) )

print(merged_arr)

Output:

[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]

Concatenate 2D Numpy Arrays row wise

To join two 2D Numpy Arrays row-wise, we need pass a sequence of arrays to concatenate() function along with value 0 for the axis parameter. It will insert all the rows of arrays one after another into a new array and returns the merged array. For example,

import numpy as np

# Create 2D Numpy array of hard coded numbers
first = np.array([[1, 1, 1],
                  [2, 2, 2],
                  [3, 3, 3]])

# Create another 2D Numpy array of hard coded numbers
second = np.array([ [4, 4, 4],
                    [6, 5, 5],
                    [6, 6, 6]])

# Concatenate 2D Numpy Arrays row wise

# Merge two 2D arrays row-wise
merged_arr = np.concatenate( (first, second), axis=0 )

print(merged_arr)

Output:

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

Concatenate 2D Numpy Arrays column wise

To join two 2D Numpy Arrays column-wise, we need pass a sequence of arrays to concatenate() function along with value 1 for the axis parameter. It will insert all the columns of arrays, one after another into a new array and returns the merged array. For example,

import numpy as np

# Create 2D Numpy array of hard coded numbers
first = np.array([[1, 1, 1],
                  [2, 2, 2],
                  [3, 3, 3]])

# Create another 2D Numpy array of hard coded numbers
second = np.array([ [4, 4, 4],
                    [6, 5, 5],
                    [6, 6, 6]])


# Concatenate 2D Numpy Arrays column wise
merged_arr = np.concatenate( (first, second), axis=1 )

print(merged_arr)

Output:

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

Concatenate 2D Numpy Arrays by flattening the shape

If we pass None as value for axis parameter, then concatenate() function will flatten all the arrays and join them. For example,

import numpy as np

# Create 2D Numpy array of hard coded numbers
first = np.array([[1, 1, 1],
                  [2, 2, 2],
                  [3, 3, 3]])

# Create another 2D Numpy array of hard coded numbers
second = np.array([ [4, 4, 4],
                    [6, 5, 5],
                    [6, 6, 6]])


# Concatenate 2D Numpy Arrays by flattening the shape
merged_arr = np.concatenate( (first, second), axis=None )

print(merged_arr)

Output:

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

Summary:

In this article, we learned that how we can join NumPy Arrays of different size and shapes.