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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) numpy(("`NumPy`")) -.-> numpy/ArrayManipulationGroup(["`Array Manipulation`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") numpy/ArrayManipulationGroup -.-> numpy/merge("`Merge`") subgraph Lab Skills python/lists -.-> lab-86420{{"`NumPy Concatenate Function`"}} python/tuples -.-> lab-86420{{"`NumPy Concatenate Function`"}} python/importing_modules -.-> lab-86420{{"`NumPy Concatenate Function`"}} python/numerical_computing -.-> lab-86420{{"`NumPy Concatenate Function`"}} python/build_in_functions -.-> lab-86420{{"`NumPy Concatenate Function`"}} numpy/multi_array -.-> lab-86420{{"`NumPy Concatenate Function`"}} numpy/data_array -.-> lab-86420{{"`NumPy Concatenate Function`"}} numpy/bool_idx -.-> lab-86420{{"`NumPy Concatenate Function`"}} numpy/fancy_idx -.-> lab-86420{{"`NumPy Concatenate Function`"}} numpy/merge -.-> lab-86420{{"`NumPy Concatenate Function`"}} end

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