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 the 1-D or 2-D arrays 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 arrays
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
# stacking the arrays
stacked_array=np.column_stack((a,b))
# printing the stacked array
print(stacked_array)

Explanation

  • Line 1: We import the numpy module.
  • Lines 3–4: We create input arrays a and b using the array() function.
  • Line 7: We stack arrays a and b using the column_stack() function. The result is assigned to a variable, stacked_array.
  • Line 10: We print the newly stacked array, stacked_array.

Free Resources