Skip to content

NumPy Pad: Using np.pad() to Pad Arrays and Matrices

NumPy Pad Using np pad() to Pad Arrays and Matrices Cover Image

In this tutorial, you’ll learn how to use the powerful NumPy pad function to pad arrays and matrices in NumPy and Python. The function is a powerful method that is often used in deep learning, especially in developing convolutional neural networks. Being able to master this function will allow you to build powerful deep learning models for image classification and processing.

The usefulness of the function goes beyond deep learning. It allows you to work in with matrices in meaningful ways, such as ensuring that they can be broadcast to a consistent size. Similarly, it can be used in image processing to add borders to an image.

By the end of this tutorial, you’ll have learned:

  • What the np.pad() function is and how to use it
  • How to pad arrays of a single dimension and of multiple dimensions
  • How to customize the padding of values
  • How to use the np.pad() function to add a border to an image

Understanding the NumPy pad Function

Before diving into how to use the np.pad() function, it can be helpful to understand the basic syntax of the function. Let’s take a look at what the function looks like and then explore the different parameters the function has to offer:

# Understanding the np.pad() Function
np.pad(
   array, 
   pad_width, 
   mode='constant', 
   **kwargs
)

The table below breaks down the parameters of the np.pad() function:

ParameterDescriptionExpected ValuesDefault Values
array=The array to be paddedarray-likeN/A
pad_width=The number of values to pad on each edge.sequence, array-like, integerN/A
mode=The type of padding to usestring or function'constant'
stat_length=The number of values at edge of each axis used to calculate a statistic value. Used when mode is any of: 'maximum', 'mean', 'median', or 'minimum'.sequence or integer'None'
constant_values=Used when mode='constant'.sequence or scalar0
end_values=Used when mode='linear_ramp' to provide an end value of the linear rampsequence or scalar0
reflect_type=Used when mode is set to 'reflect' or 'symmetric'.'even' or 'odd'N/A
The parameters of the np.pad() function

For the most part, the most important parameters are the array=, pad_width=, and mode= parameters. The mode= parameter allows for significantly high amount of customization. Let’s take a look at some of the various arguments you can pass in:

  • 'constant': pads with a constant value
  • 'edge': pads using the edge values of the array
  • 'linear-ramp': pads with the linear ramp between the end_value and the array edge value
  • 'maximum': pads with the maximum value of either all or part of the vector along each axis
  • 'mean': pads with the mean value of all or part of the vector along each axis
  • 'median': pads with the median value of all or part of the vector along each axis
  • 'minimum': pads with the minimum value of all or part of the vector along each axis
  • 'reflect': pads with the reflection of the vector
  • 'symmetric': pads with the reflection of the vector, mirrored along the edge of the array
  • 'wrap': pads with the wrap of the vector along the axis
  • 'empty': pads with undefined values

Now that we’ve covered the extensive documentation for the np.pad() function, let’s start looking at some practical examples.

Padding a One-Dimensional NumPy Array

In this first example, you will learn how to pad an array using the NumPy pad function. Let’s start by looking at an array of one dimension, to better understand what is happening.

# Using the np.pad() Function with a Single Dimension
import numpy as np

arr = np.array([1, 2, 3])
padded = np.pad(arr, pad_width=2)
print(padded)

# Returns:
# [0 0 1 2 3 0 0]

In the example above, we padded an array with a width of 2. By default, NumPy will use the value of 0. We can customize the value by changing the constant_value= parameter. Let’s use the value of 100 to pad our array:

# Padding an Array with a Non-Default Value
import numpy as np

arr = np.array([1, 2, 3])
padded = np.pad(arr, pad_width=2, constant_values=100)
print(padded)

# Returns:
# [100 100   1   2   3 100 100]

While padding a one-dimensional array can be helpful, the real power of the function comes into play when padding a multi-dimensional array, such as an image. This is what you’ll learn to do in the following section.

Padding a Multi-Dimensional NumPy Array

In this section, you’ll learn how to use the np.pad() function to pad a multi-dimensional array. This has many useful applications, such as in the creation of convolutional neural networks. Let’s see how we can use the function to pad a multi-dimensional array with constant values:

