Updated March 31, 2023
Introduction to 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 and parameter
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’.
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).
4. 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.
5. 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.
6. 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.
7. 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.
8. 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:
import numpy 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)
Output:
Example #2
Program which illustrates using numpy.pad() in python language.
Code:
import numpy 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)
Output:
Example #3
Code:
import numpy 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)
Output:
Conclusion
The pad() function is very vital for specific coding and turns to be widely used tool. The function enables the transformation of the array in order to reduce the numerous conflicts occurring with the memory system. The numpy.pad() function is widely applied to the AST in full functionality. This enable the user to specify the new size or also give the system the option to automatically compute the size for the same. This thereby specialized the memory resource optimization ability to reduce the processing time for the system.
Recommended Articles
This is a guide to numpy.pad(). Here we discuss the introduction to numpy.pad() with appropriate syntax, parameters and respective examples. You may also have a look at the following articles to learn more –