A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/machine-learning/numpy-concatenate-function-python/ below:

numpy.concatenate() function in Python - GeeksforGeeks

numpy.concatenate() function in Python

Last Updated : 12 Jul, 2025

The numpy.concatenate() function combines multiple arrays into a single array along a specified axis. This function is particularly useful when working with large datasets or performing operations that require merging data from different sources. Unlike other array-joining functions like numpy.vstack() , numpy.hstack() which only join arrays vertically or horizontally numpy.concatenate() provides greater flexibility by allowing you to specify the axis along which the arrays are joined.

Syntax of numpy.concatenate()

The syntax for the numpy.concatenate() function is as follows:

numpy.concatenate((array1, array2, ...), axis=0, out=None, dtype=None)

Parameters:

Key Features of numpy.concatenate()
  1. Flexible Axis Specification : You can concatenate arrays along any axis make it versatile for multi-dimensional data.
  2. Data Compatibility : The arrays must have matching shapes along all axes except the one being concatenated.
  3. Efficient Memory Usage : The function creates a new array but shares the underlying data from the input arrays when possible.
  4. Supports Higher Dimensions : Works easily with 1D, 2D and higher-dimensional arrays.
Examples of Using numpy.concatenate()

Let us now look at some practical examples to understand how numpy.concatenate() works

Example 1: Concatenating 1D Arrays

Suppose you have two 1D arrays and want to combine them into a single array.

Python
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.concatenate((arr1, arr2))

print("Result:", result)

Output :

Result: [1 2 3 4 5 6]

Example 2: Concatenating 2D Arrays Along Rows (axis=0)

For 2D arrays you can concatenate along rows (default behavior) or columns

Python
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])

result = np.concatenate((arr1, arr2), axis=0)

print("Result:\n", result)

Output :

numpy_concatenate Comparison with Other Functions

While numpy.concatenate() is versatile there are other functions in NumPy that perform similar tasks:

  1. numpy.vstack() :
  2. numpy.hstack():
  3. numpy.append():

For scenarios which require flexibility and efficiency numpy.concatenate() is the preferred choice.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4