numpy.pad() in Python

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.

Syntax:

snippet
numpy.pad(array, pad_width, mode='constant', **kwargs)

Parameters:

array: array_like

This is the source array which we want to pad.

pad_width: int, sequence, or array_like

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.

mode: str or function(optional)

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.

stat_length: int or sequence(optional)

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.

constant_values: scalar or sequence(optional)

This parameter is used in 'constant'. It defines the values for setting the padded values to each axis.

end_values: scalar or sequence(optional)

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.

reflect_type: even or odd(optional)

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.

Returns:

pad: ndarray

This function returns the padded array of rank equal to the array, whose shape increase according to pad_width.

Example 1:

snippet
import numpy as np
x = [1, 3, 2, 5, 4]
y = np.pad(x, (3, 2), 'constant', constant_values=(6, 4))
y

Output:

Output
array([6, 6, 6, 1, 3, 2, 5, 4, 4, 4])

In the above code

  • We have imported numpy with alias name np.
  • We have created a list of values x.
  • We have declared the variable y and assigned the returned value of np.pad() function.
  • We have passed the list x, pad_width, set the mode to constant and constant_values in the function.
  • Lastly, we tried to print the value of y.

In the output, it shows a ndarray padded with the defined size and values.

Example 2:

snippet
import numpy as np
x = [1, 3, 2, 5, 4]
y = np.pad(x, (3, 2), 'edge')
y

Output:

Output
array([1, 1, 1, 1, 3, 2, 5, 4, 4, 4])

Example 3:

snippet
import numpy as np
x = [1, 3, 2, 5, 4]
y = np.pad(x, (3, 2), 'linear_ramp', end_values=(-4, 5))
y

Output:

Output
array([-4, -2, 0, 1, 3, 2, 5, 4, 4, 5])

Example 4:

snippet
import numpy as np
x = [1, 3, 2, 5, 4]
y = np.pad(x, (3,), 'maximum')
y

Output:

Output
array([5, 5, 5, 1, 3, 2, 5, 4, 5, 5, 5])

Example 5:

snippet
import numpy as np
x = [1, 3, 2, 5, 4]
y = np.pad(x, (3,), 'mean')
y

Output:

Output
array([3, 3, 3, 1, 3, 2, 5, 4, 3, 3, 3])

Example 6:

snippet
import numpy as np
x = [1, 3, 2, 5, 4]
y = np.pad(x, (3,), 'median')
y

Output:

Output
array([3, 3, 3, 1, 3, 2, 5, 4, 3, 3, 3])

Example 7:

snippet
import numpy as np
a = [[1, 2], [3, 4]]
y = np.pad(x, (3,), 'minimum')
y

Output:

Output
array([[1, 1, 1, 1, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1], [3, 3, 3, 3, 4, 3, 3], [1, 1, 1, 1, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1]])

Example 8:

snippet
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:

Output
array([41, 31, 21, 11, 21, 31, 41, 51, 41, 31])

In the above code

  • We have imported numpy with alias name np.
  • We have created a function pad_with with vector, pad_width, iaxis, and kwargs.
  • We have declared the variable pad_value to get padding values from the get() function.
  • We have passed the padding values to the part of the vector.
  • We have created an array x using np.arange() function and changed the shape using the reshape() function.
  • We have declared a variable y and assigned the returned value of the np.pad() function.
  • We have passed the list x and pad_width in the function
  • Lastly, we tried to print the value of y.

In the output, it shows a ndarray padded with the defined size and values.

Example 9:

snippet
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:

Output
array([[10, 10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 0, 1, 10, 10, 10], [10, 10, 10, 2, 3, 10, 10, 10], [10, 10, 10, 4, 5, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10, 10]])

Example 10:

snippet
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:

Output
array([[100, 100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 0, 1, 100, 100, 100], [100, 100, 100, 2, 3, 100, 100, 100], [100, 100, 100, 4, 5, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100, 100]])
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +