In Python, we use the stack()
function to join or concatenate a sequence of input arrays along a new axis.
numpy.stack(arrays, axis=0, out=None)
The stack()
function takes the following parameter values:
arrays
(required): These are the input arrays to be concatenated.axis
(optional): This is the given axis along which we append the input arrays. out
(optional): This is the destination path for the result.The stack()
function returns the concatenated array.
import numpy as np# creating input arraysa = np.array([[1, 2, 3], [4, 5, 6]])b = np.array([[7, 8, 9], [10, 11, 12]])# concatenating the arraysmyarray = np.stack((a, b), axis = 0)# printing the concatenated arrayprint(myarray)
NumPy
module.a
and b
, using the array()
function.stack()
function. The result is assigned to a variable, myArray
.myArray
.import numpy as np# creating input arraysa = np.array([[1, 2, 3], [4, 5, 6]])b = np.array([[7, 8, 9], [10, 11, 12]])# concatenating the arraysmyarray = np.stack((a, b), axis = -1)# printing the concatenated arrayprint(myarray)
The code above is similar to Example 1 except that the arrays are concatenated along the last axis, i.e axis = -1
.