How to use NumPy vstack() in Python

Spread the love

NumPy vstack() function in Python is used to stack or concate the sequence of given arrays vertically(row-wise). It takes all elements from the given arrays and forms a single array, where the elements are added vertically. This process is similar to the concatenation of arrays along the default axis = 0 after concatenating 1-D arrays of shape (N,) turn into reshaping (1, N).

It has only one parameter i.e tuple of arrays. We can use vstack() very effectively up to three-dimensional arrays. In this article, I will explain numpy.vstack() and using its syntax, parameter and how we can create a single array by taking elements of one or more arrays.

1. Quick Examples of NumPy vstack()

If you are in a hurry, below are some quick examples of how to use vstack() function.


# Below are the quick examples

# Example 1 : Using vstack() & get the stacked array
arr = np.array([1, 2, 3])
arr1 = np.array([4, 5, 6])
arr2 = np.vstack((arr, arr1))

# Example 2 : Get the stacked array 
arr = np.array([[1], [2], [3]])
arr1 = np.array([[4], [5], [6]])
arr2 = np.array([[7], [8], [9]])
arr3 = np.vstack((arr, arr1, arr2))

# Example 3 : Get the stacked array 
arr = np.array([[1], [2], [3]])
arr1 = np.array([[4], [5], [6]])
arr2 = np.array([[7], [8], [9]])
arr3 = np.vstack((arr, arr1, arr2))

# Example 4 : Get the 3-D stacked array
arr = np.array([[[1, 3], [2, 4]], [[3, 5], [5, 7]]])
arr1 = np.array([[[4, 1], [5, 7]], [[6, 8],[3, 5]]])
arr2 = np.vstack((arr, arr1))

2. Syntax of NumPy vstack()

Following is the syntax of the vstack() function.


# Syntax of vstack() 
numpy.vstack(tup)

2.1 Parameters of the vstack()

Following is the parameter of the NumPy vstack().

tup : It contains a sequence of arrays of the same shape. these arrays are to be concatenated as a parameter and return a single NumPy array.

2.2 Return Value of the vstack()

It returns the stacked array of the given arrays.

3. Usage of the NumPy vstack()

vstack() function is used to stack the sequence of NumPy arrays vertically and return the single array. In order to join two arrays, Python NumPy module provides different types of functions which are concatenate(), stack(), vstack(), and hstack().

Below I have provided an image explaining how vstack works, it will give you better understanding.

NumPy vstack
numpy.vstack()

Let’s create 1-D arrays using numpy.array() and apply this function, it will return the stacked array., where the elements are stacked row-wise. For example,


import numpy as np 
# Using vstack() & get the stacked array
arr = np.array([1, 2, 3])
arr1 = np.array([4, 5, 6])
arr2 = np.vstack((arr, arr1))
print(arr2)

# Output:
# [[1 2 3]
# [4 5 6]]

Above example is same as concatenation of arrays along the default axis.

4. Get the 2-D Stacked NumPy Array

Take 2-D NumPy arrays and pass them into this function as a parameter, it will return the single 2-D array. For example,


# Get the stacked array
arr = np.array([[1, 2], [3, 4]])
arr1 = np.array([[4, 5], [6, 7]])
arr2 = np.vstack((arr, arr1))
print(arr2)

# Output:
# [[1 2]
# [3 4]
# [4 5]
# [6 7]]

This time we will pass three 2-D NumPy arrays into this function, it will return the 2-D single array where the elements are stacked vertically.


# Get the stacked array 
arr = np.array([[1], [2], [3]])
arr1 = np.array([[4], [5], [6]])
arr2 = np.array([[7], [8], [9]])
arr3 = np.vstack((arr, arr1, arr2))
print(arr3)

# Output:
# [[1]
# [2]
# [3]
# [4]
# [5]
# [6]
# [7]
# [8]
# [9]]

5. Get the 3-D Stacked NumPy Array

We can pass 3-D NumPy arrays as a parameter into this function, it will return a single array. Let’s take two 3-D arrays of shapes (2, 2, 2) and apply this function, it will return a single 3-D array of shapes (4, 2, 2). For example,


# Get the 3-D stacked array
arr = np.array([[[1, 3], [2, 4]], [[3, 5], [5, 7]]])
arr1 = np.array([[[4, 1], [5, 7]], [[6, 8],[3, 5]]])
arr2 = np.vstack((arr, arr1))
print(arr2)

Yields below output


# Output:
# Stacked 3-D array
[[[1 3]
  [2 4]]

 [[3 5]
  [5 7]]

 [[4 1]
  [5 7]]

 [[6 8]
  [3 5]]]

6. Conclusion

In this article, I have explained numpy.vstack() and using this how we can stack the sequence of given arrays into a single array with examples.

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing How to use NumPy vstack() in Python