NumPy pad() Function:
Numpy.pad: To pad an array, use the NumPy pad() function. This method has an optional parameter mode that can be used to specify string values (predefined padding style) or a user-supplied padding function.
Syntax:
numpy.pad(array, pad_width, mode='constant')
Parameters
array: This is required. It is an array (rank N) to pad.
pad_width: This is required. It may be a sequence, array_like, int. It defines a number of values padded to each axis edges. ((before_1, after_1), … (before_N, after_N)) unique pad widths for each axis. ((before, after),) gives the same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes.
mode: This is optional. Choose one of the string values or a user-supplied function from the list below:
- ‘constant’: It is default value. Pads having a fixed/constant value.
- ‘edge’: Pads the array’s edge values.
- ‘linear_ramp’: It Pads between end value and array edge value with a linear ramp.
- ‘maximum’: It Pads(Fills the gap) with the maximum/greatest value of all or part of the vector along each axis.
- ‘mean’: This function pads with the mean value of all or part of the vector along each axis.
- ‘median’: The median function pads with the median value of all or part of the vector along each axis.
- ‘minimum’: The minimum function pads with the minimum/smallest value of all or part of the vector along each axis.
- ‘reflect’: The reflect function pads with the vector’s reflection mirrored on the first and last values of each axis.
- ‘symmetric’: The symmetric function pads with the vector’s reflection mirrored along the array’s edge.
- ‘wrap’: The wrap function pads with the vector’s wrap along the axis. The first values are used to pad the end, whereas the last values are used to pad the start.
- ’empty’: The empty function pads with undefined values.
- <function>: It is a padding function.
Return value:
Returns a padded array with rank equal to array and shape increased by pad_width.
- Count occurrences of a value in NumPy array in Python | numpy.count() in Python
- Python NumPy loadtxt() Function
- Python: Count Number of True Elements in Numpy Array in Python
NumPy pad() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Give the list as static input and store it in a variable.
- Pass the given list, pad_width, mode for the pad() function of the numpy module(here we passed mode as ‘constant’ by passing some random constant values) and print the result.
- Pass the given list, pad_width, mode for the pad() function of the numpy module (here we passed mode as ‘edge’ to pad with edge value) and print the result.
- Pass the given list, pad_width, mode for the pad() function of the numpy module (here we passed mode as ‘maximum’ to pad with maximum value) and print the result.
- Pass the given list, pad_width, mode for the pad() function of the numpy module (here we passed mode as ‘minimum’ to pad with minimum value) and print the result.
- Pass the given list, pad_width, mode for the pad() function of the numpy module (here we passed mode as ‘mean’ to pad with mean value) and print the result.
- Pass the given list, pad_width, mode, end_values for the pad() function of the numpy module (here we passed mode as ‘linear_ramp’ to pad with linear ramp values) and print the result.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Give the list as static input and store it in a variable. gvn_lst = [25, 35] # Pass the given list, pad_width, mode for the pad() function of the numpy module # (here we passed mode as 'constant' by passing some random constant values) # and print the result. print("Pad the given list with the given constant values:") print(np.pad(gvn_lst, (4,5), 'constant', constant_values=(6, 12))) # Pass the given list, pad_width, mode for the pad() function of the numpy module # (here we passed mode as 'edge' to pad with edge value) # and print the result. print("Pad the given list with the edge value:") print(np.pad(gvn_lst, (4,5), 'edge')) # Pass the given list, pad_width, mode for the pad() function of the numpy module # (here we passed mode as 'maximum' to pad with maximum value) # and print the result. print("Pad the given list with the maximum value:") print(np.pad(gvn_lst, (4,5), 'maximum')) # Pass the given list, pad_width, mode for the pad() function of the numpy module # (here we passed mode as 'minimum' to pad with minimum value) # and print the result. print("Pad the given list with the minimum value:") print(np.pad(gvn_lst, (4,5), 'minimum')) # Pass the given list, pad_width, mode for the pad() function of the numpy module # (here we passed mode as 'mean' to pad with mean value) # and print the result. print("Pad the given list with the mean value:") print(np.pad(gvn_lst, (4,5), 'mean')) # Pass the given list, pad_width, mode for the pad() function of the numpy module # (here we passed mode as 'linear_ramp' to pad with linear ramp values) # and print the result. print("Pad the given list with the linear map value:") print(np.pad(gvn_lst, (4,5), 'linear_ramp', end_values=(0, 0)))
Output:
Pad the given list with the given constant values: [ 6 6 6 6 25 35 12 12 12 12 12] Pad the given list with the edge value: [25 25 25 25 25 35 35 35 35 35 35] Pad the given list with the maximum value: [35 35 35 35 25 35 35 35 35 35 35] Pad the given list with the minimum value: [25 25 25 25 25 35 25 25 25 25 25] Pad the given list with the mean value: [30 30 30 30 25 35 30 30 30 30 30] Pad the given list with the linear map value: [ 0 6 12 18 25 35 28 21 14 7 0]
Example2
Approach:
- Import numpy module using the import keyword.
- Give the list(multi-dimensional) as static input and store it in a variable.
- Pass the given list, multi-dimensional padwidth, mode as constant, and some random constant values as the argument to the pad() function of the numpy module and print the result.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Give the list(multi-dimensional)as static input and store it in a variable. gvn_lst = [[50, 60], [70, 80]] # Pass the given list, multi-dimensional padwidth, mode as constant # and some random constant values as the argument to the pad() # function of the numpy module and print the result. print("Pad with the given constant values:") print(np.pad(gvn_lst, ((4,5),(2,3)), 'constant', constant_values=((1, 2),(3, 4))))
Output:
Pad with the given constant values: [[ 3 3 1 1 4 4 4] [ 3 3 1 1 4 4 4] [ 3 3 1 1 4 4 4] [ 3 3 1 1 4 4 4] [ 3 3 50 60 4 4 4] [ 3 3 70 80 4 4 4] [ 3 3 2 2 4 4 4] [ 3 3 2 2 4 4 4] [ 3 3 2 2 4 4 4] [ 3 3 2 2 4 4 4] [ 3 3 2 2 4 4 4]]