numpy.vstack()
is a function in the NumPy library in Python that is used to stack arrays in a vertical direction.
The syntax for the numpy.vstack()
function is as follows:
numpy.vstack(tup)
Here, tup
is a tuple containing the arrays to be stacked.
The function returns a single array that is the result of stacking the input arrays in the vertical direction.
Here are a few examples of how to use the numpy.vstack()
function in Python:
import numpy as np
# Stack two 1-D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
stacked_array = np.vstack((array1, array2))
print(stacked_array) # [[1 2 3]
# [4 5 6]]
# Stack two 2-D arrays
array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[7, 8, 9], [10, 11, 12]])
stacked_array = np.vstack((array1, array2))
print(stacked_array) # [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]
# Stack a 1-D and a 2-D array
array1 = np.array([1, 2, 3])
array2 = np.array([[4, 5, 6], [7, 8, 9]])
stacked_array = np.vstack((array1, array2))
print(stacked_array) # [[1 2 3]
# [4 5 6]
# [7 8 9]]
In the first example, we stack two 1-D arrays to create a 2-D array.
In the second example, we stack two 2-D arrays to create a new 2-D array.
In the third example, we stack a 1-D array and a 2-D array to create a new 2-D array.
In all three examples, the numpy.vstack()
function is used to stack the arrays in a vertical direction.
Related tutorials curated for you
How to use numpy.clip() in Python
How to use numpy.vstack() in Python
How to use numpy.exp() in Python
How to use numpy.zeros() in Python
ModuleNotFoundError: No module named 'numpy' in Python
How to use numpy.round() in Python
How to use numpy.percentile() in Python
How to use numpy.linspace() in Python
What does numpy.arrange() do?
How to use numpy.ndarray.flatten() in Python
How to use numpy.ravel() in Python
NumPy: np.arrange()