numpy.pad() function in Python (original) (raw)
Last Updated : 01 Oct, 2020
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
import
numpy as np
arr
=
[
1
,
3
,
2
,
5
,
4
]
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
import
numpy as np
arr
=
[
1
,
3
,
2
,
5
,
4
]
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
import
numpy as np
arr
=
[
1
,
3
,
9
,
5
,
4
]
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
import
numpy as np
arr
=
[[
1
,
3
],[
5
,
8
]]
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]]
Similar Reads
- numpy.i0() function | Python numpy.i0() function is the modified Bessel function of the first kind, order 0. it's usually denoted by I0. Syntax : numpy.i0(x) Parameters : x : [array_like, dtype float or complex] Argument of the Bessel function. Return : [ndarray, shape = x.shape, dtype = x.dtype] The modified Bessel function ev 1 min read
- numpy.info() function in Python In Numpy we can get all the information about the function, class, or module like what will the parameter and what will be the type of the return value with the help of numpy.info() function. This function returns the help information for a function, class, or module. Syntax: numpy.info(numpy.info(o 1 min read
- numpy.outer() function - Python numpy.outer() function compute the outer product of two vectors. Syntax : numpy.outer(a, b, out = None) Parameters : a : [array_like] First input vector. Input is flattened if not already 1-dimensional. b : [array_like] Second input vector. Input is flattened if not already 1-dimensional. out : [nda 1 min read
- 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 : # Python program explaining # numpy.matrix.A() function # importing numpy as geek import numpy as geek mat = geek.matrix(geek.arange(9).resh 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.char.add() function in Python The add() method of the char class in the NumPy module is used for element-wise string concatenation for two arrays of str or unicode. numpy.char.add()Syntax : numpy.char.add(x1, x2)Parameters : x1 : first array to be concatenated (concatenated at the beginning)x2 : second array to be concatenated ( 2 min read
- numpy.who function - Python numpy.who() function print the NumPy arrays in the given dictionary. Syntax : numpy.who(vardict = None) Parameters : vardict : [dict, optional] A dictionary possibly containing ndarrays. Return : Returns ‘None’. If there is no dictionary passed in or vardict is None then returns NumPy arrays in the 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.fix() in Python The numpy.fix() is a mathematical function that rounds elements of the array to the nearest integer towards zero. The rounded values are returned as floats. Syntax : numpy.fix(a, b = None) Parameters : a : [array_like] Input array to be floated. b : [ndarray, optional] Output array. Return : The arr 2 min read