Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    NumPyvstack

    In python, numpy.vstack() is a function that helps to stack the input array sequence vertically in order to create a single array. The arrangement will be in row-wise. It is similar to concatenation along the axis 1 after 1-Dimensional arrays of (N) shape have been reshaped to the format (1,N). This function can be used to create arrays with up to 3- 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 the axis 1.

    • Return Value of This Function:

      Return value will be stacked in an array. That is an array that is stacked of the input arrays.

    How does vstack Function Work in NumPy?

    Using the vstack() function, items of arrays are arranged vertically. 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)
    

    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)
    

    Unlike program 1,in this program, two arrays are created with different elements and they are arranged vertically using the vstack function.

    Example #3

    Python program to arrange two arrays given as input by the user where array 1 is input and array 2 is already in the program.

    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)
    

    In this program, the first array is given as input by the user, and the second array is already available in the program. Then, it is arranged vertically using the function vstack().

    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)
    

    In this program, both the first array and second array is given as input by the user. Then, it is arranged vertically using the function vstack().

    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)
    

    In this program, multiple arrays are given as input by the user. Then, it is arranged vertically using the function vstack().


     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.