numpy.pad() function in Python Last Updated : 01 Oct, 2020 Comments Improve Suggest changes Like Article Like Report numpy.pad() function is used to pad the Numpy arrays. Sometimes there is a need to perform padding in Numpy arrays, then numPy.pad() function is used. The function returns the padded array of rank equal to the given array and the shape will increase according to pad_width. Syntax: numpy.pad(array, pad_width, mode='constant', **kwargs) Parameters : array: the array to pad pad_width: This parameter defines the number of values that are padded to the edges of each axis. mode : str or function(optional) **kwargs: allows you to pass keyword variable length of argument to a function. It is used when we want to handle the named argument in a function. Return: A padded array of rank equal to an array with shape increased according to pad_width. Example 1: Python3 # Python program to explain # working of numpy.pad() function import numpy as np arr = [1, 3, 2, 5, 4] # padding array using CONSTANT mode pad_arr = np.pad(arr, (3, 2), 'constant', constant_values=(6, 4)) print(pad_arr) Output: [6 6 6 1 3 2 5 4 4 4] Example 2: Python3 # Python program to explain # working of numpy.pad() function import numpy as np arr = [1, 3, 2, 5, 4] # padding array using 'linear_ramp' mode pad_arr = np.pad(arr, (3, 2), 'linear_ramp', end_values=(-4, 5)) print(pad_arr) Output: [-4 -2 -1 1 3 2 5 4 4 5] Example 3: Python3 # Python program to explain # working of numpy.pad() function import numpy as np arr = [1, 3, 9, 5, 4] # padding array using 'maximum' mode pad_arr = np.pad(arr, (3,), 'maximum') print(pad_arr) Output: [9 9 9 1 3 9 5 4 9 9 9] Example 4: Python3 # Python program to explain # working of numpy.pad() function import numpy as np arr = [[1, 3],[5, 8]] # padding array using 'minimum' mode pad_arr = np.pad(arr, (3,), 'minimum') print(pad_arr) Output: [[1 1 1 1 3 1 1 1] [1 1 1 1 3 1 1 1] [1 1 1 1 3 1 1 1] [1 1 1 1 3 1 1 1] [5 5 5 5 8 5 5 5] [1 1 1 1 3 1 1 1] [1 1 1 1 3 1 1 1] [1 1 1 1 3 1 1 1]] Comment More infoAdvertise with us Next Article numpy.pad() function in Python vanshgaur14866 Follow Improve Article Tags : Python Python-numpy Python numpy-arrayCreation Practice Tags : python Similar Reads numpy.matrix.A() function - Python numpy.matrix.A() function return self as an ndarray object. Syntax : numpy.matrix.A() Parameters : None Return : [ndarray] Return self as an ndarray. Code #1 : Python3 # Python program explaining # numpy.matrix.A() function # importing numpy as geek import numpy as geek mat = geek.matrix(geek.arange 1 min read numpy.mintypecode() function â Python numpy.mintypecode() function return the character for the minimum-size type to which given types can be safely cast. Syntax : numpy.mintypecode(typechars, typeset = 'GDFgdf', default = 'd') Parameters : typechars : [list of str or array_like] If a list of strings, each string should represent a dtyp 1 min read Numpy size() function | Python numpy.size() function in Python is used to count the number of elements in a NumPy array. You can use it to get the total count of all elements, or to count elements along a specific axis, such as rows or columns in a multidimensional array. This makes it useful when quickly trying to understand the 2 min read numpy.mask_indices() function | Python numpy.mask_indices() function return the indices to access (n, n) arrays, given a masking function. Syntax : numpy.mask_indices(n, mask_func, k = 0) Parameters : n : [int] The returned indices will be valid to access arrays of shape (n, n). mask_func : [callable] A function whose call signature is s 1 min read numpy.ma.filled() function - Python numpy.ma.filled() function return input as an array with masked data replaced by a fill value. If arr is not a MaskedArray, arr itself is returned. If arr is a MaskedArray and fill_value is None, fill_value is set to arr.fill_value. Syntax : numpy.ma.filled(arr, fill_value = None) Parameters : arr : 1 min read numpy.full() in Python numpy.full(shape, fill_value, dtype = None, order = 'C') : Return a new array with the same shape and type as a given array filled with a fill_value. Parameters : shape : Number of rows order : C_contiguous or F_contiguous dtype : [optional, float(by Default)] Data type of returned array. fill_value 1 min read numpy.matlib.zeros() function | Python numpy.matlib.zeros() function return matrix of given shape and type, filled with zeros. Syntax : numpy.matlib.zeros(shape, dtype = None, order = 'C') Parameters : shape : [sequence of ints] Shape of the empty matrix. dtype : [data-type, optional] The desired data-type for the matrix, default is floa 1 min read numpy.frompyfunc() in Python numpy.frompyfunc(func, nin, nout) function allows to create an arbitrary Python function as Numpy ufunc (universal function). Parameters: func: [A python function object ] An arbitrary python function nin: [int] Number of input arguments to that function. nout: [int] Number of objects returned by th 2 min read numpy.matlib.empty() function | Python numpy.matlib.empty() function return a new matrix of given shape and type, without initializing entries. Syntax : numpy.matlib.empty(shape, dtype = None, order = 'C') Parameters : shape : [int or tuple of int] Shape of the empty matrix. dtype : [data-type, optional] Desired output data-type. order : 1 min read Numpy ndarray.getfield() function | Python numpy.ndarray.getfield() function return a field of the given array as a certain type. Syntax : numpy.ndarray.getfield(dtype, offset=0) Parameters : dtype : [str or dtype] The dtype size of the view can not be larger than that of the array itself. offset : [int] Number of bytes to skip before beginn 1 min read Like