numpy.hstack() in Python
numpy.hstack() in Python
The numpy.hstack() function stacks the arrays in a sequence horizontally (column wise).
Syntax
numpy.hstack(tup)
Parameter
tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape, except 1-D arrays which can be of any length.
Return
The function returns the array formed by stacking the given arrays.
Example 1
# Python program explaining # numpy.hstack() function import numpy as np inp_array1 = np.array([[ 11, 12, 13], [ -11, -12, -13]] ) print ("Input array: ", inp_array1) inp_array2 = np.array([[ 14, 15, 16], [ -14, -15, -16]] ) print ("Input array: ", inp_array2) #Stack arrays in sequence horizontally (column wise). out_array = np.hstack((inp_array1, inp_array2)) print ("Output array: ", out_array)
Output
Input array: [[ 11 12 13] [-11 -12 -13]] Input array: [[ 14 15 16] [-14 -15 -16]] Output array: [[ 11 12 13 14 15 16] [-11 -12 -13 -14 -15 -16]]
Example 2
# Python program explaining # numpy.hstack() function import numpy as np inp_arr1 = np.array(( 11, 12, 13 )) print ("1st Input array: ", inp_arr1) inp_arr2 = np.array(( 14, 15, 16 )) print ("2nd Input array: ", inp_arr2) # Stacking the two arrays out_arr = np.hstack((inp_arr1, inp_arr2)) print ("Output array: ", out_arr)
Output
1st Input array: [11 12 13] 2nd Input array: [14 15 16] Output array: [11 12 13 14 15 16]