Numpy Concatenate() Function



The Numpy Concatenate() Function is used to join a sequence of arrays along an existing axis. This function takes a tuple or list of arrays to concatenate and an optional axis parameter that specifies the axis along which the arrays will be joined.

If no axis is provided then the arrays are flattened before concatenation. This function is useful for combining arrays in various dimensions whether for stacking them vertically, horizontally or along any specified axis.

Syntax

The syntax for the Numpy concatenate() function is as follows −

numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")

Parameters

Following are the parameters of the Numpy concatenate() function −

  • a1, a2, ...: array_like : These are the arrays to be concatenated. They must have the same shape, except in the dimension corresponding to axis.
  • axis(int, optional): The axis along which the arrays will be joined. The default is 0. If axis is None, the arrays are flattened before use.
  • out(ndarray, optional): If provided, the destination to place the result. It must have the right shape to hold the output.
  • dtype(data-type, optional): The type to use in the output array. By default, the dtype will be inferred from the inputs.
  • casting({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.

Return Value

It returns the concatenated array.

Example 1

Following is the example of Numpy Concatenate() Function which demonstrates concatenating two 1-D arrays along the default axis (axis=0), resulting in a single 1-D array −

import numpy as np

# Define two 1-D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Concatenate along the default axis (axis=0)
result = np.concatenate((array1, array2))

print("Result of concatenation:", result)	

Output

Result of concatenation: [1 2 3 4 5 6]

Example 2

Here in the below example we show how to concatenate two 2-D arrays along the rows (axis=0), resulting in a single 2-D array with more rows.

import numpy as np

# Define two 2-D arrays
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

# Concatenate along axis 0 (rows)
result = np.concatenate((array1, array2), axis=0)

print("Result of concatenation along axis 0:", result)

Output

Result of concatenation along axis 0:
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]

Example 3

We can concatenate two 2-D arrays along the columns by defining axis=1 in the concatenate() function, resulting in a single 2-D array with more columns. Below is the example of it −

import numpy as np

# Define two 2-D arrays
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

# Concatenate along axis 1 (columns)
result = np.concatenate((array1, array2), axis=1)

print("Result of concatenation along axis 1:", result)

Output

Result of concatenation along axis 1:
 [[1 2 5 6]
 [3 4 7 8]]
numpy_array_manipulation.htm
Advertisements