0 votes
in Python by
explain numpy.column_stack() in Python

1 Answer

0 votes
by

numpy.column_stack() in Python

The numpy.column_stack() function stacks  the 1-D arrays as columns into a 2-D array. It takes a sequence of 1-D arrays and stacks them as columns to make a single 2-D array.

Syntax

numpy.column_stack(tup)

Parameter

tup : This parameter represents a sequence of 1-D or 2-D arrays where all of them must have the same first dimension.

Return

This function returns the array formed by stacking the given arrays.

Example 1

# Python program explaining
# numpy.column_stack() 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.column_stack((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 14]
[12 15]
[13 16]]

Example 2

# Python program explaining
# numpy.column_stack() 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) 
out_array = np.column_stack((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]]

Related questions

0 votes
asked May 19, 2022 in Python by john ganales
0 votes
asked May 19, 2022 in Python by john ganales
...