Quick Contact

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

    numpy.pad()

    numPy.pad() is function present in the Python language tool pack which primarily is used to perform the padding of an array which has been entered by the user. It is a very necessary functionality and it comes packed with the functionality of performing padding of the arrays entered by the user. To simplify put, padding is transformational process which is aimed at setting a dimension in an array to enable is compensate a new size.

    Syntax for numpy.pad()

    Given below is the syntax which is used for numpy.pad() to be implemented into the python coding language programs:

    Numpy *. *pad (*array *, * *pad *_ *width *, * *mode *= *’constant’ *, ***kwargs *) *

    Parameters

    Below are the respective parameter:

    1. array: array_like of any rank:

      The array entered by the user which has to be padded.

    2. pad_width: {sequence, array_like, int}:

      The total number of values that have to be padded upfront the edges with respect to different axis of the array which is designed uniquely for each of them. The parameter ((before N, after N)) generate specified N padding in front and beyond the values for the specified axis. The parameter before = after = padding can be used as a shortcut for the arrays where padding has to be done requiring same with for all axes.

    3. mode: str or function, optional:

      The following keyword have be used for the parameters ‘mode’.

    4. This can be predefined by the coder or be entered as per the preference of the user.

      • ‘constant’ (default):

        Padding with the value that remain constant.

      • ‘edge’:

        Pads which have the edge values defined for the array.

      • ‘linear *_ *ramp’:

        Pads having the linear ramp in between the edge value & the end value.

      • ‘maximum’:

        Padding has to be done across the array for each axis where the edge value is maximum.

      • ‘mean’:

        Padding has to be done across the array for each axis where the edge value is mean throughout the array.

      • ‘median’:

        Padding has to be done across the array for each axis where the edge value is median throughout the array.

      • ‘minimum’:

        Padding has to be done across the array for each axis where the edge value is minimum.

      • ‘reflect’:

        This is used where padding is needed with respect to the arrays which have mirror-like reflective structure on the last and first value for the vector along the specified axis

      • ‘symmetric’:

        This is used where padding is needed with respect to the arrays which have mirror-like reflective structure alongside the array’s edge.

      • ‘empty’:

        Padding where the values are undefined. This is present in the newer version of Python (1.7.1).

    5. stat *_ *length : int , sequence ; the parameter is optional:

      The default value for the parameter is None which is used throughout all axis. This parameter is used with respect to minimum, maximum, mean and median parameters which aids in calculations with respect to static or non-dynamic values.

    6. constant_values : sequence, scalar, the parameter is optional:

      This parameter is used along side the constant parameter which aids in laying out different pads before or after the arrays automatically for various sets of such data.The default value for constant_values is taken to be 0 unless specifies by the user.

    7. end_values : sequence, scalar, the parameter is optional:

      The parameter is used when the ‘liner_ramp’ parameter is used which enables in padding the edges of the array. The default value for constant_values is taken to be 0 unless specifies by the user.

    8. reflect_type : {‘even’, ‘odd’}, optional:

      The parameter is used when the ‘symmetric’ or ‘reflect’ parameters are used which enables in padding the edges of the array. The odd subjective makes the array’s extended portion to be made by subtraction of the reflected-out values with double the edge value limit.

    9. pad: ndarray:

      The final output is stored in the array which is padded after processing through the various parameters having a differentiated size.

    Examples of numpy.pad()

    The following examples demonstrate the use of pad() in the codes:

    Example #1

    Program which illustrates using numpy.pad() in python language.

    Code:

    importnumpy as n1
    a1 = [10, 30, 20, 50, 40] b1 = n1.pad(a1,(3, 2), 'constant', constant_values = (60, 40))
    print("The output generated for the provided values (using constant_values) is :\n", b1)
    

    Example #2

    Program which illustrates using numpy.pad() in python language.

    Code:

    importnumpy as np
    a1 = [10, 30, 20, 50, 40] b1 = np.pad(a1,(3, 2), 'edge')
    print("The output generated for the provided values (using edge) is:\n", b1)
    

    Example #3

    Code:

    importnumpy as n1
    a1 = [10, 30, 20, 50, 40] b1 = n1.pad(a1,(3, 2), 'linear_ramp', end_values = (-40, 50))
    print("The output generated for the provided values (using linear ramp) is:\n", b1)
    


     

    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.