Numpy.vstack() Method in Python:
Numpy.vstack() is a Python function that concatenates a tuple of arrays vertically along the first dimension to create a single array.
To build a single array, it stacks the sequence of input arrays vertically.
Syntax:
numpy.vstack(tuple)
Parameters
tuple: It is the sequence of ndarrays that we want to concatenate. Except for the first axis, the arrays must have the same shape along all axis.
Return Value:
It returns a stacked ndarray i.e, the stacked array of the input arrays.
- Python NumPy hstack() Function
- Python NumPy broadcast() Function
- Python numpy.log() Function with Examples
Numpy vstack() Method with Examples in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Give the first array as static input using the array() function and store it in a variable.
- Give the second array as static input using the array() function and store it in another variable.
- Give the third array as static input using the array() function and store it in another variable.
- Print the dimensions/shape of the above given 3 arrays using the shape function.
- Pass the above given 3 arrays as arguments to the vstack() function to get a vertically stacked 2-D array.
- Store it in another variable.
- Print the shape of the above stack array using the shape function.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword
import numpy
# Give the first array as static input using the array() function
# and store it in a variable
gvn_arry1= numpy.array([4, 6, 1, 9])
# Give the second array as static input using the array() function and
# store it in another variable
gvn_arry2 = numpy.array([2, 3, 1, 14])
# Give the third array as static input using the array() function and
# store it in another variable
gvn_arry3 = numpy.array([8, 5, 4, 9])
# Print the dimensions/shape of the above given 3 arrays using the shape function
print("Given First array shape is:", gvn_arry1.shape)
print("Given second array shape is:", gvn_arry2.shape)
print("Given third array shape is:", gvn_arry3.shape)
print()
# Pass the above given 3 arrays as arguments to the vstack() function
# to get a vertically stacked 2-D array.
# store it in another variable
stack_arry = numpy.vstack((gvn_arry1, gvn_arry2, gvn_arry3))
print("The Stacked array for the above given 3 arrays is:")
print(stack_arry)
# Print the shape of the above stack array using the shape function
print("The result stacked array shape is:", stack_arry.shape)
Output:
Given First array shape is: (4,) Given second array shape is: (4,) Given third array shape is: (4,) The Stacked array for the above given 3 arrays is: [[ 4 6 1 9] [ 2 3 1 14] [ 8 5 4 9]] The result stacked array shape is: (3, 4)
Example2: The arrays are stacked along the first dimensions for N-Dimensional arrays
# Import numpy module using the import keyword
import numpy
# Give the first array(2D) as static input using the array() function
# and store it in a variable
gvn_arry1= numpy.array([ [10, 5, 1], [2, 7, 3] ])
# Give the second array(2D) as static input using the array() function and
# store it in another variable
gvn_arry2 = numpy.array([ [4, 1, 6], [1, 8, 9] ])
# Print the dimensions/shape of the above given two 2D arrays using the shape function
print("Given First array shape is:", gvn_arry1.shape)
print("Given second array shape is:", gvn_arry2.shape)
print()
# Pass the above given 2 arrays as arguments to the vstack() function
# to get a vertically stacked array.
# store it in another variable
stack_arry = numpy.vstack((gvn_arry1, gvn_arry2))
print("The Stacked array for the above given two 2D arrays is:")
print(stack_arry)
# Print the shape of the above stack array using the shape function
print("The result stacked array shape is:", stack_arry.shape)
Output:
Given First array shape is: (2, 3) Given second array shape is: (2, 3) The Stacked array for the above given two 2D arrays is: [[10 5 1] [ 2 7 3] [ 4 1 6] [ 1 8 9]] The result stacked array shape is: (4, 3)
The shape of N-dimensional arrays must be the same along all dimensions except the first, as seen below:
Example3
# Import numpy module using the import keyword
import numpy
# Give the first array as static input using the array() function
# and store it in a variable
gvn_arry1= numpy.array([ [10, 5], [2, 7] ])
# Give the second array as static input using the array() function and
# store it in another variable
gvn_arry2 = numpy.array([ [4, 1], [1, 9], [6, 5]])
# Print the dimensions/shape of the above given two arrays using the shape function
print("Given First array shape is:", gvn_arry1.shape)
print("Given second array shape is:", gvn_arry2.shape)
print()
# Pass the above given 2 arrays as arguments to the vstack() function
# to get a vertically stacked array.
# store it in another variable
stack_arry = numpy.vstack((gvn_arry1, gvn_arry2))
print("The Stacked array for the above given two arrays is:")
print(stack_arry)
# Print the shape of the above stack array using the shape function
print("The result stacked array shape is:", stack_arry.shape)
Output:
Given First array shape is: (2, 2) Given second array shape is: (3, 2) The Stacked array for the above given two arrays is: [[10 5] [ 2 7] [ 4 1] [ 1 9] [ 6 5]] The result stacked array shape is: (5, 2)