Numpy Pad Explained With Examples in Python

Hello, geeks, and welcome to this article. Today, we will cover the NumPy pad(). Along with that, we will also look at its syntax and parameters for a better overall understanding. Then, we will see the application of all the theory parts through a couple of examples. But at first, let us try to get a brief understanding of the function through its definition.

As we know, NumPy is a powerful mathematical library of Python. It provides us with a function called NumPy pad(), which adds padding to the arrays. The definition we just discussed here will become clearer as we move further in this article.

In the next section, we will be covering the syntax associated with the function.

Syntax Of Numpy Pad()

numpy.pad(array, pad_width, mode='')

This is the general syntax for our function. Several parameters are associated with it, which we will discuss in the next section.

Parameters Of Numpy Pad()

1. array: array_like

This represents the input array on which the padding needs to be performed.

2. pad_width:{sequence, array_like, int}

This parameter represents the number of values padded to the edge of each axis.

3. mode: str or functional

It represents either one of the following string values or a user-supplied function.

  • constant: In this case, the padding takes place with a constant value.
  • edge: In this case, padding takes place with the edge value of an array.
  • maximum: In this case, the padding takes the maximum value of all vector parts along the given axis.
  • minimum: In this case, the padding takes the minimum value of all vector parts along the given axis.
  • mean: In this case, the padding takes the mean value of all parts of the vector along the given axis.
  • median: In this case, the padding takes the maximum value of all vector parts along the given axis.
  • symmetric: In this case, the padding occurs along with the vector mirrored reflection along the array’s edge.
  • reflect: In this case, the Padding takes place with the vector’s reflection on the first and last values along each axis.

4. stat_length: sequence or int

This parameter is used in ‘maximum,’ ‘mean,’ ‘median,’ and ‘minimum.’ Here, the number of values at the edge of each axis is used to calculate the statistic value.

5. constant values: sequence or scalar

This parameter is used in ‘constant’. Here, the values are used to set the padded values for each axis.

6. reflect_type:{even, odd}

This parameter is used with the reflect and symmetric type.


As we are done with all the theory portion related to NumPy pad(), in this section, we will be looking at how this function works and how it helps us achieve our desired output. We will start with an elementary-level example and gradually move our way to more complicated examples.

import numpy as ppool



[23 23  1 23  4  5 23 23 23]

In the above example, at first, we have imported the NumPy module. After this, we defined an input on which operation needs to be performed. Then, we have used our syntax to get the desired output. In this example, we have used “Maximum” as our choice for mode. As a result, we get 23(max value) padded at the front and the back.

Now, let us look at one more example with a different mode.

import numpy as ppool



[70 50 43 32 21 18 21 32 43 50 70  4 70]

A similar example to that of the first one. But here, we have used a different array. Also, here, we have used “reflect” as our mode of observation. We can spot the difference in the output.

I tried out for the “maximum” and “reflect” mode. Why don’t you guys try out the “mean” mode of observation and tell me your results in the comment section?

Must Read


In this article, we covered the NumPy pad(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples.

We varied the syntax and looked at the output for each case. We can conclude that the NumPy pad() helps us add padding to the array.  I hope this article was able to clear all doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

Done reading this, why not read Memmap next?

Notify of
Inline Feedbacks
View all comments