Using numpy.vstack() function (3 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

Numpy is a cornerstone library in the Python ecosystem for numerical computing. Understanding its array manipulation capabilities can significantly enhance data manipulation tasks. The numpy.vstack() function is a specific tool in this domain, designed for vertical stacking of arrays. This guide will introduce you to the vstack function through practical examples, covering its usage from basic to advanced scenarios.

What is numpy.vstack()?

numpy.vstack() is a function used to stack arrays vertically, which means appending arrays one on top of another along their first axis. This function is particularly useful when dealing with arrays of different dimensions or when combining multiple datasets vertically. The result is a new array that retains the original arrays’ properties but in a vertically stacked manner.

Syntax:

numpy.vstack(tup)

Parameters:

  • tup: sequence of array_like. The arrays must have the same shape along all but the first axis. 1-D arrays are converted to 2-D rows before stacking.

Returns:

  • stacked: ndarray. The array formed by vertically stacking the given arrays.

Basic Example

Starting with the basics, let’s look at how to stack two one-dimensional arrays vertically:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

result = np.vstack((a, b))
print(result)

This code will output:

[[1 2 3]
 [4 5 6]]

In this example, vstack has converted two one-dimensional arrays into a two-dimensional array, with each input array forming a row of the output array.

Intermediate Example

Moving onto a more complex example, we’ll see how to stack arrays of different shapes. Consider one two-dimensional array and one one-dimensional array:

import numpy as np

np.random.seed(0)  # For reproducibility

A = np.random.randint(10, size=(2, 3))  # 2x3 array
B = np.array([10, 11, 12])              # 1D array

result = np.vstack((A, B))
print('A:\n', A)
print('B:\n', B)
print('Result:\n', result)

This will produce:

A:
 [[5 0 3]
 [3 7 9]]
B:
 [10 11 12]
Result:
 [[ 5  0 3]
 [ 3  7 9]
 [10 11 12]]

In this scenario, vstack enables the stacking of a two-dimensional array with a one-dimensional array, promoting the latter to a two-dimensional array and then appending it vertically.

Advanced Example

For our advanced example, we’ll explore combining multiple arrays of different dimensions, including a 2D array, a 1D array, and even empty arrays:

import numpy as np

np.random.seed(42)

array1 = np.random.randint(0, 10, (2, 3))
array2 = np.arange(3)
empty_array = np.empty((0, 3), int)

result = np.vstack((array1, empty_array, array2))
print('Combined Array:\n', result)

Output:

Combined Array:
 [[6 3 7]
 [4 6 9]
 [0 1 2]]

This example demonstrates the flexibility of vstack in handling complex stacking scenarios, including the handling of empty arrays without affecting the integrity of the vertical stacking operation.

Conclusion

The numpy.vstack() function is a versatile tool for array manipulation, allowing for the vertical combination of arrays irrespective of their dimensions. Through the examples provided, we’ve seen how it can be applied in different contexts, from simple to complex. Mastering this function along with other NumPy capabilities can significantly enhance your data manipulation efficiency and open doors to more advanced data analysis techniques.