numpy.stack() in Python
numpy.stack() in Python
The numpy.stack() function joins a sequence of arrays along a new axis.
Syntax
numpy.stack(arrays, axis=0, out=None)
Parameter
arrays: This parameter represents a sequence of the array where each array have the same shape.
axis: It is an optional parameter which takes integer values. It represents the axis along which the arrays will be joined.
out: This parameter represents the destination to place the result.
Return
This function returns the stacked array which has one or more dimension than the input arrays.
Example 1
# Python program explaining # numpy.stack() function import numpy as np # input array inp_arr1 = np.array([ 5, 6, 7] ) print ("1st Input array: ", inp_arr1) inp_arr2 = np.array([ 10, 11, 12] ) print ("2nd Input array: ", inp_arr2) # Stacking the arrays along axis 0 out_arr = np.stack((inp_arr1, inp_arr2), axis = 0) print ("Output array along axis 0:\n ", out_arr)
Output
1st Input array: [5 6 7] 2nd Input array: [10 11 12] Output array along axis 0: [[5 6 7] [10 11 12]]