How to Use NumPy pad() in Python

NumPy pad() function in Python is used to pad the Numpy arrays on left and right . The padding is a process in which the array is altered with some values at the beginning and end. This function has several required and optional parameters. To alter the array with padded values it can change its shape and size.

In this article, I will explain NumPy pad() function syntax, using different mode parameters, and how to pad the array along with pad_width.

1. Quick Examples of Python NumPy pad() Function

If you are in a hurry, below are some quick examples of how to Python the NumPy pad function. For more basic information about NumPy refer NumPy Tutorial.


# Below are the quick examples

# Example 1: padding array using Constant mode
arr2 = np.pad(arr, (2, 3), 'constant', constant_values=(8,25)) 

# Example 2: padding array using 'maximum' mode
arr2 = np.pad(arr, (4,), 'maximum')

# Example 3: padding array using 'linear_ramp' mode
arr2 = np.pad(arr, (3, 2), 'linear_ramp', end_values=(-6, 7))

# Example 4: padding array using 'edge' mode
arr2 = np.pad(arr, (2, 3), 'edge')

# Example 5: padding array using 'mean' mode
arr2 = np.pad(arr, (4,), 'mean')

# Example 6: padding array using 'median' mode
arr2 = np.pad(arr, (4,), 'median )

# Example 7: padding array using 'reflect' mode
arr2 = np.pad(arr, (3, 2), 'reflect')

# Example 8: padding array using 'symmetric' mode
arr2 = np.pad(arr, (3, 2), 'symmetric')

# Example 9: padding array using 'minimum' mode
arr2 = np.pad(arr, (2, 3), 'minimum') 

2. Syntax of Use NumPy pad() function

Following is the syntax of pad() function.


# Syntax of pad()
numpy.pad(arr, pad_width, mode='constant', **kwargs)

2.1 Parameters of the pad()

Following are the parameters of pad() function.

  • arr – It is an array in which elements are to be padded.
  • pad_width – It is a combination of the beginning and ending size of values that are padded to the edges of each axis.
  • mode – The mode in which the function should pad the values to the array. Some of the modes are. 
    • ‘constant’ (default) -It Pads with a constant value.
    • ‘edge’ – It Pads with the edge values of the array.
    • ‘linear_ramp’ – It Pads with the linear ramp between end_value and the array edge value.
    • ‘maximum’ – It Pads with the maximum value of all or part of the vector along each axis.
    • ‘mean’ – It Pads with the mean value of all or part of the vector along each axis.
    • ‘median’ – It Pads with the median value of all or part of the vector along each axis.
    • ‘minimum’ – It Pads with the minimum value of all or part of the vector along each axis.
    • ‘reflect’ – It Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.

2.2 Return Value of pad()

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

3. Usage of NumPy pad() using ‘constant’ Mode

The np.pad() is a NumPy library function used for padding the arrays and returns the padded array in which elements are padded to each axis. The padded array of rank equal to the array, whose shape increased according to pad_width.

Let’s create NumPy array using numpy.array() then, apply pad() function by taking pad_width (3,2) tuple, this pads 3 elements before the array, and 2 elements are added at the end of the array. We will use constant values (8,0) to pad the array.


import numpy as np
arr = np.array([4, 7, 2, 4, 3])
  
# padding array using Constant mode
arr1 = np.pad(arr, (3, 2), 'constant', constant_values=(8,0))                 
print(arr2)

# Output
# 8 8 8 4 7 2 4 3 0 0]

4. Padding Array Using ‘edge’ Mode

Pass edge mode to the function along with specified pad_width to padd elements with the edge values of the array. In our array 4 and 3 are edge values, so 4 is added 2 times at the beginning and 3 is added 3 times at the end of the array.


# padding array using 'edge' mode
arr2 = np.pad(arr, (2, 3), 'edge')
print(arr2)

# Output
# [4 4 4 7 2 4 3 3 3 3]

5. Use pad() to ‘linear_ramp’ Mode

Use 'linear_ramp' mode to the function along with specified pad_ width(3,2) and end_values(-6,7), it will return the padded array where the padded elements of each axis are the linear ramp between the edge value and the end value. For example,


# padding array using 'linear_ramp' mode
arr1 = np.pad(arr, (3, 2), 'linear_ramp', end_values=(-6, 7))
print(arr1)

# Output
# [-6 -3  0  4  7  2  4  3  5  7]
print(arr2)

6. Padding Array Using ‘maximum’ Mode

Pass 'maximum‘ mode to the function along with pad_width(4,) and get the padded array where the padded elements of both axes are maximum of the given NumPy array. For example,


# padding array using 'maximum' mode
arr2 = np.pad(arr, (4,), 'maximum')
print(arr2)

# Output
# [7 7 7 7 4 7 2 4 3 7 7 7 7]

7. Use NumPy pad() to ‘mean’ Mode

To pass 'mean' mode to the function along with pad_width(4,) and get the padded array where the padded elements of both axes are mean value of given NumPy array.


# padding array using 'mean' mode
arr2 = np.pad(arr, (4,), 'mean')
print(arr2)

# Output
# [4 4 4 4 4 7 2 4 3 4 4 4 4]

8. Use NumPy pad() to ‘median’ Mode

To pass 'median' mode to the function along with pad_width(4,) and get the padded array where the padded values of both axes are median value of the given NumPy array. For example,


# padding array using 'median' mode
arr2 = np.pad(arr, (4,), 'median')
print(arr2)

# Output
# [4 4 4 4 4 7 2 4 3 4 4 4 4]

9. Padding NumPy Array Using ‘minimum’ Mode

To pass 'minimum‘ mode to the function along with pad_width(2,3) and get the padded array where the padded elements of both axes are minimum values of the given NumPy array.


arr = [[4, 6],[7, 9]] 
# padding array using 'minimum' mode
arr2 = np.pad(arr, (2, 3), 'minimum')       
print(arr2)

# Output
# [[4 4 4 6 4 4 4]
# [4 4 4 6 4 4 4]
# [4 4 4 6 4 4 4]
# [7 7 7 9 7 7 7]
# [4 4 4 6 4 4 4]
# [4 4 4 6 4 4 4]
# [4 4 4 6 4 4 4]]

10. Use pad() to ‘symmetric’ Mode

In ‘symmetric’ mode, the value are used to pad the array via vector reflection, which is mirrored along the edge of the array. For example,


# padding array using 'symmetric' mode
arr2 = np.pad(arr, (3, 2), 'symmetric')
print(arr2)

# Output
# [2 7 4 4 7 2 4 3 3 4]

11. Conclusion

In this article, I have explained how to get padded array using numpy.pad() function along with different modes and pad_width with examples.

Happy Learning!!

You May Also Like

References

NumPy pad

Leave a Reply

You are currently viewing How to Use NumPy pad() in Python