# Padding a Multi-Dimensional Array with np.pad()
import numpy as np

arr = np.arange(9).reshape(3, 3)
padded = np.pad(arr, pad_width=1)
print(padded)

# Returns:
# [[0 0 0 0 0]
#  [0 0 1 2 0]
#  [0 3 4 5 0]
#  [0 6 7 8 0]
#  [0 0 0 0 0]]

In the example above, we created a 3×3 matrix with the values ranging from 0-9. We then applied padding of size 1 with the default values of 0.

Padding a NumPy Array with Varied Amounts of Padding

Padding an array with the np.pad() function can also be done in varied amounts of padding. This can be helpful if you want to apply padding, say, only to one or two sides. We can do this by passing in a sequence into the pad_width= parameter. Let’s take a look at what this looks like:

# Padding Arrays with Different Amounts
import numpy as np

arr = np.arange(9).reshape(3, 3)
padded = np.pad(arr, pad_width=((1, 0), (0, 1)))
print(padded)

# Returns:
# [[0 0 0 0]
#  [0 1 2 0]
#  [3 4 5 0]
#  [6 7 8 0]]

We can see the in the example above that the function also accepts a sequence-like structure. By passing in the tuple of tuples ((1, 0), (0, 1)), we identify that we want padding of the following structure:

  • 1 on the top
  • 0 on the bottom
  • 0 on the left
  • 1 on the right

Doing this is a powerful way of customizing the ways in which your arrays are padded.

Using NumPy pad to Pad with Nearest Values

In this section, you’ll learn how to use NumPy to pad an array with the nearest values found in the array. This is done by modifying the mode= parameter to use the value of 'edge'. By doing this, this modifies the value to use the value nearest to the edge.

This is better demonstrated by using an example:

# Padding an Array with the Nearest Value Using np.pad()
import numpy as np

arr = np.arange(9).reshape(3, 3)
padded = np.pad(arr, pad_width=1, mode='edge')
print(padded)

# Returns:
# [[0 0 1 2 2]
#  [0 0 1 2 2]
#  [3 3 4 5 5]
#  [6 6 7 8 8]
#  [6 6 7 8 8]]

We can see that the values are moved along to the edge, which can be helpful when padding a tensor for convolutional neural networks.

Using NumPy pad to Add a Border to an Image

In this section, we’ll explore how to use the np.pad() function to add a border to an image. Because images can be easily represented as arrays of values, we can use the np.pad() function to add a border of a solid color to an image.

Let’s take a look at how this works. Feel free to download the cover image and use it to follow along with the tutorial, or feel free to use your own image.

# Using np.pad() to Add a Border to an Image
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import image

arr = image.imread('/Users/nikpi/Downloads/im.jpeg')
with_border = np.pad(arr, pad_width=((100, 100), (100, 100), (0,0)))
plt.imshow(with_border)
plt.show()

This returns the following image:

Adding a border to an image using np.pad()
Adding a border to an image using np.pad()

Mirror an Image Using NumPy pad

In this final section, you’ll learn how to mirror an image using the np.pad() function. This can be helpful if you want to apply some basic transformations to an image. This is often done, for example, when applying simple transformations to allow for better classifications in neural networks.

Let’s take a look at how this is done using the np.pad() function:

# Mirror an Image Using np.pad()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import image

img = image.imread('/Users/nikpi/Downloads/im.jpeg')
height, width, _ = img.shape

plt.imshow(np.pad(img, ((0,0), (0, width), (0, 0)), mode='reflect'))
plt.show()

This returns the following image:

Mirroring an image using the np.pad() Function
Mirroring an image using the np.pad() Function

Conclusion

In this tutorial, you learned how to use the np.pad() function to add padding to a NumPy array. This can be a helpful method to learn to ensure that arrays are of a given size, when the padding should be defined. The function provides a huge number of options to customize output of the function.

You first learned how to pad an array of a single dimension, then a multi-dimensional array. Following that, you learned how to customize the resulting array. Finally, you learned how to work with images in terms of being able to pad and modify images, such as by adding a border or mirroring an image.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *