The pad() method adds a specified value around the array axis to achieve a desired length.
pad() Syntax
The syntax of pad() is:
numpy.pad(array, pad_width, mode = 'constant', **kwargs)
pad() Arguments
The pad() method takes multiple arguments:
array- array to padpad_width- number of values to pad on each axismode(optional) - determines how the values are padded**kwargs(optional) - additional keyword arguments that can be used
pad() Return Value
The pad() method returns a new array with padded values on the borders of the original array.
Pad a 2-D Array
Output
Array padded by width 1 on all sides: [[0 0 0 0] [0 1 2 0] [0 3 4 0] [0 0 0 0]] Array padded by width (1,2): [[0 0 0 0 0] [0 1 2 0 0] [0 3 4 0 0] [0 0 0 0 0] [0 0 0 0 0]] Array padded by width (1,2), (2,1): [[0 0 0 0 0] [0 0 1 2 0] [0 0 3 4 0] [0 0 0 0 0] [0 0 0 0 0]]
Mode Argument in pad()
The mode is an optional argument in pad() that specifies the pattern in which the array elements are padded.
The mode can be:
'constant': pads with a constant value'edge': pads with the nearest edge value'linear_ramp': pads with a linear ramp between the edge value and the end value'maximum': pads with the maximum value of the input array'mean': pads with the mean value of the input array'median': pads with the median value of the input array'minimum': pads with the minimum value of the input array'reflect': pads by mirroring the values of the array'symmetric': pads by mirroring the values, including the boundary values'wrap': pads with a circular wrap of the array'empty': pads with undefined values
The **kwargs parameter allows you to pass additional keyword arguments specific to the chosen padding mode. Different modes have different optional arguments specified by **kwargs to customize their behavior.
Example 1: Padding With 'constant' Mode
In numpy.pad(), we can use the 'constant' mode to set the padding value. It takes an additional argument constant_values.
Let's see an example.
Output
[-1 1 2 3 4 -1]
Notes:
'constant'is the default mode.- If
constant_valuesis not specified, 0 is used by default.
Example 2: Padding With 'edge' Mode
In numpy.pad(), 'edge' mode extends the values at the edges of the array outward.
Output
[1 1 2 3 4 4]
Example 3: Padding With 'linear_ramp' Mode
In numpy.pad(), 'linear_ramp' specifies the starting and ending padding values. It supports one additional argument end_values.
Output
Array padded with linear ramp: [ 4 1 2 3 4 -1] Array padded with linear ramp and wider padding: [ 4 2 1 2 3 4 1 -1]
Note: The linear_ramp mode requires the specified end_values to have the same dtype as the array.
Example 4: Padding With 'maximum' and 'minimum' Mode
In numpy.pad(), 'maximum' mode pads the input array with the maximum value of the input array whereas 'minimum' mode pads with the minimum value. They support one additional argument stat_length.
stat_length specifies the number of values at the edge of each axis used to calculate the statistic value. It is None by default and uses the entire array.
Example 5: Padding With 'mean' and 'median' Mode
In numpy.pad(), 'mean' mode pads the input array with the mean value of the input array whereas 'median' mode pads the input array with the median value of the input array. They support one additional argument stat_length.
stat_length specifies the number of values at the edge of each axis used to calculate the statistic value. It is None by default and uses the entire array.
import numpy as np
# create an 1D array
array1 = np.array([1, 3, 5, 9, 11])
# pad array with the mean value from the whole array
array2 = np.pad(array1, pad_width = 1, mode = 'mean')
print(array2) # [ 6 1 3 5 9 11 6]
# pad array with the median value from the whole array
array3 = np.pad(array1, pad_width = 1, mode = 'median')
print(array3) # [ 5 1 3 5 9 11 5]
# pad array with the mean value from 3 adjacent edge values
array4 = np.pad(array1, pad_width = 1, mode = 'mean',stat_length = 3)
print(array4) # [ 3 1 3 5 9 11 8]
# pad array with the median value from 3 adjacent edge values
array5 = np.pad(array1, pad_width = 1, mode = 'median',stat_length = 3)
print(array5) # [ 3 1 3 5 9 11 9]
Example 6: Padding With reflect and symmetric mode
In numpy.pad(), 'reflect' mode pads the input array by mirroring the values of the array, and 'symmetric' mode pads by mirroring the values of the array including the boundary values.
They support one additional argument reflect_type.
reflect_type can be even or odd:
even(default): padding is done by using the mirrored valueodd: padding is mirrored is done by using 2 * edge value - mirrored value
Output
Array padded with reflect mode and even reflect_type: [ 5 3 1 3 5 9 11 9 5] Array padded with reflect mode and odd reflect_type: [-3 -1 1 3 5 9 11 13 17] Array padded with symmetric mode and even reflect_type: [ 3 1 1 3 5 9 11 11 9] Array padded with symmetric mode and odd reflect_type: [-1 1 1 3 5 9 11 11 13]
Example 7: Padding With 'wrap' mode
In numpy.pad(), 'wrap' mode pads the array with a circular wrap of the array. It supports no additional arguments.
Output
Array padded with wrap mode: [4 1 2 3 4 1] Array padded with wrap mode and wider padding: [3 4 1 2 3 4 1 2]
Example 8: Padding With 'empty' mode
In numpy.pad(), 'empty' mode pads the input array using uninitialized values. It supports no additional arguments.
Output
Array padded with empty mode: [ 4253 1 2 3 4 723435939191610164]
Note: The numbers used for padding are not random, they are uninitialized/garbage values.
Using Custom Padding Function as mode
You can define a custom padding function to pad an array. It has the following signature:
padding_func(vector, iaxis_pad_width, iaxis, kwargs)
where
vector: a rank 1 array already padded with zerosiaxis_pad_width: a tuple with 2 elementsiaxis_pad_width[0]is the number of values padded at the beginning of vectoriaxis_pad_width[1]is the number of values padded at the end of vector
iaxis: axis currently being calculatedkwargs: any keyword arguments that the function requires
For example,
Output
Original Array: [1 2 3 4] Padded Array using Custom Padding Function as Mode: [2 2 1 2 3 4 3 3]