What is the numpy.concatenate() function in Python?
Overview
The concatenate() function in Python is used to concatenate or join a sequence of input arrays along an existing axis.
Syntax
numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
Syntax for the numpy.concatenate() function
Parameter value
The concatenate() function takes the following parameter values.
(a1, a2, ...): These are the input arrays to be concatenated. This is a required parameter.axis: This is the given axis along which the input arrays will be joined. This is an optional parameter.out: This is the destination path for the result. This is an optional parameter.dtype: This is the data type of the desired output array. The value can be inferred from the data type of the input arrays. This is an optional parameter.casting: This controls the kind of datacasting that may occur. This is an optional parameter.
Return value
The concatenate() function returns the concatenated array.
Example
import numpy as np# creating input arraysa = np.array([[1, 2, 3], [4, 5, 6]])b = np.array([[7, 8, 9]])# concatenating the arraysmyarray = np.concatenate((a,b), axis=0)# printing the concatenated arrayprint(myarray)
Explanation
- Line 1: We import the
numpymodule. - Lines 3–4: We create input arrays,
aandb, using thenp.array()function. - Line 7: We concatenate the input arrays using the
np.concatenate()function. The result is assigned to a variable,myarray. - Line 10: We print the concatenated array,
myarray.