• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:20 mins read
You are currently viewing How to Use NumPy pad() in Python

NumPy pad() function in Python is used to pad the Numpy arrays on the 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.

Advertisements

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 pad() Function

If you are in a hurry, below are some quick examples of the NumPy pad() function.


# Quick examples of pad() function

import numpy as np

arr = np.array([4, 7, 2, 4, 3])

# 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, (3, 2), 'minimum') 

2. Syntax of 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 is equal to the array, whose shape increased according to pad_width.

Let’s create NumPy array arr with values [4, 7, 2, 4, 3]. Then, you are padding the array using the ‘constant’ mode. The pad() function is used to add three elements before the array and two elements after the array. The constant value used for padding before the array is 8, and the constant value used for padding after the array is 0.


# Import numpy
import numpy as np

# Create input array
arr = np.array([4, 7, 2, 4, 3])
print("Original array:\n", arr)
  
# Padding array using constant mode
arr2 = np.pad(arr, (3, 2), 'constant', constant_values=(8,0))                  
print("After padding the arrays using constant mode:\n", arr2)

Yields below output.

NumPy pad

In the above function, the original array is padded with three 8s before the elements [4, 7, 2, 4, 3] and two 0s after the elements.

4. Padding Array Using ‘edge’ Mode

In 'edge' mode of numpy.pad(), the values at the edges of the original array are used to pad the array. If you pad the array before or after the original array, the edge values will be used for padding.

In the below example, the original array is padded with two elements before and three elements after the array using the ‘edge’ mode. The edge values [4, 7, 2, 4, 3] are used for padding, resulting in the padded array [4 4 4 7 2 4 3 3 3 3].


import numpy as np

# Create input array
arr = np.array([4, 7, 2, 4, 3])
print("Original array:\n", arr)
  
# Padding array using edge mode
arr2 = np.pad(arr, (2, 3), 'edge')               
print("After padding the arrays using 'edge' mode:\n", arr2)

Yields below output.

NumPy pad

5. Use pad() to ‘linear_ramp’ Mode

Use 'linear_ramp' mode to the function along with the 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
arr2 = np.pad(arr, (3, 2), 'linear_ramp', end_values=(-6, 7))
print("After padding the arrays using 'linear_ramp' mode:\n", arr2)

# Output:
# Original array:
#  [4 7 2 4 3]
# After padding the arrays using 'linear_ramp' mode:
#  [-6 -2  0  4  7  2  4  3  5  7]

6. Padding Array Using ‘maximum’ Mode

In the 'maximum' mode of numpy.pad(), the values in the padded region are set to the maximum value of the original array.

In the below example, the original array is padded with three elements before and two elements after the array using the ‘maximum’ mode. The values in the padded region are set to the maximum value of the original array, which is 7. The resulting padded array is [7, 7, 7, 4, 7, 2, 4, 3, 7, 7].


# Padding array using 'maximum' mode
arr2 = np.pad(arr, (3, 2), 'maximum')
print("After padding the arrays using 'maximum' mode:\n", arr2)

# Output:
# Original array:
#  [4 7 2 4 3]
# After padding the arrays using 'maximum' mode:
#  [7 7 7 4 7 2 4 3 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("After padding the arrays using 'mean' mode:\n", arr2)

# Output:
# Original array:
#  [4 7 2 4 3]
# After padding the arrays using 'mean' mode:
#  [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("After padding the arrays using 'median' mode:\n", arr2)

# Output:
# Original array:
#  [4 7 2 4 3]
# After padding the arrays using 'median' mode:
#  [4 4 4 4 4 7 2 4 3 4 4 4 4]

9. Padding NumPy Array Using ‘minimum’ Mode

In the 'minimum' mode of numpy.pad(), the values in the padded region are set to the minimum value of the original array.

In the below example, the original array is padded with three elements before and two elements after the array using the ‘minimum’ mode. The values in the padded region are set to the minimum value of the original array, which is 2. The resulting padded array is [2, 2, 2, 4, 7, 2, 4, 3, 2, 2].


# Padding array using 'minimum' mode
arr2 = np.pad(arr, (3, 2), 'minimum')
print("After padding the arrays using 'minimum' mode:\n", arr2)

# Output:
# Original array:
#  [4 7 2 4 3]
# After padding the arrays using 'minimum' mode:
#  [2 2 2 4 7 2 4 3 2 2]

10. Use pad() to ‘symmetric’ Mode

In the 'symmetric' mode of numpy.pad(), the values in the padded region are filled with a reflection of the array along the edge of the array.


# Padding array using 'symmetric' mode
arr2 = np.pad(arr, (3, 2), 'symmetric')
print("After padding the arrays using 'symmetric' mode:\n", arr2)

# Output:
# Original array:
#  [4 7 2 4 3]
# After padding the arrays using 'symmetric' mode:
#  [2 7 4 4 7 2 4 3 3 4]

Frequently Asked Questions

What is the purpose of the numpy.pad() function?

The numpy.pad() function in Python is used to pad arrays by adding elements or values at the beginning and end of each axis. It is commonly used to modify the shape and size of arrays, ensuring consistent dimensions in various data processing tasks.

How does the ‘constant’ mode work in numpy.pad()?

In the ‘constant’ mode of numpy.pad(), the padded values are set to a constant value specified by the constant_values parameter. When you use ‘constant’ mode, you provide a constant value that is used to fill the padding area of the array. This mode is particularly useful when you want to pad the array with a specific constant value.

What does the ‘edge’ mode do in numpy.pad()?

In ‘edge’ mode, the padded values are set to the edge values of the original array. It pads the array using the values at the edges, effectively extending the edge values to fill the padding area.

Can numpy.pad() be used to pad multi-dimensional arrays?

numpy.pad() can be used to pad multi-dimensional arrays. You can specify different padding widths for each axis to pad multi-dimensional arrays in a customized manner. When padding multi-dimensional arrays, you need to provide a tuple of pad widths, where each element of the tuple corresponds to the width of padding for a specific axis.

How does the ‘linear_ramp’ mode work in numpy.pad()?

In the ‘linear_ramp’ mode of numpy.pad(), the values in the padded region form a linear ramp starting from the edge value of the original array and incrementing or decrementing by 1 for each element in the padding area.

What is the purpose of the constant_values parameter in numpy.pad()?

The constant_values parameter is used in the ‘constant’ mode of numpy.pad(). It specifies the constant value to which the padded elements are set. This parameter allows you to customize the padding with a specific constant value.

Conclusion

In this article, I have explained the NumPy numpy.pad() function syntax, parameter, and usage of how to get a padded array using numpy.pad() function along with different modes and pad_width with examples.

Happy Learning!!

References