NumPy Concatenate Function

PythonPythonBeginner
Practice Now

Introduction

This tutorial explains the usage of the concatenate() function in NumPy library. The concatenate() function is mainly used to combine two or more NumPy arrays together. In other words, it is used to join a sequence of arrays along an existing axis. By using this function, we can concatenate arrays together either horizontally or vertically.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import NumPy Library

Before using the concatenate() function, we need to import the NumPy library. We can use the built-in import statement to import the NumPy library as shown below:

import numpy as np

Understanding concatenate() Syntax

The syntax required to use this function is as follows:

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

Parameters:

  • (a1, a2 ,...): This parameter indicates the sequence of array-like structures or arrays. Here a1,a2,... are the arrays having the same shape, which are to be concatenated together.
  • axis: This parameter is used to define the axis along which the array will be concatenated. The default value of this parameter is 0.
  • out: It is an optional parameter, which if provided, then it simply indicates the destination where the result will be placed. If no out argument is specified then the shape must be correct, and should match with that of what concatenate would have returned.

Returned Values:

The concatenate() function will return the concatenated array as a result.

Concatenate Two NumPy Arrays Vertically

In this example, we will concatenate two arrays vertically along axis 0. The code snippet for the same is as follows:

array1 = np.array([[5, 4], [6, 8]])
array2 = np.array([[13, 5], [72, 9]])
out = np.concatenate((array1, array2), axis = 0)
print("The result of concatenation along axis 0:")
print(out)

Output:

The result of concatenation along axis 0:
[[ 5  4]
 [ 6  8]
 [13  5]
 [72  9]]

Concatenate Two NumPy Arrays Horizontally

In this example, we will concatenate two arrays horizontally along axis 1. The code snippet for the same is as follows:

array1 = np.array([[5, 4], [6, 8]])
array2 = np.array([[13, 5], [72, 9]])
out = np.concatenate((array1, array2), axis = 1)
print("The result of concatenation along axis 1:")
print(out)

Output:

The result of concatenation along axis 1:
[[ 5  4 13  5]
 [ 6  8 72  9]]

Summary

This tutorial explained the usage of the concatenate() function in the NumPy library. We covered its syntax, parameters, and values returned by this function. You learned how to concatenate two or more NumPy arrays together either horizontally or vertically. It is a very useful function in data processing since it can combine and stack data in various ways.

Other Python Tutorials you may like