Numpy pad() Function



The Numpy pad() function is used to pad an array with values along its edges. Padding is typically done to increase the size of an array by adding values (such as zeros or a constant) around its edges. The function provides flexibility in specifying the padding width, the mode of padding, and custom values.

Padding can be applied in various ways by setting the mode parameter to options such as constant, edge, symmetric, reflect, and more, depending on the desired behavior.

Syntax

Following is the syntax of the Numpy pad() function −

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

Parameters

Following are the parameters of the Numpy pad() function −

  • array - Input array to be padded.
  • pad_width - Number of values padded to the edges of each axis. It can be an integer, tuple, or sequence of tuples.
  • mode(optional) - Specifies the method of padding. Default is constant. Available modes include constant, edge, linear_ramp, reflect, symmetric, etc.
  • kwargs(optional) - Additional keyword arguments depending on the padding mode, such as constant_values for constant padding.

Return Values

This function returns a padded array with the same or modified shape based on the specified padding.

Example

Following is a basic example to pad a numpy array using Numpy pad() function −

import numpy as np
array = np.array([1, 2, 3])
padded_array = np.pad(array, pad_width=2, mode='constant')
print("Padded Array:", padded_array)

Output

Following is the output of the above code −

Padded Array: [0 0 1 2 3 0 0]

Example: Padding with Different Modes

Using numpy.pad(), we can pad an array with different modes such as edge, reflect, or symmetric. In the following example, we have padded an array using the edge mode, which replicated on the edges −

import numpy as np
array = np.array([1, 2, 3])
print("Original Array:",array)
padded_array = np.pad(array, pad_width=2, mode='edge')
print("Edge Padded Array:", padded_array)

Output

Following is the output of the above code −

Original Array: [1 2 3]
Edge Padded Array: [1 1 1 2 3 3 3]

Example: Padding Multi-dimensional Arrays

The numpy.pad() function can also pad multi-dimensional arrays along specified axes with different padding widths. Here, a 2D array is padded as follows −

  • (1, 1): specifies padding for the rows, where 1 row is added above and 1 row below the original array, filled with 0s.
  • (2,2): specifies padding for the columns, where 2 columns are added to the left and 2 columns to the right of the original array, also filled with 0s.
import numpy as np
array = np.array([[1, 2], [3, 4]])
padded_array = np.pad(array, pad_width=((1, 1), (2, 2)), mode='constant', constant_values=0)
print("2D Padded Array:\n", padded_array)

Output

Following is the output of the above code −

2D Padded Array:
 [[0 0 0 0 0 0]
  [0 0 1 2 0 0]
  [0 0 3 4 0 0]
  [0 0 0 0 0 0]]

Example: Custom Padding with Constant Values

Using numpy.pad(), we can customize the padding values in constant mode by using the constant_values parameter. Here, a 1D array is padded with the constant value 9 on both sides, repeated 3 times −

import numpy as np
array = np.array([5, 6, 7])
padded_array = np.pad(array, pad_width=3, mode='constant', constant_values=9)
print("Constant Padded Array:", padded_array)

Output

Following is the output of the above code −

Constant Padded Array: [9 9 5 6 7 9 9]
numpy_array_manipulation.htm
Advertisements