What is the numpy.column_stack() function in NumPy?
Overview
The column_stack() function in NumPy is used to stack or arrange 1-D input arrays as columns into a 2-D array.
Syntax
numpy.column_stack(tup)
Syntax for the column_stack() function in NumpY
Parameter value
The column_stack() function takes a parameter value.
-
tup: This represents a sequence of the1-Dor2-Darrays to be stacked.
Return value
The column_stack() function returns an array formed by stacking the input arrays.
Example code
import numpy as np# creating input arraysa = np.array([1, 2, 3, 4])b = np.array([5, 6, 7, 8])# stacking the arraysstacked_array=np.column_stack((a,b))# printing the stacked arrayprint(stacked_array)
Explanation
- Line 1: We import the
numpymodule. - Lines 3–4: We create input arrays
aandbusing thearray()function. - Line 7: We stack arrays
aandbusing thecolumn_stack()function. The result is assigned to a variable,stacked_array. - Line 10: We print the newly stacked array,
stacked_array.