The numpy module of Python provides a function called numpy.pad() to perform padding in the array. This function has several required and optional parameters.
numpy.pad(array, pad_width, mode='constant', **kwargs)
This is the source array which we want to pad.
This parameter defines the number of values that are padded to the edges of each axis. The unique pad widths for each axis are defined as (before_1, after_1), (before_2, after_2), ... (before_N, after_N)). For each axis, ((before, after),) will be treated as same as before and after pad. For all axes, the int, or (pad,) is a shortcut to before = after = pad width.
This parameter has one of the following string values:
'constant'(Default)
If we assign a constant value to the mode parameter, padding will be done with a constant value.
'edge'
It is the edge value of the array. The padding will be done with this edge value.
'linear_ramp'
This value is used to perform padding with the linear ramp between the edge value and the end value.
'maximum'
This parameter value performs padding by using the max value of a vector part or all, along each axis.
'mean'
This parameter value performs padding via the mean value of a vector part or all, along each axis.
'median'
This parameter value performs padding via the median value of a vector part or all, along each axis.
'minimum'
This parameter value performs padding via the min value of a vector part or all, along each axis.
'reflect'
This value pads the array via vector reflection, which is mirrored on the starting and ending vector values, along each axis.
'symmetric'
This value is used to pad the array via vector reflection, which is mirrored along the edge of the array.
'wrap'
This value is used to perform padding of the array via the wrap of the vector along the axis. The starting values are used for padding the end, and the ending values pad the beginning.
'empty'
This value is used to pad the array with undefined values.
This parameter is used in 'maximum', 'minimum', 'mean', 'median'. It defines the number of values at each edge axis, used for calculating the static value.
This parameter is used in 'constant'. It defines the values for setting the padded values to each axis.
This parameter is used in 'linear_ramp'. It defines the values which are used for the last value of the linear_ramp and will form the edge of the padded array.
This parameter is used in 'symmetric' and 'reflect'. By default, the reflect_type is 'even' with an unaltered reflection around the edge value. By subtracting the reflected values from two times the edge value, the array's extended part is created for the 'odd' style.
pad: ndarray
This function returns the padded array of rank equal to the array, whose shape increase according to pad_width.
import numpy as np x = [1, 3, 2, 5, 4] y = np.pad(x, (3, 2), 'constant', constant_values=(6, 4)) y
Output:
In the above code
In the output, it shows a ndarray padded with the defined size and values.
import numpy as np x = [1, 3, 2, 5, 4] y = np.pad(x, (3, 2), 'edge') y
Output:
import numpy as np x = [1, 3, 2, 5, 4] y = np.pad(x, (3, 2), 'linear_ramp', end_values=(-4, 5)) y
Output:
import numpy as np x = [1, 3, 2, 5, 4] y = np.pad(x, (3,), 'maximum') y
Output:
import numpy as np x = [1, 3, 2, 5, 4] y = np.pad(x, (3,), 'mean') y
Output:
import numpy as np x = [1, 3, 2, 5, 4] y = np.pad(x, (3,), 'median') y
Output:
import numpy as np a = [[1, 2], [3, 4]] y = np.pad(x, (3,), 'minimum') y
Output:
import numpy as np def pad_with(vector, pad_width, iaxis, kwargs): padding_value = kwargs.get('padder', 10) vector[:pad_width[0]] = padding_value vector[-pad_width[1]:] = padding_value x = np.arange(6) x = x.reshape((3, 2)) y = np.pad(x, 3, pad_with) y
Output:
In the above code
In the output, it shows a ndarray padded with the defined size and values.
import numpy as np import numpy as np def pad_with(vector, pad_width, iaxis, kwargs): padding_value = kwargs.get('padder', 10) vector[:pad_width[0]] = padding_value vector[-pad_width[1]:] = padding_value x = np.arange(6) x = x.reshape((3, 2)) np.pad(x, 3, pad_with)
Output:
import numpy as np import numpy as np def pad_with(vector, pad_width, iaxis, kwargs): ... pad_value = kwargs.get('padder', 10) ... vector[:pad_width[0]] = pad_value ... vector[-pad_width[1]:] = pad_value x = np.arange(6) x = x.reshape((3, 2)) np.pad(x, 3, pad_with, padder=100)
Output: