# Numpy Pad Explained With Examples in Python

Hello geeks and welcome in this article, we will cover the NumPy pad(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part 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 that NumPy is a powerful mathematical library of python. It provides us with a function called NumPy pad(), which adds padding in the arrays. The definition that we just discussed here would become more clear as we move further in this article. In the next section, we will be covering the syntax associated with the function.

Contents

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

This is the general syntax for our function. There are several parameters associated with it which we will be discussing in the next section.

1.array: array_like

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

This parameter represents the number of values padded to 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.

## EXAMPLES

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.

```#input
import numpy as ppool
a=[1,23,4,5]
```

Output:

``[23 23  1 23  4  5 23 23 23]``

In the above example, at first, we have imported the NumPy module. After which, we have defined an input on which the 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 of which we get 23(max value) padded at the front and the back.

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

```#input
import numpy as ppool
a=[18,21,32,43,50,70,4]
```

Output:

``[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 for 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 for the “mean” mode of observation and tell me your results in the comment section? 