numpy.vstack() – The NumPy vstack Python Function

Introduction to NumPy vstack Python Function

Guide to NumPy vstack Python Function – The numpy.vstack() function is used for vertically stack a sequence of input arrays into a single array.

Syntax:

numpy.vstack(tup)

Parameters:

  • tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the first axis.

Return: [stacked ndarray] The stacked array of the input arrays.

Code Examples of NumPy vstack Python Function

Example 01:

# Python program explaining
# vstack() function

import numpy as geek

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

in_arr2 = geek.array([ 4, 5, 6] )
print ("2nd Input array : \n", in_arr2)

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

Output:

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

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Example 02:

# welcome to softhunt.net
# Python program explaining
# vstack() 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 vertically
out_arr = np.vstack((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]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What is the Difference Between numpy.hstack and numpy.vstack?

numpy.hstack and numpy.vstack are both NumPy stacks that mix NumPy arrays. The main distinction is that numpy.hstack stacks NumPy arrays horizontally, whereas numpy.vstack stacks them vertically. Aside from that, syntax and behavior are nearly identical.

What is the Difference Between numpy.vstack and numpy.concatenate?

numpy.concatenate is similar to numpy.vstack but more flexible. numpy.concatenate also joins NumPy arrays together, but it may do so in either a horizontal or vertical direction.

numpy.concatenate can join arrays in the same way that numpy.vstack does, and it can also combine arrays in the same way that numpy.hstack can. The behavior of np.concatenate is determined by how the axis argument is used in the syntax.

Another way to look at it is that numpy.vstack is a special case of numpy.concatenate.

Conclusion

That’s all for NumPy vstack 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() – The NumPy stack Python Function
  2. numpy.block() – The NumPy block Python Function
  3. numpy.concatenate() – The NumPy concatenate Python Function

Leave a Comment