What is the numpy.vstack() function in NumPy?

Overview

The vstack() function in NumPy is used to vertically stack or arrange arrays in sequence (row-wise).

Syntax

Error: Code Block Widget Crashed, Please Contact Support

Parameter value

The vstack() function takes a single parameter value, tup, which represents the arrays having the same shape along all the axis except for the first axis.

Return value

The vstack() function returns at least a 2D array formed by stacking vertically, the given arrays.

Example

import numpy as np
# creating input arrays
a = np.array([1, 2, 3, 4, 5])
b = np.array([6, 7, 8, 9, 10])
# stacking the arrays vertically
stacked_array = np.vstack((a, b))
# printing the new array
print(stacked_array)
Implementing the vstack() function

Explanation

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

Free Resources