NumPy column_stack and row_stack Python Function

In this article, we will cover NumPy’s two functions which are NumPy column_stack and row_stack.

Introduction to NumPy column_stack and row_stack

NumPy column_stack

The numpy.column_stack() function is used to create a 2-D array by stacking 1-D arrays as columns. To construct a single 2-D array, it stacks a sequence of 1-D arrays as columns. 2-D arrays are stacked in the same way as the hstack function does.

Syntax:

numpy.column_stack(tup)

Parameters:

  • tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same first dimension.

Return: [stacked 2-D array] The stacked 2-D array of the input arrays.

NumPy row_stack

numpy.row_stack() function Stack arrays vertically in sequence (row-wise). This is the same as concatenation along the first axis after reshaping 1-D arrays of shape (N) (1, N). Rebuilds arrays that have been divided by vsplit. This function works well with arrays of up to three dimensions. For pixel data having a height (first axis), width (second axis), and r/g/b channels, for example (third axis). More broad stacking and concatenation operations are provided by the functions concatenate, stack, and block.

Syntax:

numpy.row_stack(tup)

Parameters:

  • tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same first dimension.

Return: [stacked 2-D array] The stacked 2-D array of the input arrays.

Code Examples of NumPy column_stack and row_stack

NumPy column_stack

Example 01:

# welcome to softhunt.net
# Python program explaining
# column_stack() function

import numpy as np

# input array
in_arr1 = np.array((0, 1, 3,))
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array((5, 7, 9))
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays
out_arr = np.column_stack((in_arr1, in_arr2))
print ("Output stacked array:\n ", out_arr)

Output:

1st Input array : 
 [0 1 3]
2nd Input array : 
 [5 7 9]
Output stacked array:
  [[0 5]
 [1 7]
 [3 9]]

Example 02:

# welcome to softhunt.net
# Python program explaining
# column_stack() function

import numpy as np

# input array
in_arr1 = np.array([[0, 1, 3], [-0, -1, -3]] )
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array([[5, 7, 9], [-5, -7, -9]] )
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays
out_arr = np.column_stack((in_arr1, in_arr2))
print ("Output stacked array :\n ", out_arr)

Output:

1st Input array : 
 [[ 0  1  3]
 [ 0 -1 -3]]
2nd Input array : 
 [[ 5  7  9]
 [-5 -7 -9]]
Output stacked array :
  [[ 0  1  3  5  7  9]
 [ 0 -1 -3 -5 -7 -9]]

NumPy row_stack

Example 01:

# welcome to softhunt.net
# Python program explaining
# row_stack() function

import numpy as np

# input array
in_arr1 = np.array((0, 1, 3,))
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array((5, 7, 9))
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays
out_arr = np.row_stack((in_arr1, in_arr2))
print ("Output stacked array:\n ", out_arr)

Output:

1st Input array : 
 [0 1 3]
2nd Input array : 
 [5 7 9]
Output stacked array:
  [[0 1 3]
 [5 7 9]]

Example 02:

# welcome to softhunt.net
# Python program explaining
# row_stack() function

import numpy as np

# input array
in_arr1 = np.array([[0, 1, 3], [-0, -1, -3]] )
print ("1st Input array : \n", in_arr1)

in_arr2 = np.array([[5, 7, 9], [-5, -7, -9]] )
print ("2nd Input array : \n", in_arr2)

# Stacking the two arrays
out_arr = np.row_stack((in_arr1, in_arr2))
print ("Output stacked array :\n ", out_arr)

Output:

1st Input array : 
 [[ 0  1  3]
 [ 0 -1 -3]]
2nd Input array : 
 [[ 5  7  9]
 [-5 -7 -9]]
Output stacked array :
  [[ 0  1  3]
 [ 0 -1 -3]
 [ 5  7  9]
 [-5 -7 -9]]

FAQs

What is NumPy

NumPy (Numerical Python) is a library that consists of multidimensional array objects and a set of functions for manipulating them. It allows you to conduct mathematical and logical operations on arrays.

NumPy is a Python scripting language. ‘Numerical Python’ is what it stands for. It is a library that contains multidimensional array objects as well as a collection of array linear methods.

What are the features of NumPy in Python?

  1. High-performance N-dimensional array object
  2. It contains tools for integrating code from C/C++ and Fortran
  3. It contains a multidimensional container for generic data
  4. Additional linear algebra, Fourier transform, and random number capabilities
  5. It consists of broadcasting functions
  6. It had data type definition capability to work with varied databases.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.stack()
  2. numpy.vstack()
  3. numpy.hstack()
  4. numpy.dstack() 

Leave a Comment