Updated May 20, 2023
Definition of NumPy vstack
In Python, numpy.vstack() is a function that helps to stack the input array sequence vertically to create a single array. The arrangement will be row-wise. It is equivalent to concatenation along axis 1 after reshaping 1-Dimensional arrays of (N) shape to the format (1, N). This function can generate arrays with up to three dimensions.
Let us see more of this function in the following sessions.
Syntax:
Below is the syntax of numpyvstack function.
numpy.vstack(tup)
Parameters:
- tup: It is a sequence of n arrays. This tuple consists of arrays that have to be stacked. Arrays should have the shape same along all but axis 1.
- Return Value of This Function: The program will stack the return value and the input arrays to form a new array.
How does vstack Function Work in NumPy?
Suppose array 1 has elements [22, 32, 43] and array 2 has elements [546, 55, 556]. The two arrays can be arranged vertically using the function vstack(( arr1 , arr2 ) ), where arr1 and arr2 are array 1 and array 2, respectively.
Examples of NumPy vstack
Let us see some sample programs on the vstack() function using Python.
Example #1
Python program to arrange two arrays vertically using vstack.
Code:
import numpy as np
arr1 = np.array( [ 22 , 32 , 43 ] )
print ("array 1 is : \n", arr1)
arr2 = np.array( [ 546 , 55 , 556 ] )
print ("array 2 is : \n", arr2)
arrout = np.vstack( ( arr1 , arr2 ) )
print ( "arrays arranged vertically :\n ", arrout)
Output:
In this program, two arrays are created, and they are arranged vertically using the vstack function.
Example #2
Python program to arrange two arrays with multiple elements vertically using vstack.
Code:
import numpy as np
arr1 = np.array( [ [ 22 , 32 , 43 ] , [11 , 52 , 67 ] ] )
print ("array 1 is : \n", arr1)
arr2 = np.array( [ [ 546 , 55 , 556 ] , [131 , 252 , 167 ] ] )
print ("array 2 is : \n", arr2)
arrout = np.vstack( ( arr1 , arr2 ) )
print ( "arrays arranged vertically :\n ", arrout)
Output:
Unlike program 1, this program creates two arrays with different components and arranges them vertically using the vstack function.
Example #3
Here’s a Python program that takes input from the user for the first array and arranges it together with a predefined second array:
Code:
import numpy as np
# create an empty list
li = []
n = int(input("Enter the no. of elements to be given as input: "))
for it in range(0, n):
items = int(input())
li.append(items)
print ("array 1 is : \n", li)
arr2 = np.array( [ 546 , 55 , 556 ] )
print ("array 2 is : \n", arr2)
# arranging two arrays vertically
arrout = np.vstack( ( li , arr2 ) )
print ( "arrays arranged vertically :\n ", arrout)
Output:
Example #4
Python program to arrange two arrays given as input by the user.
Code:
import numpy as np
# array 1 creation
li = []
n = int(input("Enter the no. of elements to be given as input to array 1: "))
for it in range(0, n):
items = int(input())
li.append(items)
print ("array 1 is : \n", li)
li2 = []
# no: of elements to be given as input
nn = int(input("Enter the no. of elements to be given as input to array 2: "))
# iterate the loop till the given range
for itr in range(0, nn):
itemss = int(input())
li2.append(itemss)
print ("array 2 is : \n", li2)
# arranging two arrays vertically
arrout = np.vstack( ( li , li2 ) )
print ( "arrays arranged vertically :\n ", arrout)
Output:
In this program, the user is prompted to provide input for both the first and second arrays. These arrays are then vertically stacked using the vstack() function.
Example #5
Python program to arrange multiple arrays given as input by the user.
Code:
import numpy as np
# array 1 creation
# create an empty list
li = []
# no: of elements to be given as input
n = int(input("Enter the no. of elements to be given as input to array 1: "))
# iterate the loop till the given range
for it in range(0, n):
items = int(input())
li.append(items)
print ("array 1 is : \n", li)
# create an empty list
li2 = []
# no: of elements to be given as input
nn = int(input("Enter the no. of elements to be given as input to array 2: "))
# iterate the loop till the given range
for itr in range(0, nn):
itemss = int(input())
li2.append(itemss)
print ("array 2 is : \n", li2)
# create an empty list
li3 = []
# no: of elements to be given as input
no = int(input("Enter the no. of elements to be given as input to array 3: "))
# iterate the loop till the given range
for itrt in range(0, no):
itemms = int(input())
li3.append(itemms)
print ("array 3 is : \n", li3)
# arranging two arrays vertically
arrout = np.vstack( ( li , li2, li3 ) )
print ( "arrays arranged vertically :\n ", arrout)
Output:
The user is asked to give numerous arrays as input in this program. The vstack() function is then used to stack these input arrays vertically.
Conclusion
numpy.vstack() is a function that helps to stack the input array sequence vertically in order to create a single array. This article provides a detailed explanation of the vstack function in Python. It covers various aspects, including the syntax, working principles, and examples.
Recommended Articles
We hope that this EDUCBA information on “NumPy vstack” was beneficial to you. You can view EDUCBA’s recommended articles for more information.